From 4d0876d9f53d3cd39b87c2ebcb5449922c3409ff Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Fri, 7 Nov 2025 09:42:18 -0500 Subject: [PATCH 1/2] gzip redis cache --- src/AppConfig.php | 1 - src/Cache.php | 52 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 src/Cache.php diff --git a/src/AppConfig.php b/src/AppConfig.php index 83f8de1..1267172 100644 --- a/src/AppConfig.php +++ b/src/AppConfig.php @@ -15,7 +15,6 @@ use craft\helpers\App; use craft\log\MonologTarget; use craft\queue\Queue as CraftQueue; -use yii\redis\Cache; use yii\web\DbSession; class AppConfig diff --git a/src/Cache.php b/src/Cache.php new file mode 100644 index 0000000..608a840 --- /dev/null +++ b/src/Cache.php @@ -0,0 +1,52 @@ + [ + [$this, 'serializeValue'], + [$this, 'unserializeValue'], + ], + ]); + } + + private function serializeValue($value): string + { + $serialized = serialize($value); + + if ($this->gzipLevel === null) { + return $serialized; + } + + $compressed = gzencode($serialized, 9); + + return self::GZIP_PREFIX . $compressed; + } + + private function unserializeValue($value): mixed + { + if (!is_string($value)) { + return $value; + } + + $prefixLength = strlen(self::GZIP_PREFIX); + $isCompressed = strncmp($value, self::GZIP_PREFIX, $prefixLength) === 0; + + $decompressed = $isCompressed + ? gzdecode(substr($value, $prefixLength)) + : $value; + + return unserialize($decompressed); + } +} From a6f84a4861405b487ca543dbb0b1bcbe39fb86b8 Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Fri, 7 Nov 2025 09:47:28 -0500 Subject: [PATCH 2/2] Use gziplevel --- src/Cache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Cache.php b/src/Cache.php index 608a840..52cf6a7 100644 --- a/src/Cache.php +++ b/src/Cache.php @@ -29,7 +29,7 @@ private function serializeValue($value): string return $serialized; } - $compressed = gzencode($serialized, 9); + $compressed = gzencode($serialized, $this->gzipLevel); return self::GZIP_PREFIX . $compressed; }