HttpRuntime Cache与静态字典/字段

Rad*_*ský 8 c# asp.net caching httpruntime

主要有哪些优点缺点使用的httpRuntime缓存不要使用简单的静态字段?

我需要将数据存储在整个ASP.NET应用程序的范围内.

HttpRuntime.Cache["MyData"] = someHashtable;
Run Code Online (Sandbox Code Playgroud)

private static System.Collections.Hashtable _myData;
public static System.Collections.Hashtable MyData
{
    get
    {
        if (_myData == null)
        {
            _myData = new System.Collections.Hashtable();
            // TODO: Load data
        }
        return _myData;
    }
}
Run Code Online (Sandbox Code Playgroud)

fox*_*oxy 5

HttpRuntime.Cache除非明确设置(意味着对象可以在任何时间到期),否则对象中的对象具有未知的到期时间,而HashTable作为应用程序池的live 对象中的对象是活动的(除非您手动删除条目).该HttpRuntime.Cache还允许您设置各种其它特性,如(可选)高速缓存项的优先级和到期时间.