ProxyAdapter.php 4.6 KB

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