ProxyAdapter.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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\ProxyTrait;
  17. /**
  18. * @author Nicolas Grekas <p@tchwork.com>
  19. */
  20. class ProxyAdapter implements AdapterInterface, PruneableInterface, ResettableInterface
  21. {
  22. use ProxyTrait;
  23. private $namespace;
  24. private $namespaceLen;
  25. private $createCacheItem;
  26. private $poolHash;
  27. public function __construct(CacheItemPoolInterface $pool, string $namespace = '', int $defaultLifetime = 0)
  28. {
  29. $this->pool = $pool;
  30. $this->poolHash = $poolHash = spl_object_hash($pool);
  31. $this->namespace = '' === $namespace ? '' : CacheItem::validateKey($namespace);
  32. $this->namespaceLen = \strlen($namespace);
  33. $this->createCacheItem = \Closure::bind(
  34. function ($key, $innerItem) use ($defaultLifetime, $poolHash) {
  35. $item = new CacheItem();
  36. $item->key = $key;
  37. $item->value = $innerItem->get();
  38. $item->isHit = $innerItem->isHit();
  39. $item->defaultLifetime = $defaultLifetime;
  40. $item->innerItem = $innerItem;
  41. $item->poolHash = $poolHash;
  42. $innerItem->set(null);
  43. return $item;
  44. },
  45. null,
  46. CacheItem::class
  47. );
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function getItem($key)
  53. {
  54. $f = $this->createCacheItem;
  55. $item = $this->pool->getItem($this->getId($key));
  56. return $f($key, $item);
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function getItems(array $keys = [])
  62. {
  63. if ($this->namespaceLen) {
  64. foreach ($keys as $i => $key) {
  65. $keys[$i] = $this->getId($key);
  66. }
  67. }
  68. return $this->generateItems($this->pool->getItems($keys));
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function hasItem($key)
  74. {
  75. return $this->pool->hasItem($this->getId($key));
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function clear()
  81. {
  82. return $this->pool->clear();
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function deleteItem($key)
  88. {
  89. return $this->pool->deleteItem($this->getId($key));
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function deleteItems(array $keys)
  95. {
  96. if ($this->namespaceLen) {
  97. foreach ($keys as $i => $key) {
  98. $keys[$i] = $this->getId($key);
  99. }
  100. }
  101. return $this->pool->deleteItems($keys);
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function save(CacheItemInterface $item)
  107. {
  108. return $this->doSave($item, __FUNCTION__);
  109. }
  110. /**
  111. * {@inheritdoc}
  112. */
  113. public function saveDeferred(CacheItemInterface $item)
  114. {
  115. return $this->doSave($item, __FUNCTION__);
  116. }
  117. /**
  118. * {@inheritdoc}
  119. */
  120. public function commit()
  121. {
  122. return $this->pool->commit();
  123. }
  124. private function doSave(CacheItemInterface $item, $method)
  125. {
  126. if (!$item instanceof CacheItem) {
  127. return false;
  128. }
  129. $item = (array) $item;
  130. $expiry = $item["\0*\0expiry"];
  131. if (null === $expiry && 0 < $item["\0*\0defaultLifetime"]) {
  132. $expiry = time() + $item["\0*\0defaultLifetime"];
  133. }
  134. $innerItem = $item["\0*\0poolHash"] === $this->poolHash ? $item["\0*\0innerItem"] : $this->pool->getItem($this->namespace.$item["\0*\0key"]);
  135. $innerItem->set($item["\0*\0value"]);
  136. $innerItem->expiresAt(null !== $expiry ? \DateTime::createFromFormat('U', $expiry) : null);
  137. return $this->pool->$method($innerItem);
  138. }
  139. private function generateItems($items)
  140. {
  141. $f = $this->createCacheItem;
  142. foreach ($items as $key => $item) {
  143. if ($this->namespaceLen) {
  144. $key = substr($key, $this->namespaceLen);
  145. }
  146. yield $key => $f($key, $item);
  147. }
  148. }
  149. private function getId($key)
  150. {
  151. CacheItem::validateKey($key);
  152. return $this->namespace.$key;
  153. }
  154. }