清除 StackExchange.Redis 具有可配置到期时间的缓存
Clear StackExchange.Redis Cache with configurable expiry
我想知道是否有办法在指定的到期日期后从我的 c# 服务中清除/刷新整个缓存。我如何在 C# 中实现这一点?
C#
// library
using StackExchange.Redis;
public class RedisCacheService
{
// Fields
ConnectionMultiplexer _Redis = null;
IDatabase _RedisDB = null;
// ctor
public RedisCacheService(String redisConnectionString) // , DateTime ExpiryDate ? - Extracted from config
{
try
{
if (String.IsNullOrEmpty(redisConnectionString))
{
throw new Exception("The 'redisConnectionString' parameter is unassigned");
}
this._Redis = ConnectionMultiplexer.Connect(redisConString);
if (_Redis == null)
{
throw new Exception("_Redis object not initialized");
}
this._RedisDB = _Redis.GetDatabase();
// I need to set some sort of expiry config here in the constructor.
// The redisConnectionString is passed through when an instance of this class is created in the host startup and
// the redisConnectionString is extracted from the config.
}
catch (Exception e)
{
throw new Exception(e);
}
}
}
我看到了一个类似的解决方案,当我实际用对象填充缓存时,我可以传递一个过期时间,但我更想要一个全局缓存清除解决方案。
如有任何帮助或建议,我们将不胜感激。
您可以向 Redis 添加假密钥,例如 _waitforit
,并设置了 TTL。
然后你等待它过期。
同时,您订阅 key space events 以获取过期密钥。当您的 _waitforit 密钥过期时,您将刷新数据库。
这些事件的唯一缺点是您必须在 Redis 服务器上明确启用它。不过可以通过配置或命令行完成。你需要 CONFIG SET notify-keyspace-events Exe
我想
在站点注释中:
如果您不想自己编写所有代码,您可以使用 CacheManager 为您完成大部分工作。
它内置过期配置,您可以收听所有这些事件。
您也可以订阅通过键 space 事件触发的普通 C# 事件 onRemoveByHandle
(如果一切配置正确)。
我想知道是否有办法在指定的到期日期后从我的 c# 服务中清除/刷新整个缓存。我如何在 C# 中实现这一点?
C#
// library
using StackExchange.Redis;
public class RedisCacheService
{
// Fields
ConnectionMultiplexer _Redis = null;
IDatabase _RedisDB = null;
// ctor
public RedisCacheService(String redisConnectionString) // , DateTime ExpiryDate ? - Extracted from config
{
try
{
if (String.IsNullOrEmpty(redisConnectionString))
{
throw new Exception("The 'redisConnectionString' parameter is unassigned");
}
this._Redis = ConnectionMultiplexer.Connect(redisConString);
if (_Redis == null)
{
throw new Exception("_Redis object not initialized");
}
this._RedisDB = _Redis.GetDatabase();
// I need to set some sort of expiry config here in the constructor.
// The redisConnectionString is passed through when an instance of this class is created in the host startup and
// the redisConnectionString is extracted from the config.
}
catch (Exception e)
{
throw new Exception(e);
}
}
}
我看到了一个类似的解决方案,当我实际用对象填充缓存时,我可以传递一个过期时间,但我更想要一个全局缓存清除解决方案。
如有任何帮助或建议,我们将不胜感激。
您可以向 Redis 添加假密钥,例如 _waitforit
,并设置了 TTL。
然后你等待它过期。
同时,您订阅 key space events 以获取过期密钥。当您的 _waitforit 密钥过期时,您将刷新数据库。
这些事件的唯一缺点是您必须在 Redis 服务器上明确启用它。不过可以通过配置或命令行完成。你需要 CONFIG SET notify-keyspace-events Exe
我想
在站点注释中:
如果您不想自己编写所有代码,您可以使用 CacheManager 为您完成大部分工作。
它内置过期配置,您可以收听所有这些事件。
您也可以订阅通过键 space 事件触发的普通 C# 事件 onRemoveByHandle
(如果一切配置正确)。