Asp.net mvc 3 在mvc应用程序中使用cache

Asp.net mvc 3 在mvc应用程序中使用cache,asp.net-mvc-3,caching,Asp.net Mvc 3,Caching,我有一个关于mvc web应用程序中缓存的问题。 我想使用缓存来存储许多以这种方式频繁使用的列表 List<IncotermDTO> incoterm; string keyIncoterm = "listaIncoterm"; if (!CacheHelper.Get(keyIncoterm, out incoterm)) { incoterm = BLIncoterm.Ge

我有一个关于mvc web应用程序中缓存的问题。 我想使用缓存来存储许多以这种方式频繁使用的列表

List<IncotermDTO> incoterm;
             string keyIncoterm = "listaIncoterm";
             if (!CacheHelper.Get(keyIncoterm, out incoterm))
            {
                incoterm = BLIncoterm.GetIncoterm(null, null);
                CacheHelper.Add(incoterm, keyIncoterm);
            }
            ViewBag.listaIncoterm = new SelectList(incoterm.OrderBy(x => x.DESCRIPTION), "ID", "DESCRIPTION");
国际贸易术语解释通则清单;
字符串keyIncoterm=“listaIncoterm”;
如果(!CacheHelper.Get(keyIncoterm,out incoterm))
{
incoterm=BLIncoterm.GetIncoterm(null,null);
CacheHelper.Add(国际贸易术语解释通则,国际贸易术语解释通则);
}
ViewBag.listaIncoterm=新选择列表(incoterm.OrderBy(x=>x.DESCRIPTION),“ID”,“DESCRIPTION”);
遵循本文中的提示

这是类助手

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Caching;

namespace GestioneMovimentazioni
{
    public static class CacheHelper
    {
        /// <summary>
        /// Insert value into the cache using
        /// appropriate name/value pairs
        /// </summary>
        /// <typeparam name="T">Type of cached item</typeparam>
        /// <param name="o">Item to be cached</param>
        /// <param name="key">Name of item</param>
        public static void Add<T>(T o, string key)
        {
            // NOTE: Apply expiration parameters as you see fit.
            // I typically pull from configuration file.

            // In this example, I want an absolute
            // timeout so changes will always be reflected
            // at that time. Hence, the NoSlidingExpiration.
            HttpContext.Current.Cache.Insert(
                key,
                o,
                null,
                System.Web.Caching.Cache.NoAbsoluteExpiration,
                TimeSpan.FromSeconds(120));
        }

        /// <summary>
        /// Remove item from cache
        /// </summary>
        /// <param name="key">Name of cached item</param>
        public static void Clear(string key)
        {
            HttpContext.Current.Cache.Remove(key);
        }

        /// <summary>
        /// Check for item in cache
        /// </summary>
        /// <param name="key">Name of cached item</param>
        /// <returns></returns>
        public static bool Exists(string key)
        {
            return HttpContext.Current.Cache[key] != null;
        }

        /// <summary>
        /// Retrieve cached item
        /// </summary>
        /// <typeparam name="T">Type of cached item</typeparam>
        /// <param name="key">Name of cached item</param>
        /// <param name="value">Cached value. Default(T) if
        /// item doesn't exist.</param>
        /// <returns>Cached item as type</returns>
        public static bool Get<T>(string key, out T value)
        {
            try
            {
                if (!Exists(key))
                {
                    value = default(T);
                    return false;
                }

                value = (T)HttpContext.Current.Cache[key];
            }
            catch
            {
                value = default(T);
                return false;
            }

            return true;
        }


        public static T Get<T>(string key) where T : class
        {
            try
            {
                return (T)HttpContext.Current.Cache[key];
            }
            catch
            {
                return null;
            }
        }




    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Caching;
命名空间GestioneMovimentazioni
{
公共静态类CacheHelper
{
/// 
///使用将值插入缓存
///适当的名称/值对
/// 
///缓存项的类型
///要缓存的项目
///项目名称
公共静态无效添加(TO,字符串键)
{
//注意:根据需要应用过期参数。
//我通常从配置文件中提取。
//在这个例子中,我想要一个绝对值
//超时,以便始终反映更改
//那时。因此,NoSlidingExpiration。
HttpContext.Current.Cache.Insert(
钥匙
啊,,
无效的
System.Web.Caching.Cache.NoAbsoluteExport,
时间跨度从秒(120));
}
/// 
///从缓存中删除项
/// 
///缓存项的名称
公共静态无效清除(字符串键)
{
HttpContext.Current.Cache.Remove(键);
}
/// 
///检查缓存中的项
/// 
///缓存项的名称
/// 
存在公共静态bool(字符串键)
{
返回HttpContext.Current.Cache[key]!=null;
}
/// 
///检索缓存项
/// 
///缓存项的类型
///缓存项的名称
///缓存值。默认值(T)如果
///项目不存在。
///缓存项作为类型
公共静态bool Get(字符串键,out T值)
{
尝试
{
如果(!存在(键))
{
值=默认值(T);
返回false;
}
value=(T)HttpContext.Current.Cache[key];
}
抓住
{
值=默认值(T);
返回false;
}
返回true;
}
公共静态T Get(字符串键),其中T:class
{
尝试
{
返回(T)HttpContext.Current.Cache[key];
}
抓住
{
返回null;
}
}
}
}
这就是问题所在。。 是否为应用程序的所有用户缓存此列表?
如果没有,您建议采用什么实现?

HttpContext.Current.Cache对所有用户都是相同的。至少在不使用负载平衡或Webfarm的情况下