@@ -68,9 +68,11 @@ public function __construct(Redis $redis, $prefix = '', $connection = 'default')
68
68
*/
69
69
public function get ($ key )
70
70
{
71
- $ value = $ this ->connection ()->get ($ this ->prefix .$ key );
71
+ $ connection = $ this ->connection ();
72
+
73
+ $ value = $ connection ->get ($ this ->prefix .$ key );
72
74
73
- return ! is_null ($ value ) ? $ this ->unserialize ($ value ) : null ;
75
+ return ! is_null ($ value ) ? $ this ->connectionAwareUnserialize ($ value, $ connection ) : null ;
74
76
}
75
77
76
78
/**
@@ -89,12 +91,14 @@ public function many(array $keys)
89
91
90
92
$ results = [];
91
93
92
- $ values = $ this ->connection ()->mget (array_map (function ($ key ) {
94
+ $ connection = $ this ->connection ();
95
+
96
+ $ values = $ connection ->mget (array_map (function ($ key ) {
93
97
return $ this ->prefix .$ key ;
94
98
}, $ keys ));
95
99
96
100
foreach ($ values as $ index => $ value ) {
97
- $ results [$ keys [$ index ]] = ! is_null ($ value ) ? $ this ->unserialize ($ value ) : null ;
101
+ $ results [$ keys [$ index ]] = ! is_null ($ value ) ? $ this ->connectionAwareUnserialize ($ value, $ connection ) : null ;
98
102
}
99
103
100
104
return $ results ;
@@ -110,8 +114,10 @@ public function many(array $keys)
110
114
*/
111
115
public function put ($ key , $ value , $ seconds )
112
116
{
113
- return (bool ) $ this ->connection ()->setex (
114
- $ this ->prefix .$ key , (int ) max (1 , $ seconds ), $ this ->serialize ($ value )
117
+ $ connection = $ this ->connection ();
118
+
119
+ return (bool ) $ connection ->setex (
120
+ $ this ->prefix .$ key , (int ) max (1 , $ seconds ), $ this ->connectionAwareSerialize ($ value , $ connection )
115
121
);
116
122
}
117
123
@@ -165,10 +171,10 @@ public function putMany(array $values, $seconds)
165
171
*/
166
172
public function add ($ key , $ value , $ seconds )
167
173
{
168
- $ lua = " return redis.call('exists',KEYS[1])<1 and redis.call('setex',KEYS[1],ARGV[2],ARGV[1]) " ;
174
+ $ connection = $ this -> connection () ;
169
175
170
- return (bool ) $ this -> connection () ->eval (
171
- $ lua , 1 , $ this ->prefix .$ key , $ this ->serialize ($ value ), (int ) max (1 , $ seconds )
176
+ return (bool ) $ connection ->eval (
177
+ LuaScripts:: add () , 1 , $ this ->prefix .$ key , $ this ->pack ($ value, $ connection ), (int ) max (1 , $ seconds )
172
178
);
173
179
}
174
180
@@ -205,7 +211,9 @@ public function decrement($key, $value = 1)
205
211
*/
206
212
public function forever ($ key , $ value )
207
213
{
208
- return (bool ) $ this ->connection ()->set ($ this ->prefix .$ key , $ this ->serialize ($ value ));
214
+ $ connection = $ this ->connection ();
215
+
216
+ return (bool ) $ connection ->set ($ this ->prefix .$ key , $ this ->connectionAwareSerialize ($ value , $ connection ));
209
217
}
210
218
211
219
/**
@@ -414,6 +422,28 @@ public function setPrefix($prefix)
414
422
$ this ->prefix = $ prefix ;
415
423
}
416
424
425
+ /**
426
+ * Prepare a value to be used with the Redis cache store when used by eval scripts.
427
+ *
428
+ * @param mixed $value
429
+ * @param \Illuminate\Redis\Connections\Connection $connection
430
+ * @return mixed
431
+ */
432
+ protected function pack ($ value , $ connection )
433
+ {
434
+ if ($ connection instanceof PhpRedisConnection) {
435
+ if ($ connection ->serialized ()) {
436
+ return $ connection ->pack ([$ value ])[0 ];
437
+ }
438
+
439
+ if ($ connection ->compressed ()) {
440
+ return $ connection ->pack ([$ this ->serialize ($ value )])[0 ];
441
+ }
442
+ }
443
+
444
+ return $ this ->serialize ($ value );
445
+ }
446
+
417
447
/**
418
448
* Serialize the value.
419
449
*
@@ -435,4 +465,36 @@ protected function unserialize($value)
435
465
{
436
466
return is_numeric ($ value ) ? $ value : unserialize ($ value );
437
467
}
468
+
469
+ /**
470
+ * Handle connection specific considerations when a value needs to be serialized.
471
+ *
472
+ * @param mixed $value
473
+ * @param \Illuminate\Redis\Connections\Connection $connection
474
+ * @return mixed
475
+ */
476
+ protected function connectionAwareSerialize ($ value , $ connection )
477
+ {
478
+ if ($ connection instanceof PhpRedisConnection && $ connection ->serialized ()) {
479
+ return $ value ;
480
+ }
481
+
482
+ return $ this ->serialize ($ value );
483
+ }
484
+
485
+ /**
486
+ * Handle connection specific considerations when a value needs to be unserialized.
487
+ *
488
+ * @param mixed $value
489
+ * @param \Illuminate\Redis\Connections\Connection $connection
490
+ * @return mixed
491
+ */
492
+ protected function connectionAwareUnserialize ($ value , $ connection )
493
+ {
494
+ if ($ connection instanceof PhpRedisConnection && $ connection ->serialized ()) {
495
+ return $ value ;
496
+ }
497
+
498
+ return $ this ->unserialize ($ value );
499
+ }
438
500
}
0 commit comments