55use Exception ;
66use Koded \Caching \Client \CacheClientFactory ;
77use Psr \Cache \{CacheItemInterface , CacheItemPoolInterface };
8- use Psr \ SimpleCache \ CacheInterface ;
8+ use function Koded \ Stdlib \ now ;
99
1010
1111abstract class CacheItemPool implements CacheItemPoolInterface
1212{
13- /** @var CacheInterface */
13+ /** @var Cache */
1414 protected $ client ;
1515
1616 /** @var CacheItemInterface[] */
17- private $ deferred = [];
17+ protected $ deferred = [];
18+
1819
1920 abstract public function __construct (CacheClientFactory $ factory , string $ client );
2021
22+ // @codeCoverageIgnoreStart
2123 public function __destruct ()
2224 {
23- unset($ this ->client );
25+ $ this ->commit ();
26+ }
27+ // @codeCoverageIgnoreEnd
28+
29+ public function commit (): bool
30+ {
31+ foreach ($ this ->deferred as $ key => $ item ) {
32+ if (true === $ this ->save ($ item )) {
33+ unset($ this ->deferred [$ key ]);
34+ }
35+ }
36+
37+ return empty ($ this ->deferred );
38+ }
39+
40+
41+ public function save (CacheItemInterface $ item ): bool
42+ {
43+ /** @var CacheItem $item */
44+ return $ this ->client ->set ($ item ->getKey (), $ item ->get (), $ item ->getExpiresAt ());
2445 }
2546
47+
2648 public function getItems (array $ keys = []): array
2749 {
28- $ collection = [];
50+ $ items = [];
2951 foreach ($ keys as $ key ) {
30- $ collection [$ key ] = $ this ->getItem ($ key );
52+ $ items [$ key ] = $ this ->getItem ($ key );
3153 }
3254
33- return $ collection ;
55+ return $ items ;
3456 }
3557
58+
3659 public function getItem ($ key ): CacheItemInterface
3760 {
38- if (isset ($ this ->deferred [$ key ])) {
39- return $ this ->deferred [$ key ];
40- }
41-
4261 try {
43- return (new class ($ this ->client , $ key ) extends CacheItem
44- {
45- })->set ($ this ->client ->get ($ key ));
62+ $ item = new class ($ key , $ this ->client ->getTtl ()) extends CacheItem {};
63+
64+ if (false === $ this ->client ->has ($ key )) {
65+ if (isset ($ this ->deferred [$ key ])) {
66+ return clone $ this ->deferred [$ key ];
67+ }
68+ return $ item ;
69+ }
70+
71+ (function () {
72+ $ this ->isHit = true ;
73+
74+ return $ this ;
75+ })->call ($ item );
76+
77+ return $ item ->set ($ this ->client ->get ($ key ));
4678
4779 } catch (Exception $ e ) {
48- throw ExtendedCacheException ::from ($ e );
80+ throw CachePoolException ::from ($ e );
4981 }
5082 }
5183
84+
5285 public function hasItem ($ key ): bool
5386 {
5487 try {
55- return $ this ->client ->has ($ key );
88+ return isset ( $ this -> deferred [ $ key ]) || $ this ->client ->has ($ key );
5689 } catch (Exception $ e ) {
57- throw ExtendedCacheException ::from ($ e );
90+ throw CachePoolException ::from ($ e );
5891 }
5992 }
6093
94+
6195 public function clear (): bool
6296 {
63- if ($ this ->client ->clear ()) {
97+ if ($ cleared = $ this ->client ->clear ()) {
6498 $ this ->deferred = [];
65- return true ;
6699 }
67100
68- return false ;
101+ return $ cleared ;
69102 }
70103
104+
71105 public function deleteItems (array $ keys ): bool
72106 {
73- $ deleted = 0 ;
74- foreach ($ keys as $ key ) {
75- $ this ->deleteItem ($ key ) && ++$ deleted ;
107+ try {
108+ return $ this ->client ->deleteMultiple ($ keys );
109+ } catch (Exception $ e ) {
110+ throw CachePoolException::from ($ e );
76111 }
77-
78- return count ($ keys ) === $ deleted ;
79112 }
80113
114+
81115 public function deleteItem ($ key ): bool
82116 {
83117 try {
84- if ($ this ->client ->delete ($ key )) {
118+ if ($ deleted = $ this ->client ->delete ($ key )) {
85119 unset($ this ->deferred [$ key ]);
86- return true ;
87120 }
88121
89- return false ;
122+ return $ deleted ;
90123
91124 } catch (Exception $ e ) {
92- throw ExtendedCacheException ::from ($ e );
125+ throw CachePoolException ::from ($ e );
93126 }
94127 }
95128
96- public function saveDeferred (CacheItemInterface $ item ): bool
97- {
98- $ this ->deferred [$ item ->getKey ()] = $ item ;
99129
100- return true ;
101- }
102-
103- public function commit (): bool
130+ public function saveDeferred (CacheItemInterface $ item ): bool
104131 {
105- foreach ($ this ->deferred as $ key => $ item ) {
106- if (true === $ this ->save ($ item )) {
107- unset($ this ->deferred [$ key ]);
108- }
132+ /** @var CacheItem $item */
133+ if (null !== $ item ->getExpiresAt () && $ item ->getExpiresAt () <= now ()->getTimestamp ()) {
134+ return false ;
109135 }
110136
111- return empty ($ this ->deferred );
112- }
137+ $ this ->deferred [$ item ->getKey ()] = (function () {
138+ $ this ->isHit = true ;
139+ return $ this ;
140+ })->call ($ item );
113141
114- public function save (CacheItemInterface $ item ): bool
115- {
116- /** @var CacheItem $item */
117- $ value = (function () {
118- return $ this ->value ;
119- })->bindTo ($ item , $ item );
120-
121- return $ this ->client ->set ($ item ->getKey (), $ value (), cache_ttl ($ item ->ttl ()));
142+ return true ;
122143 }
123144}
0 commit comments