Remove/Delete all/one item from StackExchange.Redis cache

user960567 picture user960567 · Jul 2, 2014 · Viewed 51.1k times · Source

I am using StackExchange.Redis client with Azure Redis Cache Service. Here is my class,

public class RedisCacheService : ICacheService
{
    private readonly ISettings _settings;
    private readonly IDatabase _cache;

    public RedisCacheService(ISettings settings)
    {
        _settings = settings;
        var connectionMultiplexer = ConnectionMultiplexer.Connect(settings.RedisConnection);
        _cache = connectionMultiplexer.GetDatabase();
    }

    public bool Exists(string key)
    {
        return _cache.KeyExists(key);
    }

    public void Save(string key, string value)
    {
        var ts = TimeSpan.FromMinutes(_settings.CacheTimeout);
        _cache.StringSet(key, value, ts);
    }

    public string Get(string key)
    {
        return _cache.StringGet(key);
    }

    public void Remove(string key)
    {
        // How to remove one
    }

    public void Clear()
    {
        // How to remove all
    }
}

Update: From the help of Marc, Here is my final class

public class RedisCacheService : ICacheService
{
    private readonly ISettings _settings;
    private readonly IDatabase _cache;
    private static ConnectionMultiplexer _connectionMultiplexer;

    static RedisCacheService()
    {
        var connection = ConfigurationManager.AppSettings["RedisConnection"];
        _connectionMultiplexer = ConnectionMultiplexer.Connect(connection);
    }

    public RedisCacheService(ISettings settings)
    {
        _settings = settings;
        _cache = _connectionMultiplexer.GetDatabase();
    }

    public bool Exists(string key)
    {
        return _cache.KeyExists(key);
    }

    public void Save(string key, string value)
    {
        var ts = TimeSpan.FromMinutes(_settings.CacheTimeout);
        _cache.StringSet(key, value, ts);
    }

    public string Get(string key)
    {
        return _cache.StringGet(key);
    }

    public void Remove(string key)
    {
        _cache.KeyDelete(key);
    }

    public void Clear()
    {
        var endpoints = _connectionMultiplexer.GetEndPoints(true);
        foreach (var endpoint in endpoints)
        {
            var server = _connectionMultiplexer.GetServer(endpoint);
            server.FlushAllDatabases();    
        }
    }
}

Now I don't know how to remove all items or single item from redis cache.

Answer

Marc Gravell picture Marc Gravell · Jul 2, 2014

To remove a single item:

_cache.KeyDelete(key);

To remove all involves the FLUSHDB or FLUSHALL redis command; both are available in StackExchange.Redis; but, for reasons discussed here, they are not on the IDatabase API (because: they affect servers, not logical databases).

As per the "So how do I use them?" on that page:

server.FlushDatabase(); // to wipe a single database, 0 by default
server.FlushAllDatabases(); // to wipe all databases

(quite possibly after using GetEndpoints() on the multiplexer)