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
},
@@ -47,8 +53,11 @@ function ($key, $value, $isHit) {
47
53
*/
48
54
public function getItem ($ key )
49
55
{
56
+ if (!is_string ($ key )) {
57
+ throw new InvalidArgumentException (sprintf ('Cache key must be string, "%s" given ' , is_object ($ key ) ? get_class ($ key ) : gettype ($ key )));
58
+ }
50
59
$ f = $ this ->createCacheItem ;
51
- $ item = $ this ->pool ->getItem ($ key );
60
+ $ item = $ this ->pool ->getItem ($ this -> namespace . $ key );
52
61
if ($ isHit = $ item ->isHit ()) {
53
62
++$ this ->hits ;
54
63
} else {
@@ -63,6 +72,15 @@ public function getItem($key)
63
72
*/
64
73
public function getItems (array $ keys = array ())
65
74
{
75
+ if ($ this ->namespaceLen ) {
76
+ foreach ($ keys as $ i => $ key ) {
77
+ if (!is_string ($ key )) {
78
+ throw new InvalidArgumentException (sprintf ('Cache key must be string, "%s" given ' , is_object ($ key ) ? get_class ($ key ) : gettype ($ key )));
79
+ }
80
+ $ keys [$ i ] = $ this ->namespace .$ key ;
81
+ }
82
+ }
83
+
66
84
return $ this ->generateItems ($ this ->pool ->getItems ($ keys ));
67
85
}
68
86
@@ -71,7 +89,11 @@ public function getItems(array $keys = array())
71
89
*/
72
90
public function hasItem ($ key )
73
91
{
74
- return $ this ->pool ->hasItem ($ key );
92
+ if (!is_string ($ key )) {
93
+ throw new InvalidArgumentException (sprintf ('Cache key must be string, "%s" given ' , is_object ($ key ) ? get_class ($ key ) : gettype ($ key )));
94
+ }
95
+
96
+ return $ this ->pool ->hasItem ($ this ->namespace .$ key );
75
97
}
76
98
77
99
/**
@@ -87,14 +109,27 @@ public function clear()
87
109
*/
88
110
public function deleteItem ($ key )
89
111
{
90
- return $ this ->pool ->deleteItem ($ key );
112
+ if (!is_string ($ key )) {
113
+ throw new InvalidArgumentException (sprintf ('Cache key must be string, "%s" given ' , is_object ($ key ) ? get_class ($ key ) : gettype ($ key )));
114
+ }
115
+
116
+ return $ this ->pool ->deleteItem ($ this ->namespace .$ key );
91
117
}
92
118
93
119
/**
94
120
* {@inheritdoc}
95
121
*/
96
122
public function deleteItems (array $ keys )
97
123
{
124
+ if ($ this ->namespaceLen ) {
125
+ foreach ($ keys as $ i => $ key ) {
126
+ if (!is_string ($ key )) {
127
+ throw new InvalidArgumentException (sprintf ('Cache key must be string, "%s" given ' , is_object ($ key ) ? get_class ($ key ) : gettype ($ key )));
128
+ }
129
+ $ keys [$ i ] = $ this ->namespace .$ key ;
130
+ }
131
+ }
132
+
98
133
return $ this ->pool ->deleteItems ($ keys );
99
134
}
100
135
@@ -129,7 +164,7 @@ private function doSave(CacheItemInterface $item, $method)
129
164
}
130
165
$ item = (array ) $ item ;
131
166
$ expiry = $ item [CacheItem::CAST_PREFIX .'expiry ' ];
132
- $ poolItem = $ this ->pool ->getItem ($ item [CacheItem::CAST_PREFIX .'key ' ]);
167
+ $ poolItem = $ this ->pool ->getItem ($ this -> namespace . $ item [CacheItem::CAST_PREFIX .'key ' ]);
133
168
$ poolItem ->set ($ item [CacheItem::CAST_PREFIX .'value ' ]);
134
169
$ poolItem ->expiresAt (null !== $ expiry ? \DateTime::createFromFormat ('U ' , $ expiry ) : null );
135
170
@@ -146,6 +181,9 @@ private function generateItems($items)
146
181
} else {
147
182
++$ this ->misses ;
148
183
}
184
+ if ($ this ->namespaceLen ) {
185
+ $ key = substr ($ key , $ this ->namespaceLen );
186
+ }
149
187
150
188
yield $ key => $ f ($ key , $ item ->get (), $ isHit );
151
189
}
0 commit comments