【问题标题】:Data Caching in ASP.NetASP.Net 中的数据缓存
【发布时间】:2011-11-19 17:02:08
【问题描述】:

我需要从一些参考数据中填充一些下拉框。即城市列表、国家列表等。我需要在各种网络表格中填写。我认为,我们应该在我们的应用程序中缓存这些数据,这样我们就不会在每个表单上都访问数据库。我是缓存和 ASP.Net 的新手。请建议我如何做到这一点。

【问题讨论】:

    标签: c# .net asp.net caching


    【解决方案1】:

    我总是将以下类添加到我的所有项目中,这使我可以轻松访问 Cache 对象。实现这一点,遵循 Hasan Khan 的回答将是一个不错的方法。

    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, double Timeout)
            {
                HttpContext.Current.Cache.Insert(
                    key,
                    o,
                    null,
                    DateTime.Now.AddMinutes(Timeout),
                    System.Web.Caching.Cache.NoSlidingExpiration);
            }
    
            /// <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;
            }
        }
    

    【讨论】:

      【解决方案2】:

      从您的其他问题中,我了解到您正在使用具有 dal、业务和表示层的 3 层架构。

      所以我假设你有一些数据访问类。理想的做法是拥有同一个类的缓存实现并在其中进行缓存。

      例如:如果你有一个 IUserRepository 接口,那么 UserRepository 类将实现它并通过方法在 db 中添加/删除/更新条目,那么你也可以拥有 CachedUserRepository 它将包含 UserRepository 对象的实例,并且在 get 方法上它将首先查看针对某个键的缓存(从方法参数派生),如果找到该项目,那么它将返回它,否则您在内部对象上调用该方法;获取数据;添加到缓存,然后返回。

      你的 CachedUserRepository 显然也会有缓存对象的实例。 Cache对象的使用方法可以看http://msdn.microsoft.com/en-us/library/18c1wd61(v=vs.85).aspx

      【讨论】:

      • ... 仅用于一般文化/词汇,这称为“装饰器”模式。也就是说,您使用实现缓存的附加功能“装饰”初始存储库。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-27
      • 2011-07-19
      • 2012-06-17
      • 1970-01-01
      相关资源
      最近更新 更多