PhpBackend.php 6.4 KB

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