ProxyAdapter.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 = 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. * @param string $prefix
  136. *
  137. * @return bool
  138. */
  139. public function clear(/*string $prefix = ''*/)
  140. {
  141. $prefix = 0 < \func_num_args() ? (string) func_get_arg(0) : '';
  142. if ($this->pool instanceof AdapterInterface) {
  143. return $this->pool->clear($this->namespace.$prefix);
  144. }
  145. return $this->pool->clear();
  146. }
  147. /**
  148. * {@inheritdoc}
  149. *
  150. * @return bool
  151. */
  152. public function deleteItem($key)
  153. {
  154. return $this->pool->deleteItem($this->getId($key));
  155. }
  156. /**
  157. * {@inheritdoc}
  158. *
  159. * @return bool
  160. */
  161. public function deleteItems(array $keys)
  162. {
  163. if ($this->namespaceLen) {
  164. foreach ($keys as $i => $key) {
  165. $keys[$i] = $this->getId($key);
  166. }
  167. }
  168. return $this->pool->deleteItems($keys);
  169. }
  170. /**
  171. * {@inheritdoc}
  172. *
  173. * @return bool
  174. */
  175. public function save(CacheItemInterface $item)
  176. {
  177. return $this->doSave($item, __FUNCTION__);
  178. }
  179. /**
  180. * {@inheritdoc}
  181. *
  182. * @return bool
  183. */
  184. public function saveDeferred(CacheItemInterface $item)
  185. {
  186. return $this->doSave($item, __FUNCTION__);
  187. }
  188. /**
  189. * {@inheritdoc}
  190. *
  191. * @return bool
  192. */
  193. public function commit()
  194. {
  195. return $this->pool->commit();
  196. }
  197. private function doSave(CacheItemInterface $item, string $method)
  198. {
  199. if (!$item instanceof CacheItem) {
  200. return false;
  201. }
  202. $item = (array) $item;
  203. if (null === $item["\0*\0expiry"] && 0 < $this->defaultLifetime) {
  204. $item["\0*\0expiry"] = microtime(true) + $this->defaultLifetime;
  205. }
  206. if ($item["\0*\0poolHash"] === $this->poolHash && $item["\0*\0innerItem"]) {
  207. $innerItem = $item["\0*\0innerItem"];
  208. } elseif ($this->pool instanceof AdapterInterface) {
  209. // this is an optimization specific for AdapterInterface implementations
  210. // so we can save a round-trip to the backend by just creating a new item
  211. $f = $this->createCacheItem;
  212. $innerItem = $f($this->namespace.$item["\0*\0key"], null);
  213. } else {
  214. $innerItem = $this->pool->getItem($this->namespace.$item["\0*\0key"]);
  215. }
  216. ($this->setInnerItem)($innerItem, $item);
  217. return $this->pool->$method($innerItem);
  218. }
  219. private function generateItems(iterable $items)
  220. {
  221. $f = $this->createCacheItem;
  222. foreach ($items as $key => $item) {
  223. if ($this->namespaceLen) {
  224. $key = substr($key, $this->namespaceLen);
  225. }
  226. yield $key => $f($key, $item);
  227. }
  228. }
  229. private function getId($key): string
  230. {
  231. CacheItem::validateKey($key);
  232. return $this->namespace.$key;
  233. }
  234. }