C# ASP.Net中的数据缓存

C# ASP.Net中的数据缓存,c#,.net,asp.net,caching,C#,.net,Asp.net,Caching,我需要填写一些参考数据的下拉框。i、 城市名单,国家名单等,我需要填写在各种网络表单。我认为,我们应该将这些数据缓存在我们的应用程序中,这样,我们就不会在每个表单上访问数据库。我不熟悉缓存和ASP.Net。请告诉我如何做到这一点。从您的其他问题中,我了解到您使用的是dal、业务和表示层的三层体系结构 所以我假设您有一些数据访问类。理想的做法是使用同一类的缓存实现并在其中进行缓存 例如:如果您有一个接口IUserRepository,那么UserRepository类将实现它,并通过方法在db中添

我需要填写一些参考数据的下拉框。i、 城市名单,国家名单等,我需要填写在各种网络表单。我认为,我们应该将这些数据缓存在我们的应用程序中,这样,我们就不会在每个表单上访问数据库。我不熟悉缓存和ASP.Net。请告诉我如何做到这一点。

从您的其他问题中,我了解到您使用的是dal、业务和表示层的三层体系结构

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

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


显然,您的CachedUserRepository也将具有cache对象的实例。您可以查看有关如何使用缓存对象的详细信息。

我总是将以下类添加到我的所有项目中,以便轻松访问缓存对象。落实这一点,遵循哈桑·汗的答案将是一个好办法

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;
        }
    }
公共静态类CacheHelper
{
/// 
///使用将值插入缓存
///适当的名称/值对
/// 
///缓存项的类型
///要缓存的项目
///项目名称
公共静态void Add(TO,字符串键,双超时)
{
HttpContext.Current.Cache.Insert(
钥匙
啊,,
无效的
DateTime.Now.AddMinutes(超时),
System.Web.Caching.Cache.NoSlidingExpiration);
}
/// 
///从缓存中删除项
/// 
///缓存项的名称
公共静态无效清除(字符串键)
{
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;
}
}

。。。就一般文化/词汇而言,这被称为“装饰者”模式。也就是说,您使用实现缓存的附加功能来“装饰”初始存储库。