14
14
use Psr \Cache \CacheItemInterface ;
15
15
use Psr \Cache \CacheItemPoolInterface ;
16
16
use Symfony \Component \Cache \CacheItem ;
17
+ use Symfony \Component \Cache \Exception \InvalidArgumentException ;
17
18
18
19
/**
19
20
* @author Nicolas Grekas <[email protected] >
20
21
*/
21
22
class ProxyAdapter implements CacheItemPoolInterface
22
23
{
23
24
private $ pool ;
25
+ private $ namespace ;
26
+ private $ namespaceLen ;
24
27
private $ createCacheItem ;
25
28
private $ hits = 0 ;
26
29
private $ misses = 0 ;
27
30
28
- public function __construct (CacheItemPoolInterface $ pool )
31
+ public function __construct (CacheItemPoolInterface $ pool, $ defaultLifetime = 0 , $ namespace = '' )
29
32
{
30
33
$ this ->pool = $ pool ;
34
+ $ this ->namespace = $ namespace ;
35
+ $ this ->namespaceLen = strlen ($ namespace );
31
36
$ this ->createCacheItem = \Closure::bind (
32
- function ($ key , $ value , $ isHit ) {
37
+ function ($ key , $ value , $ isHit ) use ( $ defaultLifetime ) {
33
38
$ item = new CacheItem ();
34
39
$ item ->key = $ key ;
35
40
$ item ->value = $ value ;
36
41
$ item ->isHit = $ isHit ;
42
+ $ item ->defaultLifetime = $ defaultLifetime ;
37
43
38
44
return $ item ;
39
45
},
@@ -48,7 +54,7 @@ function ($key, $value, $isHit) {
48
54
public function getItem ($ key )
49
55
{
50
56
$ f = $ this ->createCacheItem ;
51
- $ item = $ this ->pool ->getItem ($ key );
57
+ $ item = $ this ->pool ->getItem ($ this -> getId ( $ key) );
52
58
if ($ isHit = $ item ->isHit ()) {
53
59
++$ this ->hits ;
54
60
} else {
@@ -63,6 +69,12 @@ public function getItem($key)
63
69
*/
64
70
public function getItems (array $ keys = array ())
65
71
{
72
+ if ($ this ->namespaceLen ) {
73
+ foreach ($ keys as $ i => $ key ) {
74
+ $ keys [$ i ] = $ this ->getId ($ key );
75
+ }
76
+ }
77
+
66
78
return $ this ->generateItems ($ this ->pool ->getItems ($ keys ));
67
79
}
68
80
@@ -71,7 +83,7 @@ public function getItems(array $keys = array())
71
83
*/
72
84
public function hasItem ($ key )
73
85
{
74
- return $ this ->pool ->hasItem ($ key );
86
+ return $ this ->pool ->hasItem ($ this -> getId ( $ key) );
75
87
}
76
88
77
89
/**
@@ -87,14 +99,20 @@ public function clear()
87
99
*/
88
100
public function deleteItem ($ key )
89
101
{
90
- return $ this ->pool ->deleteItem ($ key );
102
+ return $ this ->pool ->deleteItem ($ this -> getId ( $ key) );
91
103
}
92
104
93
105
/**
94
106
* {@inheritdoc}
95
107
*/
96
108
public function deleteItems (array $ keys )
97
109
{
110
+ if ($ this ->namespaceLen ) {
111
+ foreach ($ keys as $ i => $ key ) {
112
+ $ keys [$ i ] = $ this ->getId ($ key );
113
+ }
114
+ }
115
+
98
116
return $ this ->pool ->deleteItems ($ keys );
99
117
}
100
118
@@ -129,7 +147,7 @@ private function doSave(CacheItemInterface $item, $method)
129
147
}
130
148
$ item = (array ) $ item ;
131
149
$ expiry = $ item [CacheItem::CAST_PREFIX .'expiry ' ];
132
- $ poolItem = $ this ->pool ->getItem ($ item [CacheItem::CAST_PREFIX .'key ' ]);
150
+ $ poolItem = $ this ->pool ->getItem ($ this -> namespace . $ item [CacheItem::CAST_PREFIX .'key ' ]);
133
151
$ poolItem ->set ($ item [CacheItem::CAST_PREFIX .'value ' ]);
134
152
$ poolItem ->expiresAt (null !== $ expiry ? \DateTime::createFromFormat ('U ' , $ expiry ) : null );
135
153
@@ -146,6 +164,9 @@ private function generateItems($items)
146
164
} else {
147
165
++$ this ->misses ;
148
166
}
167
+ if ($ this ->namespaceLen ) {
168
+ $ key = substr ($ key , $ this ->namespaceLen );
169
+ }
149
170
150
171
yield $ key => $ f ($ key , $ item ->get (), $ isHit );
151
172
}
@@ -170,4 +191,13 @@ public function getMisses()
170
191
{
171
192
return $ this ->misses ;
172
193
}
194
+
195
+ private function getId ($ key )
196
+ {
197
+ if (!is_string ($ key )) {
198
+ throw new InvalidArgumentException (sprintf ('Cache key must be string, "%s" given ' , is_object ($ key ) ? get_class ($ key ) : gettype ($ key )));
199
+ }
200
+
201
+ return $ this ->namespace .$ key ;
202
+ }
173
203
}
0 commit comments