Apcu.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php declare (strict_types=1);
  2. namespace Sabre\Cache;
  3. use DateInterval;
  4. use DateTime;
  5. use Psr\SimpleCache\CacheInterface;
  6. use Traversable;
  7. /**
  8. * The Apcu Cache uses the apcu_* functions from PHP to manage the cache.
  9. *
  10. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  11. * @author Evert Pot (https://evertpot.com/)
  12. * @license http://sabre.io/license/
  13. */
  14. class Apcu implements CacheInterface {
  15. use MultipleTrait;
  16. /**
  17. * Fetches a value from the cache.
  18. *
  19. * @param string $key The unique key of this item in the cache.
  20. * @param mixed $default Default value to return if the key does not exist.
  21. *
  22. * @throws \Psr\SimpleCache\InvalidArgumentException
  23. * MUST be thrown if the $key string is not a legal value.
  24. * @return mixed The value of the item from the cache, or $default in case of cache miss.
  25. */
  26. function get($key, $default = null) {
  27. if (!is_string($key)) {
  28. throw new InvalidArgumentException('$key must be a string');
  29. }
  30. $value = apcu_fetch($key, $success);
  31. if (!$success) {
  32. return $default;
  33. }
  34. return $value;
  35. }
  36. /**
  37. * Persists data in the cache, uniquely referenced by a key with an
  38. * optional expiration TTL time.
  39. *
  40. * @param string $key The key of the item to store.
  41. * @param mixed $value The value of the item to store, must
  42. * be serializable.
  43. * @param null|int|DateInterval $ttl Optional. The TTL value of this item.
  44. * If no value is sent and the driver
  45. * supports TTL then the library may set
  46. * a default value for it or let the
  47. * driver take care of that.
  48. *
  49. * @throws \Psr\SimpleCache\InvalidArgumentException
  50. * MUST be thrown if the $key string is not a legal value.
  51. * @return bool True on success and false on failure.
  52. */
  53. function set($key, $value, $ttl = null) {
  54. if (!is_string($key)) {
  55. throw new InvalidArgumentException('$key must be a string');
  56. }
  57. if ($ttl instanceof DateInterval) {
  58. // Converting to a TTL in seconds
  59. $ttl = (new DateTime('now'))->add($ttl)->getTimeStamp() - time();
  60. }
  61. return apcu_store($key, $value, (int)$ttl);
  62. }
  63. /**
  64. * Delete an item from the cache by its unique key.
  65. *
  66. * @param string $key The unique cache key of the item to delete.
  67. *
  68. * @throws \Psr\SimpleCache\InvalidArgumentException
  69. * MUST be thrown if the $key string is not a legal value.
  70. * @return bool True if the item was successfully removed. False if there was an error.
  71. */
  72. function delete($key) {
  73. if (!is_string($key)) {
  74. throw new InvalidArgumentException('$key must be a string');
  75. }
  76. return apcu_delete($key);
  77. }
  78. /**
  79. * Wipes clean the entire cache's keys.
  80. *
  81. * @return bool True on success and false on failure.
  82. */
  83. function clear() {
  84. return apcu_clear_cache();
  85. }
  86. /**
  87. * Determines whether an item is present in the cache.
  88. *
  89. * NOTE: It is recommended that has() is only to be used for cache warming
  90. * type purposes and not to be used within your live applications operations
  91. * for get/set, as this method is subject to a race condition where your
  92. * has() will return true and immediately after, another script can remove
  93. * it making the state of your app out of date.
  94. *
  95. * @param string $key The cache item key.
  96. * @throws \Psr\SimpleCache\InvalidArgumentException
  97. * MUST be thrown if the $key string is not a legal value.
  98. * @return bool
  99. */
  100. function has($key) {
  101. if (!is_string($key)) {
  102. throw new InvalidArgumentException('$key must be a string');
  103. }
  104. return apcu_exists($key);
  105. }
  106. /**
  107. * Persists a set of key => value pairs in the cache, with an optional TTL.
  108. *
  109. * @param iterable $values A list of key => value pairs for a
  110. * multiple-set operation.
  111. * @param null|int|DateInterval $ttl Optional. The TTL value of this
  112. * item. If no value is sent and the
  113. * driver supports TTL then the library
  114. * may set a default value for it or
  115. * let the driver take care of that.
  116. *
  117. * @throws \Psr\SimpleCache\InvalidArgumentException
  118. * MUST be thrown if $values is neither an array nor a Traversable,
  119. * or if any of the $values are not a legal value.
  120. * @return bool True on success and false on failure.
  121. */
  122. function setMultiple($values, $ttl = null) {
  123. if (!is_array($values) && !$values instanceof Traversable) {
  124. throw new InvalidArgumentException('$values must be traversable');
  125. }
  126. if ($ttl instanceof DateInterval) {
  127. // Converting to a TTL in seconds
  128. $ttl = (new DateTime('now'))->add($ttl)->getTimeStamp() - time();
  129. }
  130. if ($values instanceof Traversable) {
  131. $values = iterator_to_array($values);
  132. }
  133. return apcu_store($values, null, (int)$ttl);
  134. }
  135. /**
  136. * Deletes multiple cache items in a single operation.
  137. *
  138. * @param iterable $keys A list of string-based keys to be deleted.
  139. *
  140. * @throws \Psr\SimpleCache\InvalidArgumentException
  141. * MUST be thrown if $keys is neither an array nor a Traversable,
  142. * or if any of the $keys are not a legal value.
  143. * @return bool True if the items were successfully removed. False if there
  144. * was an error.
  145. */
  146. function deleteMultiple($keys) {
  147. if ($keys instanceof Traversable) {
  148. $keys = iterator_to_array($keys);
  149. } elseif (!is_array($keys)) {
  150. throw new InvalidArgumentException('$keys must be iterable');
  151. }
  152. return apcu_delete($keys);
  153. }
  154. }