Ole*_*ich 5 c# asp.net stackexchange.redis
我正在使用StackExchange.Redis并构建一些redis客户端界面RedisClientManager.在我的界面中,我有2个密钥设置器(按时间跨度和日期时间到期):
按时间跨度:
public void Set(string key, object value, TimeSpan timeout)
{
_cache.StringSet(key, Serialize(value), timeout);
}
Run Code Online (Sandbox Code Playgroud)
截止日期:
public void Set(string key, object value, DateTime expires)
{
_cache.StringSet(key, Serialize(value));
_cache.KeyExpire(key, expires);
}
Run Code Online (Sandbox Code Playgroud)
用法:
按时间跨度:
RedisClientManager.Set(o.Key, o, new TimeSpan(0, 0, 5, 0));
Run Code Online (Sandbox Code Playgroud)
截止日期:
RedisClientManager.Set(o.Key, o, DateTime.UtcNow.AddMinutes(5));
Run Code Online (Sandbox Code Playgroud)
如果我使用Timespan(第一种方法)添加新密钥,则该对象位于Redis缓存中,并在5分钟后过期.如果使用Date(第二种方法)添加新密钥,则该对象不会添加到Redis.此问题仅在服务器上发生.在localhost上一切正常.可能是redis使用本地服务器时间键吗?我该如何解决这个问题?
使用密钥设置绝对到期的正确方法是什么StackExchange.Redis?
小智 8
怎么样......
public void Set(string key, object value, DateTime expires)
{
var expiryTimeSpan = expires.Subtract(DateTime.UtcNow);
_cache.StringSet(key, Serialize(value), expiryTimeSpan);
//or Set(key, value, expiryTimeSpan);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8680 次 |
| 最近记录: |