Memory.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php declare (strict_types=1);
  2. namespace Sabre\Cache;
  3. use DateInterval;
  4. use DateTime;
  5. use Psr\SimpleCache\CacheInterface;
  6. /**
  7. * The Memory cache just stores everything in PHP memory. This cache is gone
  8. * once the process ends.
  9. *
  10. * This is useful as a test-double or for long-running processes that just need
  11. * a local fast cache.
  12. *
  13. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  14. * @author Evert Pot (https://evertpot.com/)
  15. * @license http://sabre.io/license/
  16. */
  17. class Memory implements CacheInterface {
  18. protected $cache = [];
  19. use MultipleTrait;
  20. /**
  21. * Fetches a value from the cache.
  22. *
  23. * @param string $key The unique key of this item in the cache.
  24. * @param mixed $default Default value to return if the key does not exist.
  25. *
  26. * @throws \Psr\SimpleCache\InvalidArgumentException
  27. * MUST be thrown if the $key string is not a legal value.
  28. * @return mixed The value of the item from the cache, or $default in case of cache miss.
  29. */
  30. function get($key, $default = null) {
  31. if (!is_string($key)) {
  32. throw new InvalidArgumentException('$key must be a string');
  33. }
  34. if (!isset($this->cache[$key])) {
  35. return $default;
  36. }
  37. list($expire, $value) = $this->cache[$key];
  38. if (!is_null($expire) && $expire < time()) {
  39. // If a ttl was set and it expired in the past, invalidate the
  40. // cache.
  41. $this->delete($key);
  42. return $default;
  43. }
  44. return $value;
  45. }
  46. /**
  47. * Persists data in the cache, uniquely referenced by a key with an
  48. * optional expiration TTL time.
  49. *
  50. * @param string $key The key of the item to store.
  51. * @param mixed $value The value of the item to store, must
  52. * be serializable.
  53. * @param null|int|DateInterval $ttl Optional. The TTL value of this item.
  54. * If no value is sent and the driver
  55. * supports TTL then the library may set
  56. * a default value for it or let the
  57. * driver take care of that.
  58. *
  59. * @throws \Psr\SimpleCache\InvalidArgumentException
  60. * MUST be thrown if the $key string is not a legal value.
  61. * @return bool True on success and false on failure.
  62. */
  63. function set($key, $value, $ttl = null) {
  64. if (!is_string($key)) {
  65. throw new InvalidArgumentException('$key must be a string');
  66. }
  67. if ($ttl instanceof DateInterval) {
  68. $expire = (new DateTime('now'))->add($ttl)->getTimeStamp();
  69. } elseif (is_int($ttl) || ctype_digit($ttl)) {
  70. $expire = time() + $ttl;
  71. } else {
  72. $expire = null;
  73. }
  74. $this->cache[$key] = [$expire, $value];
  75. return true;
  76. }
  77. /**
  78. * Delete an item from the cache by its unique key.
  79. *
  80. * @param string $key The unique cache key of the item to delete.
  81. *
  82. * @throws \Psr\SimpleCache\InvalidArgumentException
  83. * MUST be thrown if the $key string is not a legal value.
  84. * @return bool True if the item was successfully removed. False if there was an error.
  85. */
  86. function delete($key) {
  87. if (!is_string($key)) {
  88. throw new InvalidArgumentException('$key must be a string');
  89. }
  90. unset($this->cache[$key]);
  91. return true;
  92. }
  93. /**
  94. * Wipes clean the entire cache's keys.
  95. *
  96. * @return bool True on success and false on failure.
  97. */
  98. function clear() {
  99. $this->cache = [];
  100. return true;
  101. }
  102. /**
  103. * Determines whether an item is present in the cache.
  104. *
  105. * NOTE: It is recommended that has() is only to be used for cache warming
  106. * type purposes and not to be used within your live applications operations
  107. * for get/set, as this method is subject to a race condition where your
  108. * has() will return true and immediately after, another script can remove
  109. * it making the state of your app out of date.
  110. *
  111. * @param string $key The cache item key.
  112. * @throws \Psr\SimpleCache\InvalidArgumentException
  113. * MUST be thrown if the $key string is not a legal value.
  114. * @return bool
  115. */
  116. function has($key) {
  117. if (!is_string($key)) {
  118. throw new InvalidArgumentException('$key must be a string');
  119. }
  120. return isset($this->cache[$key]);
  121. }
  122. }