1616 */
1717class RedisAdapter extends AbstractAdapter
1818{
19- /**
20- * @var \Redis
21- */
2219 private $ redis ;
2320
24- /**
25- * @param \Redis $redisConnection
26- * @param string $namespace
27- * @param int $defaultLifetime
28- */
2921 public function __construct (\Redis $ redisConnection , $ namespace = '' , $ defaultLifetime = 0 )
3022 {
3123 $ this ->redis = $ redisConnection ;
@@ -39,18 +31,13 @@ public function __construct(\Redis $redisConnection, $namespace = '', $defaultLi
3931 protected function doFetch (array $ ids )
4032 {
4133 $ values = $ this ->redis ->mget ($ ids );
42-
4334 $ index = 0 ;
4435 $ result = [];
4536
4637 foreach ($ ids as $ id ) {
47- $ value = $ values [$ index ++];
48-
49- if (false === $ value ) {
50- continue ;
38+ if (false !== $ value = $ values [$ index ++]) {
39+ $ result [$ id ] = unserialize ($ value );
5140 }
52-
53- $ result [$ id ] = unserialize ($ value );
5441 }
5542
5643 return $ result ;
@@ -67,9 +54,11 @@ protected function doHave($id)
6754 /**
6855 * {@inheritdoc}
6956 */
70- protected function doClear ()
57+ protected function doClear ($ namespace )
7158 {
72- return $ this ->redis ->flushDB ();
59+ $ this ->redis ->eval ("for _,k in ipairs(redis.call('keys',' {$ namespace }*')) do redis.call('del',k) end " );
60+
61+ return true ;
7362 }
7463
7564 /**
@@ -87,21 +76,26 @@ protected function doDelete(array $ids)
8776 */
8877 protected function doSave (array $ values , $ lifetime )
8978 {
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 );
79+ $ failed = array ();
80+
81+ foreach ($ values as $ id => $ v ) {
82+ try {
83+ $ values [$ id ] = serialize ($ v );
84+ } catch (\Exception $ e ) {
85+ $ failed [] = $ id ;
9886 }
87+ }
88+
89+ if (!$ this ->redis ->mSet ($ values )) {
90+ return false ;
91+ }
9992
100- if (false === $ response ) {
101- $ failed [] = $ key ;
93+ if ($ lifetime >= 1 ) {
94+ foreach ($ values as $ id => $ v ) {
95+ $ this ->redis ->expire ($ id , $ lifetime );
10296 }
10397 }
10498
105- return count ( $ failed) > 0 ? $ failed : true ;
99+ return $ failed ;
106100 }
107101}
0 commit comments