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

前端 未结 4 528
执念已碎
执念已碎 2021-01-31 14:54

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

public class RedisCacheService : ICacheService
{
    private readonly ISe         


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-31 15:05

    Both answers by @Rasi and @Marc Gravell contain pieces of code needed. Based on above, here is working snippet assuming there is just 1 server:

    You need to connect to redis with allowAdmin=true, one way to obtain such options is to assign AllowAdmin to already parsed string:

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

    Then to flush all databases:

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

    Above will work on any redis deployment, not just Azure.

提交回复
热议问题