MemoryPool.php 926 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace Elgg\Cache;
  3. use Stash;
  4. /**
  5. * An in-memory implementation of a cache pool.
  6. *
  7. * NB: Data put into this cache is not persisted between requests.
  8. *
  9. * WARNING: API IN FLUX. DO NOT USE DIRECTLY.
  10. *
  11. * @package Elgg
  12. * @subpackage Cache
  13. * @since 1.10.0
  14. *
  15. * @access private
  16. */
  17. final class MemoryPool implements Pool {
  18. /**
  19. * @var array
  20. */
  21. private $values = array();
  22. /** @inheritDoc */
  23. public function get($key, callable $callback) {
  24. assert(is_string($key) || is_int($key));
  25. if (!array_key_exists($key, $this->values)) {
  26. $this->values[$key] = call_user_func($callback);
  27. }
  28. return $this->values[$key];
  29. }
  30. /** @inheritDoc */
  31. public function invalidate($key) {
  32. assert(is_string($key) || is_int($key));
  33. unset($this->values[$key]);
  34. }
  35. /** @inheritDoc */
  36. public function put($key, $value) {
  37. assert(is_string($key) || is_int($key));
  38. $this->values[$key] = $value;
  39. }
  40. }