-1

I am using a class that makes use of a static ConcurrentDictionary object for caching purposes. When data is requested, it first looks in the ConcurrentDictionary. If it is not found there, it is retrieved from the database, and put in the ConcurrentDictionary.

The data stays in this collection across multiple requests, as it should. But is this special behavior due to it being a ConcurrentDictionary instead of a regular Dictionary, or is that just the behavior of any static object?

Also, sometimes when I load the page, it has to reload data from the database. Where is this data stored, and what would cause this cache/dictionary to be cleared? Is there a timeout setting in IIS for the website or application pool that controls this?

GendoIkari
  • 11,734
  • 6
  • 62
  • 104

1 Answers1

1

ConcurrentDictionary is great when working with multiple threads - it prevents multiple "write" operations to the same key.

A static variable is great when working with a shared data across multiple requests/users (but may not be thread safe).

So in short:

  • It is being shared across multiple users because of its static attribute.
  • It is preventing multiple threads from accessing the dictionary because its a ConcurrentDictionary

As for the second part of your questions:

static variable of your class are not garbage collected until the app domain hosting your class is unloaded

Look at this answer: How and when are c# Static members disposed?

Community
  • 1
  • 1
Simcha Khabinsky
  • 1,970
  • 2
  • 19
  • 34
  • Thanks. But when is the app domain unloaded? Should it remain indefinitely unless IIS or the hosting server is restarted? Or is there a timeout set somewhere? – GendoIkari Nov 12 '14 at 21:00