ProxyAdapter.php 8.1 KB

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