0

I have some static values coming from Database which needs to be persisted in the application for every user logins.(I am using ASP.Net MVC 4 application)

For that I have implemented MemoryCache (not sure if this is better than to use HttpApplicationState or System.Web.HttpContext.Current.Cache.). Appreciated if any hint I can get on this too.

//Create a custom policy as per the business need
CacheItemPolicy wkflPolicy = new CacheItemPolicy(); // I dont want to set expiration


if (wkflCache.Get("MyKey") == null)
        {
            // Caching all static DB values.
            DataSet myDS= objEditComponent.GetDBValues();
            wkflCache.Add("MyKey", myDS, wkflPolicy);
        }

Its persisting for all postbacks but when I am making Ajax call from my cshtml (view) page I could not get the cache value thus have to make another DB call, after checking null condition.

Is it happening due to low memory (wondering how come every time low memory happens when I am doing a ajax call) or I am missing something here?

Biki
  • 2,518
  • 8
  • 39
  • 53

1 Answers1

0

It is best to assume that anything you put in the cache can be deleted at random.

If it is something that you need all the time and is shared by all users, just shove it into a static variable. (Ideally while making all of its properties read-only.)

The cache is for things you need some of the time, but don't care too much about if you have to fetch again.

Jonathan Allen
  • 68,373
  • 70
  • 259
  • 447