66use Psr \Cache \{CacheItemInterface , CacheItemPoolInterface };
77use function Koded \Stdlib \now ;
88
9-
109abstract class CacheItemPool implements CacheItemPoolInterface
1110{
12- /** @var Cache */
13- protected $ client ;
11+ protected Cache $ client ;
1412
1513 /** @var CacheItemInterface[] */
16- private $ deferred = [];
17-
14+ private array $ deferred = [];
1815
1916 abstract public function __construct (string $ client , array $ parameters );
2017
@@ -32,34 +29,28 @@ public function commit(): bool
3229 unset($ this ->deferred [$ key ]);
3330 }
3431 }
35-
3632 return empty ($ this ->deferred );
3733 }
3834
39-
4035 public function save (CacheItemInterface $ item ): bool
4136 {
4237 /** @var CacheItem $item */
4338 return $ this ->client ->set ($ item ->getKey (), $ item ->get (), $ item ->getExpiresAt ());
4439 }
4540
46-
4741 public function getItems (array $ keys = []): array
4842 {
4943 $ items = [];
5044 foreach ($ keys as $ key ) {
5145 $ items [$ key ] = $ this ->getItem ($ key );
5246 }
53-
5447 return $ items ;
5548 }
5649
57-
5850 public function getItem ($ key ): CacheItemInterface
5951 {
6052 try {
6153 $ item = new class ($ key , $ this ->client ->getTtl ()) extends CacheItem {};
62-
6354 if (false === $ this ->client ->has ($ key )) {
6455 if (isset ($ this ->deferred [$ key ])) {
6556 return clone $ this ->deferred [$ key ];
@@ -79,7 +70,6 @@ public function getItem($key): CacheItemInterface
7970 }
8071 }
8172
82-
8373 public function hasItem ($ key ): bool
8474 {
8575 try {
@@ -89,17 +79,14 @@ public function hasItem($key): bool
8979 }
9080 }
9181
92-
9382 public function clear (): bool
9483 {
9584 if ($ cleared = $ this ->client ->clear ()) {
9685 $ this ->deferred = [];
9786 }
98-
9987 return $ cleared ;
10088 }
10189
102-
10390 public function deleteItems (array $ keys ): bool
10491 {
10592 try {
@@ -109,22 +96,18 @@ public function deleteItems(array $keys): bool
10996 }
11097 }
11198
112-
11399 public function deleteItem ($ key ): bool
114100 {
115101 try {
116102 if ($ deleted = $ this ->client ->delete ($ key )) {
117103 unset($ this ->deferred [$ key ]);
118104 }
119-
120105 return $ deleted ;
121-
122106 } catch (Exception $ e ) {
123107 throw CachePoolException::from ($ e );
124108 }
125109 }
126110
127-
128111 public function saveDeferred (CacheItemInterface $ item ): bool
129112 {
130113 /** @var CacheItem $item */
@@ -139,4 +122,16 @@ public function saveDeferred(CacheItemInterface $item): bool
139122
140123 return true ;
141124 }
142- }
125+
126+ /**
127+ * Returns the instance of the underlying cache client.
128+ *
129+ * This method is not part of the PSR-6.
130+ *
131+ * @return Cache
132+ */
133+ public function client (): Cache
134+ {
135+ return $ this ->client ;
136+ }
137+ }
0 commit comments