ASP.NET MVC Application Variables?
2 answers
You can store application data in the ASP.NET cache .
Add your item to the cache using the Cache.Insert method . Set the value of the full transition time to TimeSpan 5 minutes. Write a wrapper class to access the object in the cache. A wrapper class can provide a method to get an object from the cache. This method can check if the item is in the cache and load it if it is not.
For example:
public static class CacheHelper
{
public static MyObject Get()
{
MyObject obj = HttpRuntime.Cache.Get("myobject") as MyObject;
if (obj == null)
{
// Create the object to insert into the cache
obj = CreateObjectByWhateverMeansNecessary();
HttpRuntime.Cache.Insert("myobject", obj, null, DateTime.Now.AddMinutes(5), System.Web.Caching.Cache.NoSlidingExpiration);
}
return obj;
}
}
+3
source to share