11
11
12
12
namespace Symfony \Component \Cache \Adapter ;
13
13
14
+ use Symfony \Component \Cache \Exception \InvalidArgumentException ;
15
+
14
16
/**
15
17
* @author Aurimas Niekis <aurimas@niekis.lt>
16
18
*/
17
19
class RedisAdapter extends AbstractAdapter
18
20
{
19
- /**
20
- * @var \Redis
21
- */
22
21
private $ redis ;
23
22
24
- /**
25
- * @param \Redis $redisConnection
26
- * @param string $namespace
27
- * @param int $defaultLifetime
28
- */
29
23
public function __construct (\Redis $ redisConnection , $ namespace = '' , $ defaultLifetime = 0 )
30
24
{
31
25
$ this ->redis = $ redisConnection ;
32
26
27
+ if (preg_match ('#[^-+_.A-Za-z0-9]# ' , $ namespace , $ match )) {
28
+ throw new InvalidArgumentException (sprintf ('RedisAdapter namespace contains "%s" but only characters in [-+_.A-Za-z0-9] are allowed. ' , $ match [0 ]));
29
+ }
30
+
33
31
parent ::__construct ($ namespace , $ defaultLifetime );
34
32
}
35
33
@@ -39,18 +37,13 @@ public function __construct(\Redis $redisConnection, $namespace = '', $defaultLi
39
37
protected function doFetch (array $ ids )
40
38
{
41
39
$ values = $ this ->redis ->mget ($ ids );
42
-
43
40
$ index = 0 ;
44
41
$ result = [];
45
42
46
43
foreach ($ ids as $ id ) {
47
- $ value = $ values [$ index ++];
48
-
49
- if (false === $ value ) {
50
- continue ;
44
+ if (false !== $ value = $ values [$ index ++]) {
45
+ $ result [$ id ] = unserialize ($ value );
51
46
}
52
-
53
- $ result [$ id ] = unserialize ($ value );
54
47
}
55
48
56
49
return $ result ;
EDBE
@@ -67,9 +60,15 @@ protected function doHave($id)
67
60
/**
68
61
* {@inheritdoc}
69
62
*/
70
- protected function doClear ()
63
+ protected function doClear ($ namespace )
71
64
{
72
- return $ this ->redis ->flushDB ();
65
+ if (!isset ($ namespace [0 ])) {
66
+ $ this ->redis ->flushDB ();
67
+ } else {
68
+ $ this ->redis ->eval (sprintf ("for _,k in ipairs(redis.call('keys','%s*')) do redis.call('del',k) end " , $ namespace ));
69
+ }
70
+
71
+ return true ;
73
72
}
74
73
75
74
/**
@@ -87,21 +86,26 @@ protected function doDelete(array $ids)
87
86
*/
88
87
protected function doSave (array $ values , $ lifetime )
89
88
{
90
- $ failed = [];
91
- foreach ($ values as $ key => $ value ) {
92
- $ value = serialize ($ value );
93
-
94
- if ($ lifetime < 1 ) {
95
- $ response = $ this ->redis ->set ($ key , $ value );
96
- } else {
97
- $ response = $ this ->redis ->setex ($ key , $ lifetime , $ value );
89
+ $ failed = array ();
90
+
91
+ foreach ($ values as $ id => $ v ) {
92
+ try {
93
+ $ values [$ id ] = serialize ($ v );
94
+ } catch (\Exception $ e ) {
95
+ $ failed [] = $ id ;
98
96
}
97
+ }
98
+
99
+ if (!$ this ->redis ->mSet ($ values )) {
100
+ return false ;
101
+ }
99
102
100
- if (false === $ response ) {
101
- $ failed [] = $ key ;
103
+ if ($ lifetime >= 1 ) {
104
+ foreach ($ values as $ id => $ v ) {
105
+ $ this ->redis ->expire ($ id , $ lifetime );
102
106
}
103
107
}
104
108
105
- return count ( $ failed) > 0 ? $ failed : true ;
109
+ return $ failed ;
106
110
}
107
111
}
0 commit comments