CacheInterface.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\caching;
  8. /**
  9. * CacheInterface is the base interface for cache.
  10. *
  11. * A data item can be stored in the cache by calling [[set()]] and be retrieved back
  12. * later (in the same or different request) by [[get()]]. In both operations,
  13. * a key identifying the data item is required. An expiration time and/or a [[Dependency|dependency]]
  14. * can also be specified when calling [[set()]]. If the data item expires or the dependency
  15. * changes at the time of calling [[get()]], the cache will return no data.
  16. *
  17. * A typical usage pattern of cache is like the following:
  18. *
  19. * ```php
  20. * $key = 'demo';
  21. * $data = $cache->get($key);
  22. * if ($data === false) {
  23. * // ...generate $data here...
  24. * $cache->set($key, $data, $duration, $dependency);
  25. * }
  26. * ```
  27. *
  28. * Because CacheInterface extends the [[\ArrayAccess]] interface, it can be used like an array. For example,
  29. *
  30. * ```php
  31. * $cache['foo'] = 'some data';
  32. * echo $cache['foo'];
  33. * ```
  34. *
  35. * For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
  36. *
  37. * @author Qiang Xue <qiang.xue@gmail.com>
  38. * @author Dmitry Naumenko <d.naumenko.a@gmail.com>
  39. * @since 2.0.13. Previous framework versions used abstract class [[yii\caching\Cache]] as interface.
  40. */
  41. interface CacheInterface extends \ArrayAccess
  42. {
  43. /**
  44. * Builds a normalized cache key from a given key.
  45. *
  46. * If the given key is a string containing alphanumeric characters only and no more than 32 characters,
  47. * then the key will be returned back prefixed with [[keyPrefix]]. Otherwise, a normalized key
  48. * is generated by serializing the given key, applying MD5 hashing, and prefixing with [[keyPrefix]].
  49. *
  50. * @param mixed $key the key to be normalized
  51. * @return string the generated cache key
  52. */
  53. public function buildKey($key);
  54. /**
  55. * Retrieves a value from cache with a specified key.
  56. * @param mixed $key a key identifying the cached value. This can be a simple string or
  57. * a complex data structure consisting of factors representing the key.
  58. * @return mixed the value stored in cache, false if the value is not in the cache, expired,
  59. * or the dependency associated with the cached data has changed.
  60. */
  61. public function get($key);
  62. /**
  63. * Checks whether a specified key exists in the cache.
  64. * This can be faster than getting the value from the cache if the data is big.
  65. * In case a cache does not support this feature natively, this method will try to simulate it
  66. * but has no performance improvement over getting it.
  67. * Note that this method does not check whether the dependency associated
  68. * with the cached data, if there is any, has changed. So a call to [[get]]
  69. * may return false while exists returns true.
  70. * @param mixed $key a key identifying the cached value. This can be a simple string or
  71. * a complex data structure consisting of factors representing the key.
  72. * @return bool true if a value exists in cache, false if the value is not in the cache or expired.
  73. */
  74. public function exists($key);
  75. /**
  76. * Retrieves multiple values from cache with the specified keys.
  77. * Some caches (such as memcache, apc) allow retrieving multiple cached values at the same time,
  78. * which may improve the performance. In case a cache does not support this feature natively,
  79. * this method will try to simulate it.
  80. * @param string[] $keys list of string keys identifying the cached values
  81. * @return array list of cached values corresponding to the specified keys. The array
  82. * is returned in terms of (key, value) pairs.
  83. * If a value is not cached or expired, the corresponding array value will be false.
  84. */
  85. public function multiGet($keys);
  86. /**
  87. * Stores a value identified by a key into cache.
  88. * If the cache already contains such a key, the existing value and
  89. * expiration time will be replaced with the new ones, respectively.
  90. *
  91. * @param mixed $key a key identifying the value to be cached. This can be a simple string or
  92. * a complex data structure consisting of factors representing the key.
  93. * @param mixed $value the value to be cached
  94. * @param int $duration default duration in seconds before the cache will expire. If not set,
  95. * default [[defaultDuration]] value is used.
  96. * @param Dependency $dependency dependency of the cached item. If the dependency changes,
  97. * the corresponding value in the cache will be invalidated when it is fetched via [[get()]].
  98. * This parameter is ignored if [[serializer]] is false.
  99. * @return bool whether the value is successfully stored into cache
  100. */
  101. public function set($key, $value, $duration = null, $dependency = null);
  102. /**
  103. * Stores multiple items in cache. Each item contains a value identified by a key.
  104. * If the cache already contains such a key, the existing value and
  105. * expiration time will be replaced with the new ones, respectively.
  106. *
  107. * @param array $items the items to be cached, as key-value pairs.
  108. * @param int $duration default number of seconds in which the cached values will expire. 0 means never expire.
  109. * @param Dependency $dependency dependency of the cached items. If the dependency changes,
  110. * the corresponding values in the cache will be invalidated when it is fetched via [[get()]].
  111. * This parameter is ignored if [[serializer]] is false.
  112. * @return array array of failed keys
  113. */
  114. public function multiSet($items, $duration = 0, $dependency = null);
  115. /**
  116. * Stores a value identified by a key into cache if the cache does not contain this key.
  117. * Nothing will be done if the cache already contains the key.
  118. * @param mixed $key a key identifying the value to be cached. This can be a simple string or
  119. * a complex data structure consisting of factors representing the key.
  120. * @param mixed $value the value to be cached
  121. * @param int $duration the number of seconds in which the cached value will expire. 0 means never expire.
  122. * @param Dependency $dependency dependency of the cached item. If the dependency changes,
  123. * the corresponding value in the cache will be invalidated when it is fetched via [[get()]].
  124. * This parameter is ignored if [[serializer]] is false.
  125. * @return bool whether the value is successfully stored into cache
  126. */
  127. public function add($key, $value, $duration = 0, $dependency = null);
  128. /**
  129. * Stores multiple items in cache. Each item contains a value identified by a key.
  130. * If the cache already contains such a key, the existing value and expiration time will be preserved.
  131. *
  132. * @param array $items the items to be cached, as key-value pairs.
  133. * @param int $duration default number of seconds in which the cached values will expire. 0 means never expire.
  134. * @param Dependency $dependency dependency of the cached items. If the dependency changes,
  135. * the corresponding values in the cache will be invalidated when it is fetched via [[get()]].
  136. * This parameter is ignored if [[serializer]] is false.
  137. * @return array array of failed keys
  138. */
  139. public function multiAdd($items, $duration = 0, $dependency = null);
  140. /**
  141. * Deletes a value with the specified key from cache.
  142. * @param mixed $key a key identifying the value to be deleted from cache. This can be a simple string or
  143. * a complex data structure consisting of factors representing the key.
  144. * @return bool if no error happens during deletion
  145. */
  146. public function delete($key);
  147. /**
  148. * Deletes all values from cache.
  149. * Be careful of performing this operation if the cache is shared among multiple applications.
  150. * @return bool whether the flush operation was successful.
  151. */
  152. public function flush();
  153. /**
  154. * Method combines both [[set()]] and [[get()]] methods to retrieve value identified by a $key,
  155. * or to store the result of $callable execution if there is no cache available for the $key.
  156. *
  157. * Usage example:
  158. *
  159. * ```php
  160. * public function getTopProducts($count = 10) {
  161. * $cache = $this->cache; // Could be Yii::$app->cache
  162. * return $cache->getOrSet(['top-n-products', 'n' => $count], function ($cache) use ($count) {
  163. * return Products::find()->mostPopular()->limit($count)->all();
  164. * }, 1000);
  165. * }
  166. * ```
  167. *
  168. * @param mixed $key a key identifying the value to be cached. This can be a simple string or
  169. * a complex data structure consisting of factors representing the key.
  170. * @param callable|\Closure $callable the callable or closure that will be used to generate a value to be cached.
  171. * In case $callable returns `false`, the value will not be cached.
  172. * @param int $duration default duration in seconds before the cache will expire. If not set,
  173. * [[defaultDuration]] value will be used.
  174. * @param Dependency $dependency dependency of the cached item. If the dependency changes,
  175. * the corresponding value in the cache will be invalidated when it is fetched via [[get()]].
  176. * This parameter is ignored if [[serializer]] is `false`.
  177. * @return mixed result of $callable execution
  178. */
  179. public function getOrSet($key, $callable, $duration = null, $dependency = null);
  180. }