Distributed Memory Cache

Karl Gardner 165 Reputation points
2024-09-02T04:05:19.39+00:00

Hello,


I have a question about how the IDistributedCache interface works with Asp.Net Core and specifically the AddDistribetedMemoryCache extension method. Let's say we have one VM instance on an app service webapp. Going with the example in the documentation, we set a key and value in the cache: _cache.SetAsync("cachedTimeUTC", encodedCurrentTimeUTC). Now the web app (hosted on Azure App Service) has listened for requests on the server and ran a program that saved data in memory. So essentially this key-value pair is saved in memory on that VM instance (I'm assuming RAM memory). What I am confused about is if another user who visited this same site at a later time would be able to access that same key-value pair in memory? So does this key-value pair persist in memory after the https session has finished?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,808 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,526 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,459 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
7,679 questions
{count} votes

Accepted answer
  1. ajkuma 25,791 Reputation points Microsoft Employee
    2024-09-05T17:51:48.28+00:00

    Karl Gardner, Following-up on this:

    Based on my understanding of your scenario description:

    -- If another user who visited this same site at a later time would be able to access that same key-value pair in memory?
    Yes, they would.

     

    -- does this key-value pair persist in memory after the https session has finished?
    Yes, it does.

     

    --If the keys are global. This essentially means it persists in memory (for any user) after the key-value pair is set? 

    That is correct.

     

    There are limits that might lead to cache entries being evicted, but these limits have no connection to the user or the HTTPS session.

    Just to highlight, depending on your requirement - you may consider to use MemoryCache directly with AddMemoryCache instead of AddDistributedMemoryCache. Unless you need a component that requires IDistributedCache or plan to switch to a different cache store, this approach offers more flexibility, such as the ability to store arbitrary objects rather than just byte arrays and set priority levels for each cache entry.

    Also, probably worth pointing to sessionstate which is per-session (not to be confused with per-user) and built on top of IDistributedCache (which means you can use AddDistributedMemoryCache to back it).
    Please checkout the note in the doc: Warning

    Don't store sensitive data in session state. The user might not close the browser and clear the session cookie. Some browsers maintain valid session cookies across browser windows. A session might not be restricted to a single user. The next user might continue to browse the app with the same session cookie.


    If the answer helped (pointed you in the right direction) > please click Accept Answer Or please share the requested/more info to help you better.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Sreeju Nair 12,346 Reputation points
    2024-09-02T04:16:58.14+00:00

    The keys for the distributed cache are Global.

    E.g. if you want to store the cache key for a particular user, then you need to construct a key in association with the user, e.g. cacheKey + userID. Based on your application you can build your caching logic. Once you set a key with a value, any request accessing the key will get the same value.

    hope this helps.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.