Skip to content

Commit a29fd47

Browse files
taylorotwelltegos
authored andcommitted
atomically flush redis cache tags (laravel#57098)
1 parent fcdaa73 commit a29fd47

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

src/Illuminate/Cache/RedisTaggedCache.php

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Illuminate\Cache\Events\CacheFlushed;
66
use Illuminate\Cache\Events\CacheFlushing;
7+
use Illuminate\Redis\Connections\PhpRedisConnection;
8+
use Illuminate\Redis\Connections\PredisConnection;
79

810
class RedisTaggedCache extends TaggedCache
911
{
@@ -110,8 +112,46 @@ public function flush()
110112
{
111113
$this->event(new CacheFlushing($this->getName()));
112114

113-
$this->flushValues();
114-
$this->tags->flush();
115+
$redisPrefix = match ($this->store->connection()::class) {
116+
PhpRedisConnection::class => $this->store->connection()->client()->getOption(\Redis::OPT_PREFIX),
117+
PredisConnection::class => $this->store->connection()->client()->getOptions()->prefix,
118+
};
119+
120+
$cachePrefix = $redisPrefix.$this->store->getPrefix();
121+
122+
$cacheTags = [];
123+
$keysToBeDeleted = [];
124+
125+
foreach ($this->tags->getNames() as $name) {
126+
$cacheTags[] = $cachePrefix.$this->tags->tagId($name);
127+
}
128+
129+
foreach ($this->tags->entries() as $entry) {
130+
$keysToBeDeleted[] = $this->store->getPrefix().$entry;
131+
}
132+
133+
$script = <<<'LUA'
134+
local prefix = table.remove(ARGV, 1)
135+
136+
for i, key in ipairs(KEYS) do
137+
redis.call('DEL', key)
138+
139+
for j, arg in ipairs(ARGV) do
140+
local zkey = string.gsub(key, prefix, "")
141+
redis.call('ZREM', arg, zkey)
142+
end
143+
end
144+
LUA;
145+
146+
$this->store->connection()->eval(
147+
$script,
148+
count($keysToBeDeleted),
149+
...$keysToBeDeleted,
150+
...[$cachePrefix, ...$cacheTags]
151+
);
152+
153+
// $this->flushValues();
154+
// $this->tags->flush();
115155

116156
$this->event(new CacheFlushed($this->getName()));
117157

0 commit comments

Comments
 (0)