Cache Busting with Redis Clusters

Viewed 89

Currently we are using clustered redis.

In some instances, we have to delete hundred of thousands of cached objects. I am wondering what's currently the best approach to do so?

At the moment we are executing a LUA scrip with scan and unlink.

private const string ClearCacheLuaScript = (@"
local cursor = 0
local calls = 0
local dels = 0
repeat
    local result = redis.call('SCAN', cursor, 'MATCH', ARGV[1])
    calls = calls + 1
    for _,key in ipairs(result[2]) do
        redis.call('UNLINK', key)
        dels = dels + 1
    end
    cursor = tonumber(result[1])
until cursor == 0");

public async Task FlushAllAsync(string section, string group, string prefix)
{
    var cacheKey = GetCacheKey(section, group, prefix);
    await _cache.ScriptEvaluateAsync(ClearCacheLuaScript
        , values: new RedisValue[]
            {
            cacheKey+"*",
            });
}

Currently wondering if the alternative next best approach would be to iteratively delete with keys. Something like

public async Task FlushAllKeys()
{
    foreach(var key in keys)
    {
        await _cache.KeyDeleteAsync(key );
    }
}

My main question is, should I be deleting via a pattern using LUA or grab all keys and iteratively delete and not use LUA script

0 Answers
Related