13
13
14
14
use Psr \Cache \CacheItemInterface ;
15
15
use Psr \Cache \CacheItemPoolInterface ;
16
+ use Symfony \Component \Cache \CacheInterface ;
16
17
use Symfony \Component \Cache \CacheItem ;
17
18
use Symfony \Component \Cache \Exception \InvalidArgumentException ;
18
19
use Symfony \Component \Cache \PruneableInterface ;
19
20
use Symfony \Component \Cache \ResettableInterface ;
21
+ use Symfony \Component \Cache \Traits \GetTrait ;
20
22
21
23
/**
22
24
* Chains several adapters together.
26
28
*
27
29
* @author Kévin Dunglas <[email protected] >
28
30
*/
29
- class ChainAdapter implements AdapterInterface, PruneableInterface, ResettableInterface
31
+ class ChainAdapter implements AdapterInterface, CacheInterface, PruneableInterface, ResettableInterface
30
32
{
33
+ use GetTrait;
34
+
31
35
private $ adapters = array ();
32
36
private $ adapterCount ;
33
37
private $ syncItem ;
@@ -61,6 +65,8 @@ function ($sourceItem, $item) use ($defaultLifetime) {
61
65
$ item ->expiry = $ sourceItem ->expiry ;
62
66
$ item ->isHit = $ sourceItem ->isHit ;
63
67
68
+ $ sourceItem ->isTaggable = false ;
69
+
64
70
if (0 < $ sourceItem ->defaultLifetime && $ sourceItem ->defaultLifetime < $ defaultLifetime ) {
65
71
$ defaultLifetime = $ sourceItem ->defaultLifetime ;
66
72
}
@@ -75,6 +81,33 @@ function ($sourceItem, $item) use ($defaultLifetime) {
75
81
);
76
82
}
77
83
84
+ /**
85
+ * {@inheritdoc}
86
+ */
87
+ public function get (string $ key , callable $ callback )
88
+ {
89
+ $ lastItem = null ;
90
+ $ i = 0 ;
91
+ $ wrap = function (CacheItem $ item = null ) use ($ key , $ callback , &$ wrap , &$ i , &$ lastItem ) {
92
+ $ adapter = $ this ->adapters [$ i ];
93
+ if (isset ($ this ->adapters [++$ i ])) {
94
+ $ callback = $ wrap ;
95
+ }
96
+ if ($ adapter instanceof CacheInterface) {
97
+ $ value = $ adapter ->get ($ key , $ callback );
98
+ } else {
99
+ $ value = $ this ->doGet ($ adapter , $ key , $ callback );
100
+ }
101
+ if (null !== $ item ) {
102
+ ($ this ->syncItem )($ lastItem = $ lastItem ?? $ item , $ item );
103
+ }
104
+
105
+ return $ value ;
106
+ };
107
+
108
+ return $ wrap ();
109
+ }
110
+
78
111
/**
79
112
* {@inheritdoc}
80
113
*/
0 commit comments