StashPool.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace Elgg\Cache;
  3. use Stash;
  4. /**
  5. * Defers to Stash for the meat of the caching logic.
  6. *
  7. * WARNING: API IN FLUX. DO NOT USE DIRECTLY.
  8. *
  9. * @package Elgg
  10. * @subpackage Cache
  11. * @since 1.10.0
  12. *
  13. * @access private
  14. */
  15. final class StashPool implements Pool {
  16. /** @var Stash\Pool */
  17. private $stash;
  18. /**
  19. * Constructor
  20. *
  21. * @param Stash\Pool $stash The Stash instance to be decorated.
  22. */
  23. public function __construct(Stash\Pool $stash) {
  24. $this->stash = $stash;
  25. }
  26. /** @inheritDoc */
  27. public function get($key, callable $callback) {
  28. assert(is_string($key) || is_int($key));
  29. $item = $this->stash->getItem((string)$key);
  30. $result = $item->get();
  31. if ($item->isMiss()) {
  32. $item->lock();
  33. $result = call_user_func($callback);
  34. $this->stash->save($item->set($result));
  35. }
  36. return $result;
  37. }
  38. /** @inheritDoc */
  39. public function invalidate($key) {
  40. assert(is_string($key) || is_int($key));
  41. $this->stash->getItem((string)$key)->clear();
  42. }
  43. /** @inheritDoc */
  44. public function put($key, $value) {
  45. assert(is_string($key) || is_int($key));
  46. $this->stash->getItem((string)$key)->set($value);
  47. }
  48. /**
  49. * Create an in-memory implementation of the pool.
  50. *
  51. * @return StashPool
  52. */
  53. public static function createEphemeral() {
  54. return new self(new Stash\Pool(new Stash\Driver\Ephemeral()));
  55. }
  56. }