diff --git a/src/Illuminate/Cache/RedisStore.php b/src/Illuminate/Cache/RedisStore.php index d84c9a505965..ebdd0febbd82 100755 --- a/src/Illuminate/Cache/RedisStore.php +++ b/src/Illuminate/Cache/RedisStore.php @@ -432,6 +432,10 @@ public function setPrefix($prefix) protected function pack($value, $connection) { if ($connection instanceof PhpRedisConnection) { + if ($this->shouldBeStoredWithoutSerialization($value)) { + return $value; + } + if ($connection->serialized()) { return $connection->pack([$value])[0]; } @@ -452,7 +456,18 @@ protected function pack($value, $connection) */ protected function serialize($value) { - return is_numeric($value) && ! in_array($value, [INF, -INF]) && ! is_nan($value) ? $value : serialize($value); + return $this->shouldBeStoredWithoutSerialization($value) ? $value : serialize($value); + } + + /** + * Determine if the given value should be stored as plain value. + * + * @param mixed $value + * @return bool + */ + protected function shouldBeStoredWithoutSerialization($value): bool + { + return is_numeric($value) && ! in_array($value, [INF, -INF]) && ! is_nan($value); } /** diff --git a/tests/Integration/Cache/RedisStoreTest.php b/tests/Integration/Cache/RedisStoreTest.php index 8df8b4ee3f9c..25ee5ccbe721 100644 --- a/tests/Integration/Cache/RedisStoreTest.php +++ b/tests/Integration/Cache/RedisStoreTest.php @@ -249,4 +249,20 @@ public function testPutManyCallsPutWhenClustered() 'fizz' => 'buz', ], 10); } + + public function testIncrementWithSerializationEnabled() + { + /** @var \Illuminate\Cache\RedisStore $store */ + $store = Cache::store('redis'); + /** @var \Redis $client */ + $client = $store->connection()->client(); + $client->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP); + + $store->flush(); + $store->add('foo', 1, 10); + $this->assertEquals(1, $store->get('foo')); + + $store->increment('foo'); + $this->assertEquals(2, $store->get('foo')); + } }