Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Question: Expiration allmost never happens on it's own in the background #248

@ChrisRichner

Description

@ChrisRichner

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:

Startup.cs -> services.AddMemoryCache(c => c.CompactOnMemoryPressure = false);

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.

What do I miss?
Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions