Coder Social home page Coder Social logo

Comments (4)

Tratcher avatar Tratcher commented on June 19, 2024

There are some known issues with MemoryCache (and DistributedMemoryCache) over-eagerly removing items due to perceived memory load. See CompactOnMemoryPressure: https://github.com/aspnet/Caching/blob/d417a71d7be76a629336227f3bb75d46bc6cf9a3/src/Microsoft.Extensions.Caching.Memory/MemoryCacheOptions.cs#L21

This has been redesigned for 2.0 RTM

from session.

 avatar commented on June 19, 2024

Thank you for the information.

In the meantime we may have found a workaround for v1.1.2.
When having the services declared in this order we constantly lose the session:
services.AddMvc();
services.AddDistributedMemoryCache();
services.AddSession(o => o.IdleTimeout = TimeSpan.FromMinutes(20);

As soon as we invert the lines the problem disappears:
services.AddDistributedMemoryCache();
services.AddSession(o => o.IdleTimeout = TimeSpan.FromMinutes(20);
services.AddMvc();

This workaround is actually working, we have tried inverting multiple times the lines of code on multiple PCs and whenever we add Mvc after MemoryCache and Session we are never been able to get the "Accessing expired session" warning.

I thought only the Configure method needed to have each IApplicationBuilder defined in order, while adding IServiceCollection in the ConfigureServices method did not have that requirement.
Am I wrong? Does this make sense to you?

from session.

Tratcher avatar Tratcher commented on June 19, 2024

Service registration order can matter if multiple things try to register the same service differently. These differences may explain what you're seeing:
https://github.com/aspnet/Mvc/blob/rel/1.1.3/src/Microsoft.AspNetCore.Mvc.TagHelpers/DependencyInjection/TagHelperExtensions.cs#L34-L35
https://github.com/aspnet/Caching/blob/rel/1.1.2/src/Microsoft.Extensions.Caching.Memory/MemoryCacheServiceCollectionExtensions.cs#L83

The MVC registration uses a shared MemoryCache and shared MemoryCacheOptions. The Caching one uses an isolated MemoryCache and options. Sharing the cache may be worse for you if something else is putting a lot of load on it.

from session.

 avatar commented on June 19, 2024

Thank you @Tratcher for your clarification!

from session.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.