ProxyAdapter.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Cache\Adapter;
  11. use Psr\Cache\CacheItemInterface;
  12. use Psr\Cache\CacheItemPoolInterface;
  13. use Symfony\Component\Cache\CacheItem;
  14. use Symfony\Component\Cache\PruneableInterface;
  15. use Symfony\Component\Cache\ResettableInterface;
  16. use Symfony\Component\Cache\Traits\ContractsTrait;
  17. use Symfony\Component\Cache\Traits\ProxyTrait;
  18. use Symfony\Contracts\Cache\CacheInterface;
  19. /**
  20. * @author Nicolas Grekas <p@tchwork.com>
  21. */
  22. class ProxyAdapter implements AdapterInterface, CacheInterface, PruneableInterface, ResettableInterface
  23. {
  24. use ContractsTrait;
  25. use ProxyTrait;
  26. private $namespace;
  27. private $namespaceLen;
  28. private $createCacheItem;
  29. private $setInnerItem;
  30. private $poolHash;
  31. private $defaultLifetime;
  32. public function __construct(CacheItemPoolInterface $pool, string $namespace = '', int $defaultLifetime = 0)
  33. {
  34. $this->pool = $pool;
  35. $this->poolHash = $poolHash = spl_object_hash($pool);
  36. $this->namespace = '' === $namespace ? '' : CacheItem::validateKey($namespace);
  37. $this->namespaceLen = \strlen($namespace);
  38. $this->defaultLifetime = $defaultLifetime;
  39. $this->createCacheItem = \Closure::bind(
  40. static function ($key, $innerItem) use ($poolHash) {
  41. $item = new CacheItem();
  42. $item->key = $key;
  43. if (null === $innerItem) {
  44. return $item;
  45. }
  46. $item->value = $v = $innerItem->get();
  47. $item->isHit = $innerItem->isHit();
  48. $item->innerItem = $innerItem;
  49. $item->poolHash = $poolHash;
  50. // Detect wrapped values that encode for their expiry and creation duration
  51. // For compactness, these values are packed in the key of an array using
  52. // magic numbers in the form 9D-..-..-..-..-00-..-..-..-5F
  53. if (\is_array($v) && 1 === \count($v) && 10 === \strlen($k = (string) key($v)) && "\x9D" === $k[0] && "\0" === $k[5] && "\x5F" === $k[9]) {
  54. $item->value = $v[$k];
  55. $v = unpack('Ve/Nc', substr($k, 1, -1));
  56. $item->metadata[CacheItem::METADATA_EXPIRY] = $v['e'] + CacheItem::METADATA_EXPIRY_OFFSET;
  57. $item->metadata[CacheItem::METADATA_CTIME] = $v['c'];
  58. } elseif ($innerItem instanceof CacheItem) {
  59. $item->metadata = $innerItem->metadata;
  60. }
  61. $innerItem->set(null);
  62. return $item;
  63. },
  64. null,
  65. CacheItem::class
  66. );
  67. $this->setInnerItem = \Closure::bind(
  68. /**
  69. * @param array $item A CacheItem cast to (array); accessing protected properties requires adding the "\0*\0" PHP prefix
  70. */
  71. static function (CacheItemInterface $innerItem, array $item) {
  72. // Tags are stored separately, no need to account for them when considering this item's newly set metadata
  73. if (isset(($metadata = $item["\0*\0newMetadata"])[CacheItem::METADATA_TAGS])) {
  74. unset($metadata[CacheItem::METADATA_TAGS]);
  75. }
  76. if ($metadata) {
  77. // For compactness, expiry and creation duration are packed in the key of an array, using magic numbers as separators
  78. $item["\0*\0value"] = ["\x9D".pack('VN', (int) (0.1 + $metadata[self::METADATA_EXPIRY] - self::METADATA_EXPIRY_OFFSET), $metadata[self::METADATA_CTIME])."\x5F" => $item["\0*\0value"]];
  79. }
  80. $innerItem->set($item["\0*\0value"]);
  81. $innerItem->expiresAt(null !== $item["\0*\0expiry"] ? \DateTime::createFromFormat('U.u', sprintf('%.6F', 0 === $item["\0*\0expiry"] ? \PHP_INT_MAX : $item["\0*\0expiry"])) : null);
  82. },
  83. null,
  84. CacheItem::class
  85. );
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function get(string $key, callable $callback, float $beta = null, array &$metadata = null)
  91. {
  92. if (!$this->pool instanceof CacheInterface) {
  93. return $this->doGet($this, $key, $callback, $beta, $metadata);
  94. }
  95. return $this->pool->get($this->getId($key), function ($innerItem, bool &$save) use ($key, $callback) {
  96. $item = ($this->createCacheItem)($key, $innerItem);
  97. $item->set($value = $callback($item, $save));
  98. ($this->setInnerItem)($innerItem, (array) $item);
  99. return $value;
  100. }, $beta, $metadata);
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function getItem($key)
  106. {
  107. $f = $this->createCacheItem;
  108. $item = $this->pool->getItem($this->getId($key));
  109. return $f($key, $item);
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. public function getItems(array $keys = [])
  115. {
  116. if ($this->namespaceLen) {
  117. foreach ($keys as $i => $key) {
  118. $keys[$i] = $this->getId($key);
  119. }
  120. }
  121. return $this->generateItems($this->pool->getItems($keys));
  122. }
  123. /**
  124. * {@inheritdoc}
  125. *
  126. * @return bool
  127. */
  128. public function hasItem($key)
  129. {
  130. return $this->pool->hasItem($this->getId($key));
  131. }
  132. /**
  133. * {@inheritdoc}
  134. *
  135. * @return bool
  136. */
  137. public function clear(string $prefix = '')
  138. {
  139. if ($this->pool instanceof AdapterInterface) {
  140. return $this->pool->clear($this->namespace.$prefix);
  141. }
  142. return $this->pool->clear();
  143. }
  144. /**
  145. * {@inheritdoc}
  146. *
  147. * @return bool
  148. */
  149. public function deleteItem($key)
  150. {
  151. return $this->pool->deleteItem($this->getId($key));
  152. }
  153. /**
  154. * {@inheritdoc}
  155. *
  156. * @return bool
  157. */
  158. public function deleteItems(array $keys)
  159. {
  160. if ($this->namespaceLen) {
  161. foreach ($keys as $i => $key) {
  162. $keys[$i] = $this->getId($key);
  163. }
  164. }
  165. return $this->pool->deleteItems($keys);
  166. }
  167. /**
  168. * {@inheritdoc}
  169. *
  170. * @return bool
  171. */
  172. public function save(CacheItemInterface $item)
  173. {
  174. return $this->doSave($item, __FUNCTION__);
  175. }
  176. /**
  177. * {@inheritdoc}
  178. *
  179. * @return bool
  180. */
  181. public function saveDeferred(CacheItemInterface $item)
  182. {
  183. return $this->doSave($item, __FUNCTION__);
  184. }
  185. /**
  186. * {@inheritdoc}
  187. *
  188. * @return bool
  189. */
  190. public function commit()
  191. {
  192. return $this->pool->commit();
  193. }
  194. private function doSave(CacheItemInterface $item, string $method)
  195. {
  196. if (!$item instanceof CacheItem) {
  197. return false;
  198. }
  199. $item = (array) $item;
  200. if (null === $item["\0*\0expiry"] && 0 < $this->defaultLifetime) {
  201. $item["\0*\0expiry"] = microtime(true) + $this->defaultLifetime;
  202. }
  203. if ($item["\0*\0poolHash"] === $this->poolHash && $item["\0*\0innerItem"]) {
  204. $innerItem = $item["\0*\0innerItem"];
  205. } elseif ($this->pool instanceof AdapterInterface) {
  206. // this is an optimization specific for AdapterInterface implementations
  207. // so we can save a round-trip to the backend by just creating a new item
  208. $f = $this->createCacheItem;
  209. $innerItem = $f($this->namespace.$item["\0*\0key"], null);
  210. } else {
  211. $innerItem = $this->pool->getItem($this->namespace.$item["\0*\0key"]);
  212. }
  213. ($this->setInnerItem)($innerItem, $item);
  214. return $this->pool->$method($innerItem);
  215. }
  216. private function generateItems(iterable $items)
  217. {
  218. $f = $this->createCacheItem;
  219. foreach ($items as $key => $item) {
  220. if ($this->namespaceLen) {
  221. $key = substr($key, $this->namespaceLen);
  222. }
  223. yield $key => $f($key, $item);
  224. }
  225. }
  226. private function getId($key): string
  227. {
  228. CacheItem::validateKey($key);
  229. return $this->namespace.$key;
  230. }
  231. }