设置过期时间后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 (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...