3

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()
Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
SamJolly
  • 6,347
  • 13
  • 59
  • 125
  • 5
    Depends on the scope of the variable, among other things. Show us your code. – Patrick Hofman Apr 04 '18 at 16:42
  • I see what you are saying. So in my case it is created as a private variable within a class, so as long as that class is instantiated then it is present. I thought for some weird reason that it persisted out of process, even though the calling object had been disposed. – SamJolly Apr 04 '18 at 16:46
  • 3
    It's static so it will last for the life time of the application domain once that type's static constructor has been executed. – Igor Apr 04 '18 at 17:02

2 Answers2

5

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.

StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • The interesting challenge here is when to update it as it is actually populated from REST API call. Data is fairly static and I believe Azure App pools refresh pretty often on different VMs within the App Service so this will potentially force a fairly regular repopulation anyway. – SamJolly Apr 04 '18 at 19:59
2

If the dictionary is indeed a static class level field, it will live as long as the application pool does. It is shared across the users on the same application pool.

If the list is only populated once depends on the way you call the population method. I would advise proper locking or use of Lazy<T> (with the correct constructor) to ensure exclusiveness.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325