PageRenderTime 246ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/core/lib/Drupal/Core/Cache/PhpBackend.php

https://gitlab.com/reasonat/test8
PHP | 271 lines | 114 code | 33 blank | 124 comment | 11 complexity | 526a17158b47839eb48b1edf29e5614b MD5 | raw file
  1. <?php
  2. namespace Drupal\Core\Cache;
  3. use Drupal\Core\PhpStorage\PhpStorageFactory;
  4. use Drupal\Component\Utility\Crypt;
  5. /**
  6. * Defines a PHP cache implementation.
  7. *
  8. * Stores cache items in a PHP file using a storage that implements
  9. * Drupal\Component\PhpStorage\PhpStorageInterface.
  10. *
  11. * This is fast because of PHP's opcode caching mechanism. Once a file's
  12. * content is stored in PHP's opcode cache, including it doesn't require
  13. * reading the contents from a filesystem. Instead, PHP will use the already
  14. * compiled opcodes stored in memory.
  15. *
  16. * @ingroup cache
  17. */
  18. class PhpBackend implements CacheBackendInterface {
  19. /**
  20. * @var string
  21. */
  22. protected $bin;
  23. /**
  24. * Array to store cache objects.
  25. */
  26. protected $cache = array();
  27. /**
  28. * The cache tags checksum provider.
  29. *
  30. * @var \Drupal\Core\Cache\CacheTagsChecksumInterface
  31. */
  32. protected $checksumProvider;
  33. /**
  34. * Constructs a PhpBackend object.
  35. *
  36. * @param string $bin
  37. * The cache bin for which the object is created.
  38. * @param \Drupal\Core\Cache\CacheTagsChecksumInterface $checksum_provider
  39. * The cache tags checksum provider.
  40. */
  41. public function __construct($bin, CacheTagsChecksumInterface $checksum_provider) {
  42. $this->bin = 'cache_' . $bin;
  43. $this->checksumProvider = $checksum_provider;
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function get($cid, $allow_invalid = FALSE) {
  49. return $this->getByHash($this->normalizeCid($cid), $allow_invalid);
  50. }
  51. /**
  52. * Fetch a cache item using a hashed cache ID.
  53. *
  54. * @param string $cidhash
  55. * The hashed version of the original cache ID after being normalized.
  56. * @param bool $allow_invalid
  57. * (optional) If TRUE, a cache item may be returned even if it is expired or
  58. * has been invalidated.
  59. *
  60. * @return bool|mixed
  61. */
  62. protected function getByHash($cidhash, $allow_invalid = FALSE) {
  63. if ($file = $this->storage()->getFullPath($cidhash)) {
  64. $cache = @include $file;
  65. }
  66. if (isset($cache)) {
  67. return $this->prepareItem($cache, $allow_invalid);
  68. }
  69. return FALSE;
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function setMultiple(array $items) {
  75. foreach ($items as $cid => $item) {
  76. $this->set($cid, $item['data'], isset($item['expire']) ? $item['expire'] : CacheBackendInterface::CACHE_PERMANENT, isset($item['tags']) ? $item['tags'] : array());
  77. }
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function getMultiple(&$cids, $allow_invalid = FALSE) {
  83. $ret = array();
  84. foreach ($cids as $cid) {
  85. if ($item = $this->get($cid, $allow_invalid)) {
  86. $ret[$item->cid] = $item;
  87. }
  88. }
  89. $cids = array_diff($cids, array_keys($ret));
  90. return $ret;
  91. }
  92. /**
  93. * Prepares a cached item.
  94. *
  95. * Checks that items are either permanent or did not expire, and returns data
  96. * as appropriate.
  97. *
  98. * @param object $cache
  99. * An item loaded from cache_get() or cache_get_multiple().
  100. * @param bool $allow_invalid
  101. * If FALSE, the method returns FALSE if the cache item is not valid.
  102. *
  103. * @return mixed
  104. * The item with data as appropriate or FALSE if there is no
  105. * valid item to load.
  106. */
  107. protected function prepareItem($cache, $allow_invalid) {
  108. if (!isset($cache->data)) {
  109. return FALSE;
  110. }
  111. // Check expire time.
  112. $cache->valid = $cache->expire == Cache::PERMANENT || $cache->expire >= REQUEST_TIME;
  113. // Check if invalidateTags() has been called with any of the item's tags.
  114. if (!$this->checksumProvider->isValid($cache->checksum, $cache->tags)) {
  115. $cache->valid = FALSE;
  116. }
  117. if (!$allow_invalid && !$cache->valid) {
  118. return FALSE;
  119. }
  120. return $cache;
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = array()) {
  126. assert('\Drupal\Component\Assertion\Inspector::assertAllStrings($tags)', 'Cache Tags must be strings.');
  127. $item = (object) array(
  128. 'cid' => $cid,
  129. 'data' => $data,
  130. 'created' => round(microtime(TRUE), 3),
  131. 'expire' => $expire,
  132. 'tags' => array_unique($tags),
  133. 'checksum' => $this->checksumProvider->getCurrentChecksum($tags),
  134. );
  135. $this->writeItem($this->normalizeCid($cid), $item);
  136. }
  137. /**
  138. * {@inheritdoc}
  139. */
  140. public function delete($cid) {
  141. $this->storage()->delete($this->normalizeCid($cid));
  142. }
  143. /**
  144. * {@inheritdoc}
  145. */
  146. public function deleteMultiple(array $cids) {
  147. foreach ($cids as $cid) {
  148. $this->delete($cid);
  149. }
  150. }
  151. /**
  152. * {@inheritdoc}
  153. */
  154. public function deleteAll() {
  155. $this->storage()->deleteAll();
  156. }
  157. /**
  158. * {@inheritdoc}
  159. */
  160. public function invalidate($cid) {
  161. $this->invalidatebyHash($this->normalizeCid($cid));
  162. }
  163. /**
  164. * Invalidate one cache item.
  165. *
  166. * @param string $cidhash
  167. * The hashed version of the original cache ID after being normalized.
  168. */
  169. protected function invalidatebyHash($cidhash) {
  170. if ($item = $this->getByHash($cidhash)) {
  171. $item->expire = REQUEST_TIME - 1;
  172. $this->writeItem($cidhash, $item);
  173. }
  174. }
  175. /**
  176. * {@inheritdoc}
  177. */
  178. public function invalidateMultiple(array $cids) {
  179. foreach ($cids as $cid) {
  180. $this->invalidate($cid);
  181. }
  182. }
  183. /**
  184. * {@inheritdoc}
  185. */
  186. public function invalidateAll() {
  187. foreach ($this->storage()->listAll() as $cidhash) {
  188. $this->invalidatebyHash($cidhash);
  189. }
  190. }
  191. /**
  192. * {@inheritdoc}
  193. */
  194. public function garbageCollection() {
  195. }
  196. /**
  197. * {@inheritdoc}
  198. */
  199. public function removeBin() {
  200. $this->cache = array();
  201. $this->storage()->deleteAll();
  202. }
  203. /**
  204. * Writes a cache item to PhpStorage.
  205. *
  206. * @param string $cidhash
  207. * The hashed version of the original cache ID after being normalized.
  208. * @param \stdClass $item
  209. * The cache item to store.
  210. */
  211. protected function writeItem($cidhash, \stdClass $item) {
  212. $content = '<?php return unserialize(' . var_export(serialize($item), TRUE) . ');';
  213. $this->storage()->save($cidhash, $content);
  214. }
  215. /**
  216. * Gets the PHP code storage object to use.
  217. *
  218. * @return \Drupal\Component\PhpStorage\PhpStorageInterface
  219. */
  220. protected function storage() {
  221. if (!isset($this->storage)) {
  222. $this->storage = PhpStorageFactory::get($this->bin);
  223. }
  224. return $this->storage;
  225. }
  226. /**
  227. * Ensures a normalized cache ID.
  228. *
  229. * @param string $cid
  230. * The passed in cache ID.
  231. *
  232. * @return string
  233. * A normalized cache ID.
  234. */
  235. protected function normalizeCid($cid) {
  236. return Crypt::hashBase64($cid);
  237. }
  238. }