ASP.NET MVC Application Variables?

前端 未结 2 1909
慢半拍i
慢半拍i 2021-01-15 01:28

are there Application variables in ASP.NET? I want to store for all Users an Object which should be independent updated every 5 Minutes. But all users should always see last

相关标签:
2条回答
  • 2021-01-15 01:59

    You can use the OutputCacheAttribute for the method in question. It has a duration property.

    0 讨论(0)
  • 2021-01-15 02:01

    You can store application-wide data in the ASP.NET Cache.

    Add your item to the cache using the Cache.Insert method. Set the sliding expiration value to a TimeSpan of 5 minutes. Write a wrapper class for accessing the object in the cache. The wrapper class can provide a method to obtain the object from the cache. This method can check whether whether the item is in the cache and load it if it isn't.

    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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题