我正在使用带有aws redis缓存的.net核心应用程序接口(2.1)。我看不出有什么方法可以将过期时间设置为IDistributedCache.SetAsync。这怎么可能呢?
我的代码片段如下:
// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddDistributedRedisCache(options =>
{
var redisCacheUrl = Configuration["RedisCacheUrl"];
if (!string.IsNullOrEmpty(redisCacheUrl))
{
options.Configuration = redisCacheUrl;
}
});
}
//Set & GetCache
public async Task<R> GetInsights<R>(string cacheKey, IDistributedCache _distributedCache)
{
var encodedResult = await _distributedCache.GetStringAsync(cacheKey);
if (!string.IsNullOrWhiteSpace(encodedResult))
{
var cacheValue = JsonConvert.DeserializeObject<R>(encodedResult);
return cacheValue;
}
var result = GetResults<R>(); //Call to resource access
var encodedResult = JsonConvert.SerializeObject(result);
await _distributedCache.SetAsync(cacheKey, Encoding.UTF8.GetBytes(encodedResult)); //Duration?
return result;
}
缓存有多长时间可用?如何设置过期时间?如果这是不可能的,我如何删除缓存?
发布于 2019-02-20 04:40:02
它在options
参数中。您可以传递DistributedCacheEntryOptions
的一个实例,该实例具有可用于设置过期时间的各种属性。例如:
await _distributedCache.SetAsync(cacheKey, Encoding.UTF8.GetBytes(encodedResult), new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1)
});
https://stackoverflow.com/questions/54774517
复制相似问题