Currently i am developing an .Netcore WebAPI. I am using Azure Cache for Redis
& Nuget for this Microsoft.Extensions.Caching.StackExchangeRedis
.
I have below code in StartUp
services.AddDistributedRedisCache(ch => ch.Configuration = Configuration["Redis:Connection"]);
I have implimeted the extension methods for GetAsync & SetAsync with expiration time as shown below.
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);
}
Issue:- Although i am setting AbsoluteExpirationRelativeToNow & AbsoluteExpiration (any one of these) along with SlidingExpiration (which suppose to be lessthen above 2). Hardly i am giving 1 min expiration, but keys not getting expired. so every time i am calling its same stale data. what am i missing?