CacheInterface.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace React\Cache;
  3. use React\Promise\PromiseInterface;
  4. interface CacheInterface
  5. {
  6. /**
  7. * Retrieves an item from the cache.
  8. *
  9. * This method will resolve with the cached value on success or with the
  10. * given `$default` value when no item can be found or when an error occurs.
  11. * Similarly, an expired cache item (once the time-to-live is expired) is
  12. * considered a cache miss.
  13. *
  14. * ```php
  15. * $cache
  16. * ->get('foo')
  17. * ->then('var_dump');
  18. * ```
  19. *
  20. * This example fetches the value of the key `foo` and passes it to the
  21. * `var_dump` function. You can use any of the composition provided by
  22. * [promises](https://github.com/reactphp/promise).
  23. *
  24. * @param string $key
  25. * @param mixed $default Default value to return for cache miss or null if not given.
  26. * @return PromiseInterface<mixed>
  27. */
  28. public function get($key, $default = null);
  29. /**
  30. * Stores an item in the cache.
  31. *
  32. * This method will resolve with `true` on success or `false` when an error
  33. * occurs. If the cache implementation has to go over the network to store
  34. * it, it may take a while.
  35. *
  36. * The optional `$ttl` parameter sets the maximum time-to-live in seconds
  37. * for this cache item. If this parameter is omitted (or `null`), the item
  38. * will stay in the cache for as long as the underlying implementation
  39. * supports. Trying to access an expired cache item results in a cache miss,
  40. * see also [`get()`](#get).
  41. *
  42. * ```php
  43. * $cache->set('foo', 'bar', 60);
  44. * ```
  45. *
  46. * This example eventually sets the value of the key `foo` to `bar`. If it
  47. * already exists, it is overridden.
  48. *
  49. * This interface does not enforce any particular TTL resolution, so special
  50. * care may have to be taken if you rely on very high precision with
  51. * millisecond accuracy or below. Cache implementations SHOULD work on a
  52. * best effort basis and SHOULD provide at least second accuracy unless
  53. * otherwise noted. Many existing cache implementations are known to provide
  54. * microsecond or millisecond accuracy, but it's generally not recommended
  55. * to rely on this high precision.
  56. *
  57. * This interface suggests that cache implementations SHOULD use a monotonic
  58. * time source if available. Given that a monotonic time source is only
  59. * available as of PHP 7.3 by default, cache implementations MAY fall back
  60. * to using wall-clock time.
  61. * While this does not affect many common use cases, this is an important
  62. * distinction for programs that rely on a high time precision or on systems
  63. * that are subject to discontinuous time adjustments (time jumps).
  64. * This means that if you store a cache item with a TTL of 30s and then
  65. * adjust your system time forward by 20s, the cache item SHOULD still
  66. * expire in 30s.
  67. *
  68. * @param string $key
  69. * @param mixed $value
  70. * @param ?float $ttl
  71. * @return PromiseInterface<bool> Returns a promise which resolves to `true` on success or `false` on error
  72. */
  73. public function set($key, $value, $ttl = null);
  74. /**
  75. * Deletes an item from the cache.
  76. *
  77. * This method will resolve with `true` on success or `false` when an error
  78. * occurs. When no item for `$key` is found in the cache, it also resolves
  79. * to `true`. If the cache implementation has to go over the network to
  80. * delete it, it may take a while.
  81. *
  82. * ```php
  83. * $cache->delete('foo');
  84. * ```
  85. *
  86. * This example eventually deletes the key `foo` from the cache. As with
  87. * `set()`, this may not happen instantly and a promise is returned to
  88. * provide guarantees whether or not the item has been removed from cache.
  89. *
  90. * @param string $key
  91. * @return PromiseInterface<bool> Returns a promise which resolves to `true` on success or `false` on error
  92. */
  93. public function delete($key);
  94. /**
  95. * Retrieves multiple cache items by their unique keys.
  96. *
  97. * This method will resolve with an array of cached values on success or with the
  98. * given `$default` value when an item can not be found or when an error occurs.
  99. * Similarly, an expired cache item (once the time-to-live is expired) is
  100. * considered a cache miss.
  101. *
  102. * ```php
  103. * $cache->getMultiple(array('name', 'age'))->then(function (array $values) {
  104. * $name = $values['name'] ?? 'User';
  105. * $age = $values['age'] ?? 'n/a';
  106. *
  107. * echo $name . ' is ' . $age . PHP_EOL;
  108. * });
  109. * ```
  110. *
  111. * This example fetches the cache items for the `name` and `age` keys and
  112. * prints some example output. You can use any of the composition provided
  113. * by [promises](https://github.com/reactphp/promise).
  114. *
  115. * @param string[] $keys A list of keys that can obtained in a single operation.
  116. * @param mixed $default Default value to return for keys that do not exist.
  117. * @return PromiseInterface<array> Returns a promise which resolves to an `array` of cached values
  118. */
  119. public function getMultiple(array $keys, $default = null);
  120. /**
  121. * Persists a set of key => value pairs in the cache, with an optional TTL.
  122. *
  123. * This method will resolve with `true` on success or `false` when an error
  124. * occurs. If the cache implementation has to go over the network to store
  125. * it, it may take a while.
  126. *
  127. * The optional `$ttl` parameter sets the maximum time-to-live in seconds
  128. * for these cache items. If this parameter is omitted (or `null`), these items
  129. * will stay in the cache for as long as the underlying implementation
  130. * supports. Trying to access an expired cache items results in a cache miss,
  131. * see also [`get()`](#get).
  132. *
  133. * ```php
  134. * $cache->setMultiple(array('foo' => 1, 'bar' => 2), 60);
  135. * ```
  136. *
  137. * This example eventually sets the list of values - the key `foo` to 1 value
  138. * and the key `bar` to 2. If some of the keys already exist, they are overridden.
  139. *
  140. * @param array $values A list of key => value pairs for a multiple-set operation.
  141. * @param ?float $ttl Optional. The TTL value of this item.
  142. * @return PromiseInterface<bool> Returns a promise which resolves to `true` on success or `false` on error
  143. */
  144. public function setMultiple(array $values, $ttl = null);
  145. /**
  146. * Deletes multiple cache items in a single operation.
  147. *
  148. * @param string[] $keys A list of string-based keys to be deleted.
  149. * @return PromiseInterface<bool> Returns a promise which resolves to `true` on success or `false` on error
  150. */
  151. public function deleteMultiple(array $keys);
  152. /**
  153. * Wipes clean the entire cache.
  154. *
  155. * @return PromiseInterface<bool> Returns a promise which resolves to `true` on success or `false` on error
  156. */
  157. public function clear();
  158. /**
  159. * Determines whether an item is present in the cache.
  160. *
  161. * This method will resolve with `true` on success or `false` when no item can be found
  162. * or when an error occurs. Similarly, an expired cache item (once the time-to-live
  163. * is expired) is considered a cache miss.
  164. *
  165. * ```php
  166. * $cache
  167. * ->has('foo')
  168. * ->then('var_dump');
  169. * ```
  170. *
  171. * This example checks if the value of the key `foo` is set in the cache and passes
  172. * the result to the `var_dump` function. You can use any of the composition provided by
  173. * [promises](https://github.com/reactphp/promise).
  174. *
  175. * NOTE: It is recommended that has() is only to be used for cache warming type purposes
  176. * and not to be used within your live applications operations for get/set, as this method
  177. * is subject to a race condition where your has() will return true and immediately after,
  178. * another script can remove it making the state of your app out of date.
  179. *
  180. * @param string $key The cache item key.
  181. * @return PromiseInterface<bool> Returns a promise which resolves to `true` on success or `false` on error
  182. */
  183. public function has($key);
  184. }