Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TASK: Improve flushByTags performance #32

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
49 changes: 45 additions & 4 deletions Classes/RedisBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ class RedisBackend extends IndependentAbstractBackend implements TaggableBackend

protected static array $loggedErrors = [];

/**
* Redis allows a maximum of 1024 * 1024 parameters, but we use a lower limit to prevent long blocking calls.
*/
protected int $batchSize = 100000;

/**
* @param EnvironmentConfiguration $environmentConfiguration
* @param array $options Configuration options - depends on the actual backend
Expand Down Expand Up @@ -284,11 +289,47 @@ public function flushByTag(string $tag): int
*/
public function flushByTags(array $tags): int
{
$flushedTags = 0;
foreach ($tags as $tag) {
$flushedTags += $this->flushByTag($tag);
if ($this->isFrozen()) {
throw new \RuntimeException(sprintf('Cannot add or modify cache entry because the backend of cache "%s" is frozen.', $this->cacheIdentifier), 1647642328);
}
return $flushedTags;

// language=lua
$script = "
local total_entries = 0
local num_arg = #ARGV
for i = 1, num_arg do
local entries = redis.call('SMEMBERS', KEYS[i])
for k1,entryIdentifier in ipairs(entries) do
redis.call('DEL', ARGV[i]..'entry:'..entryIdentifier)

local tags = redis.call('SMEMBERS', ARGV[i]..'tags:'..entryIdentifier)
for k2,tagName in ipairs(tags) do
redis.call('SREM', ARGV[i]..'tag:'..tagName, entryIdentifier)
end

redis.call('DEL', ARGV[i]..'tags:'..entryIdentifier)
end
redis.call('DEL', KEYS[i])
total_entries = total_entries + #entries
end
return total_entries
";

$flushedEntriesTotal = 0;

// Flush tags in batches
for ($i = 0, $iMax = count($tags); $i < $iMax; $i += $this->batchSize) {
$tagList = array_slice($tags, $i, $this->batchSize);
$keys = array_map(function ($tag) {
return $this->getPrefixedIdentifier('tag:' . $tag);
}, $tagList);
$values = array_fill(0, count($keys), $this->getPrefixedIdentifier(''));

$flushedEntries = $this->client->eval($script, count($keys), ...$keys, ...$values);
$flushedEntriesTotal = is_int($flushedEntries) ? $flushedEntries : 0;
}

return $flushedEntriesTotal;
}

/**
Expand Down