ASP.NET MVC Application Variables?

Are there application variables in ASP.NET? I want to keep an object for all users that needs to be independently updated every 5 minutes. But all users should always see the latest version. Any suggestions (C #)?

+2


source to share


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


You can use the OutputCacheAttribute on the method in question. It has the property of duration.



0


source







All Articles