从StackExchange.Redis缓存中删除/删除所有/一项

问题描述 投票:38回答:4

我正在使用Azure Redis缓存服务的StackExchange.Redis客户端。这是我的班级,

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
    }
}

更新:在Marc的帮助下,这是我的最后一堂课

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();    
        }
    }
}

现在我不知道如何从redis缓存中删除所有项目或单个项目。

azure stackexchange.redis azure-redis-cache
4个回答
50
投票

要删除单个项目:

_cache.KeyDelete(key);

删除所有涉及FLUSHDBFLUSHALL redis命令;两者都可以在StackExchange.Redis中找到;但是,for reasons discussed here,他们不在IDatabase API上(因为:它们影响服务器,而不是逻辑数据库)。

根据“那我该如何使用它们?”在该页面上:

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

(很可能在多路复用器上使用GetEndpoints()之后)


22
投票

我无法在Azure Redis缓存中刷新数据库,出现此错误:

除非启用管理模式,否则此操作不可用:FLUSHDB

而是遍历所有键以删除:

var endpoints = connectionMultiplexer.GetEndPoints();
var server = connectionMultiplexer.GetServer(endpoints.First());
//FlushDatabase didn't work for me: got error admin mode not enabled error
//server.FlushDatabase();
var keys = server.Keys();
foreach (var key in keys)
{
  Console.WriteLine("Removing Key {0} from cache", key.ToString());
  _cache.KeyDelete(key);
}

4
投票

@ Rasi和@Marc Gravell的答案都包含所需的代码片段。基于上面,这里是假设只有一个服务器的工作片段:

您需要使用allowAdmin=true连接到redis,获得此类选项的一种方法是将AllowAdmin分配给已经解析的字符串:

var options = ConfigurationOptions.Parse("server:6379");
options.AllowAdmin = true;
var redis = ConnectionMultiplexer.Connect(options);

然后到flush all数据库:

var endpoints = redis.GetEndPoints();
var server = redis.GetServer(endpoints[0]);
server.FlushAllDatabases();

以上将适用于任何redis部署,而不仅仅是Azure。


0
投票

您也可以删除哈希,即如果要从任何缓存列表中清除特定值。例如,我们有一个emp列表,内部有不同的部门作为缓存。

public static void DeleteHash(string key, string cacheSubKey)
        {
            if (string.IsNullOrEmpty(key))
                throw new ArgumentNullException("key");

            Cache.HashDelete(key, cacheSubKey);
        }

所以你也可以传递Key name和cache子键。

© www.soinside.com 2019 - 2024. All rights reserved.