的httpRuntime缓存与静态辞典/场 [英] HttpRuntime Cache vs. static dictionary/fields

查看:163
本文介绍了的httpRuntime缓存与静态辞典/场的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有哪些主要的赞成劣势使用的httpRuntime缓存不要使用简单的静态字段?

我需要将数据存储在整个ASP.NET应用范围。

  HttpRuntime.Cache [迈德特] = someHashtable;

VS

 私有静态System.Collections.Hashtable _myData;
公共静态System.Collections.Hashtable迈德特
{
    得到
    {
        如果(_myData == NULL)
        {
            _myData =新System.Collections.Hashtable();
            // TODO:加载数据
        }
        返回_myData;
    }
}


解决方案

HttpRuntime.Cache 对象有未知的到期期限,除非明确地设置(即对象可以过期的任何时间),而内的对象你的的HashTable 住您的应用程序池是活的(除非你手动删除条目)。在 HttpRuntime.Cache ,您还可以设置不同等特点,如(可选)高速缓存项的优先级和到期时间。

What are the main pros and cons for using HttpRuntime Cache against using simple static field?

I need to store data in scope of entire ASP.NET application.

HttpRuntime.Cache["MyData"] = someHashtable;

vs.

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;
    }
}

解决方案

Objects in HttpRuntime.Cache have unknown expiry periods unless explicitly set (meaning that objects can expire any time), whereas objects within your HashTable live for as your application pool is alive (unless you manually remove an entry). The HttpRuntime.Cache also allows you to set various other characteristics, such as (optional) cache item priority and expiry time.

这篇关于的httpRuntime缓存与静态辞典/场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆