You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Dec 14, 2018. It is now read-only.
I'm currently implementing a class which should mimic session timeouts. Before writing this on my own I've thought taking the IMemoryCache implementation should do the trick as well.
I'm having a plain vanilla asp.net core web api project. I've added the nuget package to project.json and added the following code:
Then I'm going to use the ctor injected IMemoryCache instance in my class by TryGetValue to get out an existing item or Set() to add a new one.
I use RegisterPostEvictionCallback to run some code when the item expires and add an expiration token to be able to have a Reset() method which expires all items at once -> Clear()
if (MemoryCache.TryGetValue<TokenSessionMapping>(key, out tsm))
{
return tsm;
}
MemoryCache.Set(key, tsm,
new MemoryCacheEntryOptions()
.SetSlidingExpiration(TimeSpan.FromSeconds(15))
.SetAbsoluteExpiration(TimeSpan.FromHours(1))
.RegisterPostEvictionCallback(PostEvictionCallback)
.AddExpirationToken(new CancellationChangeToken(CancellationTokenSource.Token))
);
The problem I'm having is that the items don't expire automatically. It needs another web api call that accesses the Set() method to trigger the evicted callback on the already expired items.
Why didn't the evicted callback fire when the item expired in first place? I can't afford to have another web api to trigger the session timeouts for the system.