Memory.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Collection;
  3. use Psr\SimpleCache\CacheInterface;
  4. /**
  5. * This is the default implementation for in-memory cell collection.
  6. *
  7. * Alternatives implementation should leverage off-memory, non-volatile storage
  8. * to reduce overall memory usage.
  9. */
  10. class Memory implements CacheInterface
  11. {
  12. private $cache = [];
  13. public function clear()
  14. {
  15. $this->cache = [];
  16. return true;
  17. }
  18. public function delete($key)
  19. {
  20. unset($this->cache[$key]);
  21. return true;
  22. }
  23. public function deleteMultiple($keys)
  24. {
  25. foreach ($keys as $key) {
  26. $this->delete($key);
  27. }
  28. return true;
  29. }
  30. public function get($key, $default = null)
  31. {
  32. if ($this->has($key)) {
  33. return $this->cache[$key];
  34. }
  35. return $default;
  36. }
  37. public function getMultiple($keys, $default = null)
  38. {
  39. $results = [];
  40. foreach ($keys as $key) {
  41. $results[$key] = $this->get($key, $default);
  42. }
  43. return $results;
  44. }
  45. public function has($key)
  46. {
  47. return array_key_exists($key, $this->cache);
  48. }
  49. public function set($key, $value, $ttl = null)
  50. {
  51. $this->cache[$key] = $value;
  52. return true;
  53. }
  54. public function setMultiple($values, $ttl = null)
  55. {
  56. foreach ($values as $key => $value) {
  57. $this->set($key, $value);
  58. }
  59. return true;
  60. }
  61. }