ProxyAdapter.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. private $defaultLifetime;
  28. /**
  29. * @param string $namespace
  30. * @param int $defaultLifetime
  31. */
  32. public function __construct(CacheItemPoolInterface $pool, $namespace = '', $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. $item->poolHash = $poolHash;
  44. if (null !== $innerItem) {
  45. $item->value = $innerItem->get();
  46. $item->isHit = $innerItem->isHit();
  47. $item->innerItem = $innerItem;
  48. $innerItem->set(null);
  49. }
  50. return $item;
  51. },
  52. null,
  53. CacheItem::class
  54. );
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function getItem($key)
  60. {
  61. $f = $this->createCacheItem;
  62. $item = $this->pool->getItem($this->getId($key));
  63. return $f($key, $item);
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function getItems(array $keys = [])
  69. {
  70. if ($this->namespaceLen) {
  71. foreach ($keys as $i => $key) {
  72. $keys[$i] = $this->getId($key);
  73. }
  74. }
  75. return $this->generateItems($this->pool->getItems($keys));
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function hasItem($key)
  81. {
  82. return $this->pool->hasItem($this->getId($key));
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function clear()
  88. {
  89. return $this->pool->clear();
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function deleteItem($key)
  95. {
  96. return $this->pool->deleteItem($this->getId($key));
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. public function deleteItems(array $keys)
  102. {
  103. if ($this->namespaceLen) {
  104. foreach ($keys as $i => $key) {
  105. $keys[$i] = $this->getId($key);
  106. }
  107. }
  108. return $this->pool->deleteItems($keys);
  109. }
  110. /**
  111. * {@inheritdoc}
  112. */
  113. public function save(CacheItemInterface $item)
  114. {
  115. return $this->doSave($item, __FUNCTION__);
  116. }
  117. /**
  118. * {@inheritdoc}
  119. */
  120. public function saveDeferred(CacheItemInterface $item)
  121. {
  122. return $this->doSave($item, __FUNCTION__);
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function commit()
  128. {
  129. return $this->pool->commit();
  130. }
  131. private function doSave(CacheItemInterface $item, $method)
  132. {
  133. if (!$item instanceof CacheItem) {
  134. return false;
  135. }
  136. $item = (array) $item;
  137. $expiry = $item["\0*\0expiry"];
  138. if (null === $expiry && 0 < $this->defaultLifetime) {
  139. $expiry = time() + $this->defaultLifetime;
  140. }
  141. if ($item["\0*\0poolHash"] === $this->poolHash && $item["\0*\0innerItem"]) {
  142. $innerItem = $item["\0*\0innerItem"];
  143. } elseif ($this->pool instanceof AdapterInterface) {
  144. // this is an optimization specific for AdapterInterface implementations
  145. // so we can save a round-trip to the backend by just creating a new item
  146. $f = $this->createCacheItem;
  147. $innerItem = $f($this->namespace.$item["\0*\0key"], null);
  148. } else {
  149. $innerItem = $this->pool->getItem($this->namespace.$item["\0*\0key"]);
  150. }
  151. $innerItem->set($item["\0*\0value"]);
  152. $innerItem->expiresAt(null !== $expiry ? \DateTime::createFromFormat('U', $expiry) : null);
  153. return $this->pool->$method($innerItem);
  154. }
  155. private function generateItems($items)
  156. {
  157. $f = $this->createCacheItem;
  158. foreach ($items as $key => $item) {
  159. if ($this->namespaceLen) {
  160. $key = substr($key, $this->namespaceLen);
  161. }
  162. yield $key => $f($key, $item);
  163. }
  164. }
  165. private function getId($key)
  166. {
  167. CacheItem::validateKey($key);
  168. return $this->namespace.$key;
  169. }
  170. }