Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Azure 从StackExchange.Redis缓存中删除所有/一项_Azure_Stackexchange.redis_Azure Redis Cache - Fatal编程技术网

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

Azure 从StackExchange.Redis缓存中删除所有/一项,azure,stackexchange.redis,azure-redis-cache,Azure,Stackexchange.redis,Azure Redis Cache,我正在使用StackExchange.Redis客户端和Azure Redis缓存服务。这是我的班级 public class RedisCacheService : ICacheService { private readonly ISettings _settings; private readonly IDatabase _cache; public RedisCacheService(ISettings settings) { _sett

我正在使用StackExchange.Redis客户端和Azure 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
    }
}
更新:在马克的帮助下,这是我的最后一堂课

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缓存中删除所有项目或单个项目。

要删除单个项目:

_cache.KeyDelete(key);
删除所有涉及
FLUSHDB
FLUSHALL
redis命令的文件;两者都可以在StackExchange.Redis中使用;但是,它们不在
IDatabase
API上(因为:它们影响服务器,而不是逻辑数据库)

根据该页上的“我如何使用它们?”

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

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

我无法刷新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);
}

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

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

var options = ConfigurationOptions.Parse("server:6379");
options.AllowAdmin = true;
var redis = ConnectionMultiplexer.Connect(options);
然后转到数据库:

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

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

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

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

            Cache.HashDelete(key, cacheSubKey);
        }

因此,您可以传递密钥名并缓存子密钥。

谢谢。如何从多路复用器获取服务器。@user960567<代码>多路复用器.GetServer(…);最简单的方法是从multiplexer.GetEndpoints(true)请求一个端点。显然,如果您只与一台服务器通信,那么这一切就容易多了。这一切都在链接文章的末尾解释过了。但是我只有来自azure的连接字符串,xx.redis.cache.windows.net,ssl=true,password=。。。。然后我如何获得服务器。我的RedisCacheService也是按请求(asp.net)提供的。每个请求实例化ConnectionMultiplexer和IDatabase可以吗。@user960567实际上,我很确定它们可以,但是:我怀疑外部调用程序无法使用它;因此,实际上,如果您只知道一个地址,那么它很有可能是主地址,
GetEndpoints
将只返回一个项作为旁注:我强烈建议在redis前面添加内存缓存;redis很快,但您不发出的redis请求很快faster@MarcGravell,我同意,这就是为什么我也有.Net内存缓存。但它不是分布式的。这意味着我需要支持多个服务器。@MarcGravell我特别使用Redis,因为我有一个扩展的环境,其中有许多应用服务器。内存缓存在这种情况下不起作用,对吗?@IssaFram当然可以-只要你有发布缓存失效的某种策略。幸运的是,redis内置了pub/sub,如果您真的需要,可以启用键空间通知。我们正是使用这种设置(通过我们主动希望立即失效的密钥上的opt-in publish,而不是通过自动密钥空间通知)来处理多服务器集群上的内存+redis缓存确保您在配置字符串中设置
allowAdmin=true
(或
ConfigurationOptions
)@肖恩卡尼-链接断了。这里是更新的链接