我正在使用 .net core api (2.1) 和 aws redis 缓存。我看不到将过期设置为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;
}
缓存可用多长时间?如何设置过期时间?如果这是不可能的,我该如何删除缓存?