MemoryBackend.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. namespace Drupal\Core\Cache;
  3. use Drupal\Component\Assertion\Inspector;
  4. /**
  5. * Defines a memory cache implementation.
  6. *
  7. * Stores cache items in memory using a PHP array.
  8. *
  9. * Should be used for unit tests and specialist use-cases only, does not
  10. * store cached items between requests.
  11. *
  12. * The functions ::prepareItem()/::set() use unserialize()/serialize(). It
  13. * behaves as an external cache backend to avoid changing the cached data by
  14. * reference. In ::prepareItem(), the object is not modified by the call to
  15. * unserialize() because we make a clone of it.
  16. *
  17. * @ingroup cache
  18. */
  19. class MemoryBackend implements CacheBackendInterface, CacheTagsInvalidatorInterface {
  20. /**
  21. * Array to store cache objects.
  22. */
  23. protected $cache = [];
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function get($cid, $allow_invalid = FALSE) {
  28. if (isset($this->cache[$cid])) {
  29. return $this->prepareItem($this->cache[$cid], $allow_invalid);
  30. }
  31. else {
  32. return FALSE;
  33. }
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function getMultiple(&$cids, $allow_invalid = FALSE) {
  39. $ret = [];
  40. $items = array_intersect_key($this->cache, array_flip($cids));
  41. foreach ($items as $item) {
  42. $item = $this->prepareItem($item, $allow_invalid);
  43. if ($item) {
  44. $ret[$item->cid] = $item;
  45. }
  46. }
  47. $cids = array_diff($cids, array_keys($ret));
  48. return $ret;
  49. }
  50. /**
  51. * Prepares a cached item.
  52. *
  53. * Checks that items are either permanent or did not expire, and returns data
  54. * as appropriate.
  55. *
  56. * @param object $cache
  57. * An item loaded from self::get() or self::getMultiple().
  58. * @param bool $allow_invalid
  59. * (optional) If TRUE, cache items may be returned even if they have expired
  60. * or been invalidated.
  61. *
  62. * @return mixed
  63. * The item with data as appropriate or FALSE if there is no
  64. * valid item to load.
  65. */
  66. protected function prepareItem($cache, $allow_invalid) {
  67. if (!isset($cache->data)) {
  68. return FALSE;
  69. }
  70. // The object passed into this function is the one stored in $this->cache.
  71. // We must clone it as part of the preparation step so that the actual
  72. // cache object is not affected by the unserialize() call or other
  73. // manipulations of the returned object.
  74. $prepared = clone $cache;
  75. $prepared->data = unserialize($prepared->data);
  76. // Check expire time.
  77. $prepared->valid = $prepared->expire == Cache::PERMANENT || $prepared->expire >= $this->getRequestTime();
  78. if (!$allow_invalid && !$prepared->valid) {
  79. return FALSE;
  80. }
  81. return $prepared;
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = []) {
  87. assert(Inspector::assertAllStrings($tags), 'Cache Tags must be strings.');
  88. $tags = array_unique($tags);
  89. // Sort the cache tags so that they are stored consistently in the database.
  90. sort($tags);
  91. $this->cache[$cid] = (object) [
  92. 'cid' => $cid,
  93. 'data' => serialize($data),
  94. 'created' => $this->getRequestTime(),
  95. 'expire' => $expire,
  96. 'tags' => $tags,
  97. ];
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function setMultiple(array $items = []) {
  103. foreach ($items as $cid => $item) {
  104. $this->set($cid, $item['data'], isset($item['expire']) ? $item['expire'] : CacheBackendInterface::CACHE_PERMANENT, isset($item['tags']) ? $item['tags'] : []);
  105. }
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function delete($cid) {
  111. unset($this->cache[$cid]);
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function deleteMultiple(array $cids) {
  117. $this->cache = array_diff_key($this->cache, array_flip($cids));
  118. }
  119. /**
  120. * {@inheritdoc}
  121. */
  122. public function deleteAll() {
  123. $this->cache = [];
  124. }
  125. /**
  126. * {@inheritdoc}
  127. */
  128. public function invalidate($cid) {
  129. if (isset($this->cache[$cid])) {
  130. $this->cache[$cid]->expire = $this->getRequestTime() - 1;
  131. }
  132. }
  133. /**
  134. * {@inheritdoc}
  135. */
  136. public function invalidateMultiple(array $cids) {
  137. $items = array_intersect_key($this->cache, array_flip($cids));
  138. foreach ($items as $cid => $item) {
  139. $this->cache[$cid]->expire = $this->getRequestTime() - 1;
  140. }
  141. }
  142. /**
  143. * {@inheritdoc}
  144. */
  145. public function invalidateTags(array $tags) {
  146. foreach ($this->cache as $cid => $item) {
  147. if (array_intersect($tags, $item->tags)) {
  148. $this->cache[$cid]->expire = $this->getRequestTime() - 1;
  149. }
  150. }
  151. }
  152. /**
  153. * {@inheritdoc}
  154. */
  155. public function invalidateAll() {
  156. foreach ($this->cache as $cid => $item) {
  157. $this->cache[$cid]->expire = $this->getRequestTime() - 1;
  158. }
  159. }
  160. /**
  161. * {@inheritdoc}
  162. */
  163. public function garbageCollection() {
  164. }
  165. /**
  166. * {@inheritdoc}
  167. */
  168. public function removeBin() {
  169. $this->cache = [];
  170. }
  171. /**
  172. * Wrapper method for REQUEST_TIME constant.
  173. *
  174. * @return int
  175. */
  176. protected function getRequestTime() {
  177. return defined('REQUEST_TIME') ? REQUEST_TIME : (int) $_SERVER['REQUEST_TIME'];
  178. }
  179. /**
  180. * Prevents data stored in memory backends from being serialized.
  181. */
  182. public function __sleep() {
  183. return [];
  184. }
  185. /**
  186. * Reset statically cached variables.
  187. *
  188. * This is only used by tests.
  189. */
  190. public function reset() {
  191. $this->cache = [];
  192. }
  193. }