ASP.NET MVC应用程序变量?
在ASP.NET应用程序存在变数?我想存储所有用户应该是独立的,每5分钟更新一次的对象。但是,所有用户应该经常看到它的最后一个版本。任何建议(C#)?
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 version of it. Any suggestions (C#)?
您可以存储应用程序范围内的数据在ASP.NET的缓存。
You can store application-wide data in the ASP.NET Cache.
您的项目添加到缓存中使用 Cache.Insert 方法。滑动到期值设置为5分钟的时间跨度。编写一个包装类为高速缓存访问对象。包装类可以提供一个方法,以获得从缓存的对象。这种方法可以检查是否该项目是否在缓存中,并加载它,如果它不是。
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.
例如:
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;
}
}