Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/AppConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 52 additions & 0 deletions src/Cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace craft\cloud;

class Cache extends \yii\redis\Cache
{
/**
* gzip compression level, null to disable
*/
public ?int $gzipLevel = 9;

public const GZIP_PREFIX = 'gz:';

public function __construct($config = [])
{
parent::__construct($config + [
'serializer' => [
[$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);
}
}
Loading