【问题标题】:Best practices for cache and session manager for an MVC ApplicationMVC 应用程序的缓存和会话管理器的最佳实践
【发布时间】:2013-12-27 17:19:30
【问题描述】:

大师,

我们在过去的应用程序中通过各种方式实现了 CacheManagerSessionManager,例如创建 SessionHelper 静态类和 CacheHelper 静态类。

虽然它运作良好,但我们缺乏一些概括和全球化视角的能力。 因此,对于新的从头开发,我们打算在灵活性和可扩展性方面为此类通用实施提供最佳实践。

请提出建议。

【问题讨论】:

    标签: asp.net-mvc session caching


    【解决方案1】:

    您可以创建一个接口来定义缓存和会话管理中使用的常用操作,命名为 IStateManager。例如

    /// <summary>
    /// An interface to provide access to a state storage implementation
    /// </summary>
    public interface IStateManager
    {
        /// <summary>
        /// Gets or sets the value associated with the specified key.
        /// </summary>
        /// <typeparam name="T">Type</typeparam>
        /// <param name="key">The key of the value to get.</param>
        /// <returns>The value associated with the specified key.</returns>
        T Get<T>(string key);
    
        /// <summary>
        /// Adds the specified key and object to the state manager.
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="data">Data</param>
        void Set(string key, object data);
    
        /// <summary>
        /// Adds the specified key and object to the state manager.
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="data">Data</param>
        /// <param name="cacheTime">Cache time</param>
        void Set(string key, object data, int cacheTime);
    
        /// <summary>
        /// Gets a value indicating whether the value associated with the specified key is in the state manager.
        /// </summary>
        /// <param name="key">key</param>
        /// <returns>Result</returns>
        bool IsSet(string key);
    
        /// <summary>
        /// Removes the value with the specified key from the state manager.
        /// </summary>
        /// <param name="key">/key</param>
        void Remove(string key);
    
        /// <summary>
        /// Removes items by pattern
        /// </summary>
        /// <param name="pattern">pattern</param>
        void RemoveByPattern(string pattern);
    
        /// <summary>
        /// Clear all state manager data
        /// </summary>
        void Clear();
    }
    

    然后,您可以创建接口的实现以提供不同的功能。例如。使用 System.Runtime.Caching 的内存实现

    /// <summary>
    /// Represents an in memory cache
    /// </summary>
    public class MemoryCacheManager : IStateManager
    {
        public MemoryCacheManager()
        {
        }
    
        protected ObjectCache Cache
        {
            get
            {
                return MemoryCache.Default;
            }
        }
    
        /// <summary>
        /// Gets or sets the value associated with the specified key.
        /// </summary>
        /// <typeparam name="T">Type</typeparam>
        /// <param name="key">The key of the value to get.</param>
        /// <returns>The value associated with the specified key.</returns>
        public T Get<T>(string key)
        {
            return (T)Cache[key];
        }
    
        /// <summary>
        /// Adds the specified key and object to the cache with a default cache time of 30 minutes.
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="data">Data</param>
        public void Set(string key, object data)
        {
            Set(key, data, 30);
        }
    
        /// <summary>
        /// Adds the specified key and object to the cache.
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="data">Data</param>
        /// <param name="cacheTime">Cache time</param>
        public void Set(string key, object data, int cacheTime)
        {
            if (data == null)
                return;
    
            var policy = new CacheItemPolicy();
            policy.AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(cacheTime);
            Cache.Add(new CacheItem(key, data), policy);
        }
    
        /// <summary>
        /// Gets a value indicating whether the value associated with the specified key is cached
        /// </summary>
        /// <param name="key">key</param>
        /// <returns>Result</returns>
        public bool IsSet(string key)
        {
            return (Cache.Contains(key));
        }
    
        /// <summary>
        /// Removes the value with the specified key from the cache
        /// </summary>
        /// <param name="key">/key</param>
        public void Remove(string key)
        {
            Cache.Remove(key);
        }
    
        /// <summary>
        /// Removes items by pattern
        /// </summary>
        /// <param name="pattern">pattern</param>
        public void RemoveByPattern(string pattern)
        {
            var regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
            var keysToRemove = new List<String>();
    
            foreach (var item in Cache)
                if (regex.IsMatch(item.Key))
                    keysToRemove.Add(item.Key);
    
            foreach (string key in keysToRemove)
            {
                Remove(key);
            }
        }
    
        /// <summary>
        /// Clear all cache data
        /// </summary>
        public void Clear()
        {
            foreach (var item in Cache)
                Remove(item.Key);
        }
    }
    

    您可以创建此接口的多个实现,例如为您的应用程序提供分布式缓存的“Memcached”实现或提供基于用户会话的功能的“会话”实现。

    然后,您可以使用您选择的依赖容器将实现注入您的服务\控制器并连接您的应用程序。

    尽量避免可能会给单元测试带来问题的静态类。

    【讨论】:

      【解决方案2】:

      可以使用过滤器属性进行缓存,可以通过单例类处理会话..

      http://weblogs.asp.net/scottgu/archive/2007/11/13/asp-net-mvc-framework-part-1.aspx

      您可以在上面的链接中获取一些示例,以了解最好的方法或方法。

      【讨论】:

      • 需要一些示例,因为 Singlton 有其自身的复杂性。
      猜你喜欢
      • 2011-03-29
      • 2014-01-24
      • 1970-01-01
      • 2016-10-23
      • 2015-04-30
      • 2014-03-11
      • 2011-12-12
      • 2010-09-06
      相关资源
      最近更新 更多