如何解决设置过期时间后Redis密钥未过期
目前我正在开发一个 .Netcore WebAPI。我正在为此 Azure Cache for Redis
使用 Microsoft.Extensions.Caching.StackExchangeRedis
和 Nuget。
我在 StartUp 中有以下代码
services.AddDistributedRedisCache(ch => ch.Configuration = Configuration["Redis:Connection"]);
我已经实现了 GetAsync 和 SetAsync 的扩展方法,其过期时间如下所示。
public static async Task<T> GetCacheValueAsync<T>(this IDistributedCache cache,string key) where T : class
{
string result = await cache.GetStringAsync(key);
if (string.IsNullOrEmpty(result))
{
return null;
}
var deserializedObj = JsonConvert.DeserializeObject<T>(result);
return deserializedObj;
}
public static async Task SetCacheValueAsync<T>(this IDistributedCache cache,string key,T value) where T : class
{
_ = new DistributedCacheEntryOptions
{
// Remove item from cache after duration
AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(60),//AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(60)
// Remove item from cache if unsued for the duration
SlidingExpiration = TimeSpan.FromSeconds(30)
};
string result = JsonConvert.SerializeObject(value);
//string result = value.ToJsonString();
await cache.SetStringAsync(key,result);
}
问题:- 尽管我正在设置 AbsoluteExpirationRelativeToNow 和 AbsoluteExpiration(其中任何一个)以及 SlidingExpiration(假设小于 2)。我几乎没有给出 1 分钟的过期时间,但密钥没有过期。所以每次我调用相同的陈旧数据时。我错过了什么?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。