MemoryBackend.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 cache_get() or cache_get_multiple().
  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. foreach ($cids as $cid) {
  138. $this->cache[$cid]->expire = $this->getRequestTime() - 1;
  139. }
  140. }
  141. /**
  142. * {@inheritdoc}
  143. */
  144. public function invalidateTags(array $tags) {
  145. foreach ($this->cache as $cid => $item) {
  146. if (array_intersect($tags, $item->tags)) {
  147. $this->cache[$cid]->expire = $this->getRequestTime() - 1;
  148. }
  149. }
  150. }
  151. /**
  152. * {@inheritdoc}
  153. */
  154. public function invalidateAll() {
  155. foreach ($this->cache as $cid => $item) {
  156. $this->cache[$cid]->expire = $this->getRequestTime() - 1;
  157. }
  158. }
  159. /**
  160. * {@inheritdoc}
  161. */
  162. public function garbageCollection() {
  163. }
  164. /**
  165. * {@inheritdoc}
  166. */
  167. public function removeBin() {
  168. $this->cache = [];
  169. }
  170. /**
  171. * Wrapper method for REQUEST_TIME constant.
  172. *
  173. * @return int
  174. */
  175. protected function getRequestTime() {
  176. return defined('REQUEST_TIME') ? REQUEST_TIME : (int) $_SERVER['REQUEST_TIME'];
  177. }
  178. /**
  179. * Prevents data stored in memory backends from being serialized.
  180. */
  181. public function __sleep() {
  182. return [];
  183. }
  184. /**
  185. * Reset statically cached variables.
  186. *
  187. * This is only used by tests.
  188. */
  189. public function reset() {
  190. $this->cache = [];
  191. }
  192. }