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..52cf6a7 --- /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, $this->gzipLevel); + + 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); + } +}