Search code examples
c#asp.netasp.net-mvcasp.net-mvc-5concurrentdictionary

How long does the ConcurrentDictionary object last?


I am using MVC 5, ASP.NET 4.7 on Azure App Services

I am using the ConcurrentDictionary object to persist data to save multiple calls to the data source.

A few questions on its behaviour:

1) Once it has been populated, then it will persist for multiple users of the same web application to access. Am I correct? So only the first user after a web site restart gets a performance hit.

2) How long does it persist for? Is it until another pool refresh or site restart, or is there some form of inactive timeout?

Thanks in advance.

EDIT:

public class myCache
{
    private static readonly ConcurrentDictionary<int, string> myCacheDictionary = new ConcurrentDictionary<int, string>();

    public static async Task populateCache()

Solution

  • 1) Once it has been populated, then it will persist for multiple users of the same web application to access. Am I correct? So only the first user after a web site restart gets a performance hit.

    Yes, the static keyword means that this will be kept around for as long as the application is in memory, unless you do something to clear it out programmatically. That same instance will be used in every request, regardless of user, thread, synchronization context, etc.

    2) How long does it persist for? Is it until another pool refresh or site restart, or is there some form of inactive timeout?

    It'll be there until there's an app pool refresh or site restart, unless you program it otherwise.

    You might be interested in the MemoryCache class if you're using this for caching purposes. It will let you set policies to purge its data after a specific amount of time, or when it detects that you're starting to run low on memory.