From 2ff02cd333c5d215ddcfe9d0057057161aad660e Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Tue, 19 Jun 2018 22:28:47 +0200 Subject: [PATCH] [Cache] added support for phpredis 4 `compression` and `tcp_keepalive` options --- src/Symfony/Component/Cache/CHANGELOG.md | 1 + .../Component/Cache/Tests/Adapter/PredisAdapterTest.php | 2 ++ src/Symfony/Component/Cache/Traits/RedisTrait.php | 9 +++++++++ 3 files changed, 12 insertions(+) diff --git a/src/Symfony/Component/Cache/CHANGELOG.md b/src/Symfony/Component/Cache/CHANGELOG.md index 2288db8cfebd..98cf028026a6 100644 --- a/src/Symfony/Component/Cache/CHANGELOG.md +++ b/src/Symfony/Component/Cache/CHANGELOG.md @@ -6,6 +6,7 @@ CHANGELOG * added `CacheInterface`, which provides stampede protection via probabilistic early expiration and should become the preferred way to use a cache * added sub-second expiry accuracy for backends that support it + * added support for phpredis 4 `compression` and `tcp_keepalive` options * throw `LogicException` when `CacheItem::tag()` is called on an item coming from a non tag-aware pool * deprecated `CacheItem::getPreviousTags()`, use `CacheItem::getMetadata()` instead * deprecated the `AbstractAdapter::createSystemCache()` method diff --git a/src/Symfony/Component/Cache/Tests/Adapter/PredisAdapterTest.php b/src/Symfony/Component/Cache/Tests/Adapter/PredisAdapterTest.php index c005d64abf1c..b49a15c3bacf 100644 --- a/src/Symfony/Component/Cache/Tests/Adapter/PredisAdapterTest.php +++ b/src/Symfony/Component/Cache/Tests/Adapter/PredisAdapterTest.php @@ -44,6 +44,8 @@ public function testCreateConnection() 'persistent_id' => null, 'read_timeout' => 0, 'retry_interval' => 0, + 'compression' => true, + 'tcp_keepalive' => 0, 'lazy' => false, 'database' => '1', 'password' => null, diff --git a/src/Symfony/Component/Cache/Traits/RedisTrait.php b/src/Symfony/Component/Cache/Traits/RedisTrait.php index b247b115fa2c..d914de926a66 100644 --- a/src/Symfony/Component/Cache/Traits/RedisTrait.php +++ b/src/Symfony/Component/Cache/Traits/RedisTrait.php @@ -34,6 +34,8 @@ trait RedisTrait 'timeout' => 30, 'read_timeout' => 0, 'retry_interval' => 0, + 'compression' => true, + 'tcp_keepalive' => 0, 'lazy' => false, ); private $redis; @@ -142,6 +144,13 @@ public static function createConnection($dsn, array $options = array()) throw new InvalidArgumentException(sprintf('Redis connection failed (%s): %s', $e, $dsn)); } + if (0 < $params['tcp_keepalive'] && \defined('Redis::OPT_TCP_KEEPALIVE')) { + $redis->setOption(\Redis::OPT_TCP_KEEPALIVE, $params['tcp_keepalive']); + } + if ($params['compression'] && \defined('Redis::COMPRESSION_LZF')) { + $redis->setOption(\Redis::OPT_COMPRESSION, \Redis::COMPRESSION_LZF); + } + return true; };