use*_*567 39 azure stackexchange.redis azure-redis-cache
我正在使用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
}
}
Run Code Online (Sandbox Code Playgroud)
更新:在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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在我不知道如何从redis缓存中删除所有项目或单个项目.
Mar*_*ell 51
要删除单个项目:
_cache.KeyDelete(key);
Run Code Online (Sandbox Code Playgroud)
要删除all,请执行FLUSHDBor FLUSHALLredis命令; 两者都可以在StackExchange.Redis中找到; 但是,由于这里讨论的原因,它们不在IDatabaseAPI上(因为:它们影响服务器,而不是逻辑数据库).
根据"那我该如何使用它们?" 在该页面上:
server.FlushDatabase(); // to wipe a single database, 0 by default
server.FlushAllDatabases(); // to wipe all databases
Run Code Online (Sandbox Code Playgroud)
(很可能在使用GetEndpoints()多路复用器后)
RaS*_*aSi 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);
}
Run Code Online (Sandbox Code Playgroud)
@Rasi 和 @Marc Gravell 的两个答案都包含所需的代码片段。基于以上,这里是假设只有 1 个服务器的工作片段:
您需要使用 连接到 redis allowAdmin=true,获取此类选项的一种方法是将 AllowAdmin 分配给已解析的字符串:
var options = ConfigurationOptions.Parse("server:6379");
options.AllowAdmin = true;
var redis = ConnectionMultiplexer.Connect(options);
Run Code Online (Sandbox Code Playgroud)
然后刷新所有数据库:
var endpoints = redis.GetEndPoints();
var server = redis.GetServer(endpoints[0]);
server.FlushAllDatabases();
Run Code Online (Sandbox Code Playgroud)
以上适用于任何 redis 部署,而不仅仅是 Azure。
| 归档时间: |
|
| 查看次数: |
37341 次 |
| 最近记录: |