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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ installed using Composer:
composer require wieni/wmpage_cache_redis
```

To enable this cache storage, change the `wmpage_cache.storage` container parameter:
To enable this cache storage, change the following container parameters:
```yaml
parameters:
wmpage_cache.storage: wmpage_cache.storage.redis
wmpage_cache.invalidator: wmpage_cache_redis.checksum
wmpage_cache.checksum: wmpage_cache_redis.checksum

wmpage_cache.redis.prefix: 'wmpage_cache:'
```
Expand Down
78 changes: 78 additions & 0 deletions src/RedisCacheTagsChecksum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Drupal\wmpage_cache_redis;

use Drupal\Core\Cache\CacheTagsChecksumInterface;
use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\wmpage_cache\InvalidatorInterface;

class RedisCacheTagsChecksum implements CacheTagsChecksumInterface, CacheTagsInvalidatorInterface, InvalidatorInterface
{
/** @var RedisClientFactory */
protected $redisClientFactory;
/** @var CacheTagsChecksumInterface|CacheTagsInvalidatorInterface|null */
protected $redisCacheTagsChecksum;

public function __construct(RedisClientFactory $redisClientFactory)
{
$this->redisClientFactory = $redisClientFactory;
}

public function getCurrentChecksum(array $tags)
{
if ($decorated = $this->decorated()) {
return $decorated->getCurrentChecksum($tags);
}

return 'noop';
}

public function isValid($checksum, array $tags)
{
if ($decorated = $this->decorated()) {
return $decorated->isValid($checksum, $tags);
}

return false;
}

public function reset()
{
if ($decorated = $this->decorated()) {
$decorated->reset();
}
}

public function invalidateCacheTags(array $tags): void
{
// noop
// We use Drupal's cache tag invalidation system to invalidate the cache
}

public function invalidateTags(array $tags)
{
if ($decorated = $this->decorated()) {
$decorated->invalidateTags($tags);
}
}

/** @return CacheTagsChecksumInterface|CacheTagsInvalidatorInterface|null */
protected function decorated()
{
if (isset($this->redisCacheTagsChecksum)) {
return $this->redisCacheTagsChecksum ?: null;
}

$decorated = false;
try {
$client = $this->redisClientFactory::getClient();
if ($client instanceof \Redis) {
$decorated = new RedisCacheTagsChecksum($this->redisClientFactory);
}
} catch (\Exception $e) {
watchdog_exception('wmcontroller.redis', $e);
}

return $this->redisCacheTagsChecksum = $decorated;
}
}
13 changes: 13 additions & 0 deletions src/RedisCacheTagsChecksumClientFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Drupal\wmpage_cache_redis;

/**
* We extend it because we want a different client
* The ClientFactory calls static::$client so we cannot re-use the same
* factory. This sucks.
*/
class RedisCacheTagsChecksumClientFactory extends RedisClientFactory
{
protected static $client;
}
33 changes: 31 additions & 2 deletions src/Storage/RedisStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\wmpage_cache_redis\Storage;

use Drupal\Core\Cache\CacheTagsChecksumInterface;
use Drupal\wmpage_cache\Cache;
use Drupal\wmpage_cache\Exception\NoSuchCacheEntryException;
use Drupal\wmpage_cache\CacheSerializerInterface;
Expand All @@ -13,13 +14,16 @@ class RedisStorage implements StorageInterface
{
/** @var \Redis */
protected $redis;
/** @var CacheTagsChecksumInterface */
protected $checksumProvider;
/** @var CacheSerializerInterface */
protected $serializer;
/** @var string */
protected $prefix;

public function __construct(
RedisClientFactory $clientFactory,
CacheTagsChecksumInterface $checksumProvider,
CacheSerializerInterface $serializer,
$prefix = ''
) {
Expand All @@ -34,6 +38,7 @@ public function __construct(
$this->redis = null;
watchdog_exception('wmpage_cache_redis', $e);
}
$this->checksumProvider = $checksumProvider;
$this->serializer = $serializer;
$this->prefix = $prefix;
}
Expand All @@ -60,11 +65,29 @@ public function loadMultiple(array $ids, bool $includeBody = true): \Iterator
$rows = $this->redis->mget(
$this->prefix($chunk, $includeBody ? 'body' : '')
);
$checksums = $this->redis->mget(
$this->prefix($chunk, 'checksum')
);

foreach (array_filter($rows) as $rowJson) {
foreach ($rows as $i => $rowJson) {
if (!$rowJson) {
continue;
}
$row = json_decode($rowJson, true);
$item = $this->serializer->denormalize($row);
if ($item->getExpiry() > $time) {
$tags = $this->redis->sMembers(
$this->prefix($item->getId(), 'tags')
);
if (
$item->getExpiry() > $time
&& (
!isset($checksums[$i])
|| $this->checksumProvider->isValid(
$checksums[$i],
$tags
)
)
) {
yield $item;
}
}
Expand All @@ -91,6 +114,11 @@ public function set(Cache $item, array $tags): void
json_encode($this->serializer->normalize($item, true)),
($item->getExpiry() - $time)
);
$tx->set(
$this->prefix($id, 'checksum'),
$this->checksumProvider->getCurrentChecksum($tags),
($item->getExpiry() - $time)
);
$tx->zAdd(
$this->prefix('expiries'),
$item->getExpiry(),
Expand Down Expand Up @@ -155,6 +183,7 @@ public function remove(array $ids): void
$tx->del($this->prefix($id));
$tx->del($this->prefix($id, 'body'));
$tx->del($this->prefix($id, 'tags'));
$tx->del($this->prefix($id, 'checksum'));
$tx->del($this->prefix($id, 'path'));
$tx->zRem($this->prefix('expiries'), $id);

Expand Down
10 changes: 10 additions & 0 deletions wmpage_cache_redis.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,15 @@ services:
class: Drupal\wmpage_cache_redis\Storage\RedisStorage
arguments:
- '@wmpage_cache.redis.factory'
- '@wmpage_cache.checksum'
- '@wmpage_cache.serializer'
- '%wmpage_cache.redis.prefix%'

wmpage_cache_redis.checksum_factory:
class: Drupal\wmpage_cache_redis\RedisCacheTagsChecksumClientFactory

wmpage_cache_redis.checksum:
class: Drupal\wmpage_cache_redis\RedisCacheTagsChecksum
arguments:
- '@wmpage_cache_redis.checksum_factory'
tags: [{ name: cache_tags_invalidator }]