简体   繁体   English

ASP.Net中的数据缓存

[英]Data Caching in ASP.Net

I need to fill some dropdown boxex from some reference data. 我需要从一些参考数据中填写一些下拉框。 ie City List, Country List etc. I need to fill it in various webforms. 即城市列表,国家列表等。我需要填写各种网络表格。 I think, we should cache this data in our application so that, we don't hit database on every form. 我认为,我们应该在我们的应用程序中缓存这些数据,这样我们就不会在每个表单上都有数据库。 I am new to caching and ASP.Net. 我是缓存和ASP.Net的新手。 Please suggest me how to do this. 请建议我如何做到这一点。

I always add the following class to all my projects which give me easy access to the Cache object. 我总是将以下类添加到我的所有项目中,这使我可以轻松访问Cache对象。 Implementing this, following Hasan Khan's answer would be a good way to go. 按照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;
        }
    }

From other question of yours I read that you're using 3 layer architecture with dal, business and presentation layer. 从你的其他问题我读到你正在使用带有dal,业务和表示层的3层架构。

So I assume that you have some data access class. 所以我假设你有一些数据访问类。 Ideal thing to do would be to have a cached implementation of the same class and do caching in that. 理想的做法是拥有相同类的缓存实现并在其中进行缓存。

Eg: If you have an interface IUserRepository then UserRepository class would implement it and add/delete/update entries in db via methods then you can also have CachedUserRepository which will contain instance of UserRepository object and on get methods it will first look into the cache against some key (derived from method parameters) and if the item is found then it will return it otherwise you call the method on internal object; 例如:如果你有一个接口IUserRepository,那么UserRepository类将实现它并通过方法在db中添加/删除/更新条目然后你也可以有CachedUserRepository,它将包含UserRepository对象的实例,在get方法上它将首先查看缓存一些键(从方法参数派生),如果找到该项,那么它将返回它,否则你在内部对象上调用该方法; get the data; 得到数据; add to cache and then return it. 添加到缓存然后返回它。

Your CachedUserRepository will also have instance of cache object obviously. 您的CachedUserRepository显然也会有缓存对象的实例。 You can look at http://msdn.microsoft.com/en-us/library/18c1wd61(v=vs.85).aspx for details on how to use Cache object. 有关如何使用Cache对象的详细信息,请查看http://msdn.microsoft.com/en-us/library/18c1wd61(v=vs.85).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM