2014-02-10 4 views
0

mvc 웹 응용 프로그램 캐시 관련 질문이 있습니다. 나는이 방법이 문서 http://johnnycoder.com/blog/2008/12/10/c-cache-helper-class/mvc 응용 프로그램에서 캐시 사용

이있는 팁 다음

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

에서 자주 사용되는 많은 목록을 저장하는 캐시를 사용하고자하는 클래스 헬퍼

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

HttpContext.Current.Cache는 모든 사용자가 동일합니다. 최소한 Loadbalancing 또는 Webfarm을 사용하지 않는 한 ..

관련 문제