在mvc应用程序中使用缓存

时间:2014-02-10 12:10:15

标签: asp.net-mvc-3 caching

我在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");

遵循本文中的提示 http://johnnycoder.com/blog/2008/12/10/c-cache-helper-class/

这是班助手

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




    }
}

这是个问题.. 是否会为该应用程序的所有用户缓存此列表? 如果没有,你建议实施什么?

1 个答案:

答案 0 :(得分:1)

HttpContext.Current.Cache对所有用户都是一样的。至少只要您不使用Loadbalancing或Webfarm ..