1

If I add something to the ASP.NET Application Cache, is it persistent or does it re-load on some interval?

HttpContext.Current.Application["MyKeyName"] = "Some Data";

Will "MyKeyName" exist until the end of the application's life?

Also, the application cache is available for all sessions correct? Meaning, once it loads once for the first user, it doesn't have to load again for the rest of the users.

WebDev999
  • 31
  • 1
  • 3

1 Answers1

2

Per MSDN:

Using the application cache is similar to using application state. However, unlike application state, the data in the application cache is volatile, meaning it is not stored in memory for the life of the application. The advantage of using the application cache is that ASP.NET manages the cache and removes items when they expire or become invalidated, or when memory runs low. You can also configure application caching to notify your application when an item is removed. For more information see Caching Application Data.

The example code you provided

HttpContext.Current.Application["MyKeyName"] = "Some Data";

is using Application State, not using the Application Cache. Application State has many disadvantages to the Application Cache - namely that it is lost when the application pool recycles, it is not thread-safe, there is no way to make the cache expire, and there is no cache dependency mechanism to make it update automatically if the underlying data store is updated. Application State is generally considered to be an antiquated caching technology. See Application State Considerations for more details.

To use the application cache, you would access it through the HttpContext.Cache property. Note that in MVC the HttpContext is available in most places, so you should avoid using the static HttpContext.Current accessor.

// Get
var value = this.HttpContext.Cache["MyKeyName"];

// Set
this.HttpContext.Cache.Insert(
    "MyKeyName", 
    value, 
    null, 
    DateTime.Now.AddMinutes(5), 
    System.Web.Caching.Cache.NoSlidingExpiration,
    System.Web.Caching.CacheItemPriority.NotRemovable,
    null);

There is also a new caching option in .NET 4: System.Runtime.Caching, which is similar to the application cache, but does not have any dependencies on the System.Web namespace.

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • Thanks for your reply. Basically what I am trying to do is load some data when the app starts which will be made available and shared throughout the app for all users until the app pool is restarted. The data will be read-only so thats why i ignored the "non thread safe" aspect. Do you think using the ApplicationState is the best solution for this? Or can you point me to some examples of better approaches? – WebDev999 Jun 11 '16 at 15:14
  • I have never used ApplicationState. It is a carry-over from classic ASP, and AFAIK, it is only there for backward compatibility with that antiquated framework. Loading too much data into the application for its entire lifetime will reduce scalability, but for small amounts it is fine. You could use ApplicationState, but I recommend using the above solution (just replace the 4th parameter with `System.Web.Caching.Cache.NoAbsoluteExpiration`) or `System.Runtime.Caching`. That said, it's better to use [a solution that reloads on demand](http://stackoverflow.com/a/32726827/181087). – NightOwl888 Jun 11 '16 at 16:40