Http级别缓存助手类(ASP.Net Core)

2021-01-06 07:29

阅读:751

标签:collect   sed   key   value   tac   add   rom   sum   res   

用于http级别缓存,防止在一次http请求中查询相同数据

    /// 
    /// 使用HttpContext的暂存对象存储要被缓存的信息
    /// 
    public class HTTPCacheManager
    {

        private readonly IHttpContextAccessor _httpContextAccessor;

        public HTTPCacheManager(IHttpContextAccessor httpContextAccessor)
        {
            this._httpContextAccessor = httpContextAccessor;
        }

        /// 
        /// Gets a key/value collection that can be used to share data within the scope of this request 
        /// 
        protected virtual IDictionary GetItems()
        {
            return this._httpContextAccessor.HttpContext?.Items;
        }

        public virtual object Get(string key)
        {
            object result = null;
            var items = this.GetItems();
            if (items != null)
            {
                result = items[key];
            }
            return result;
        }

        /// 
        /// Get a cached item. If it‘s not in the cache yet, then load and cache it
        /// 
        /// Type of cached item
        /// Cache key
        /// The cached value associated with the specified key
        public virtual T Get(string key)
        {
            T result = default(T);
            var items = this.GetItems();
            if (items != null)
            {
                result = (T)this.Get(key);
            }
            return result;
        }

        /// 
        /// Adds the specified key and object to the cache
        /// 
        /// Key of cached item
        /// Value for caching
        public virtual void Set(string key, object data)
        {
            var items = this.GetItems();
            if (items != null && data != null)
            {
                items[key] = data;
            }
        }

        /// 
        /// Gets a value indicating whether the value associated with the specified key is cached
        /// 
        /// Key of cached item
        /// True if item already is in cache; otherwise false
        public virtual bool IsSet(string key)
        {
            bool result = false;
            var items = this.GetItems();
            if (items != null)
            {
                result = items[key] != null;
            }
            return result;
        }

        /// 
        /// Removes the value with the specified key from the cache
        /// 
        /// Key of cached item
        public virtual void Remove(string key)
        {
            var items = this.GetItems();
            if (items != null)
            {
                items.Remove(key);
            }
        }

        /// 
        /// Removes items by key pattern
        /// 
        /// String key pattern
        public virtual void RemoveByPattern(string pattern)
        {
            var items = this.GetItems();
            if (items != null)
            {
                //get cache keys that matches pattern
                var regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
                var matchesKeys = items.Keys.Select(p => p.ToString()).Where(key => regex.IsMatch(key)).ToList();

                //remove matching values
                foreach (var key in matchesKeys)
                {
                    items.Remove(key);
                }
            }
        }

        /// 
        /// Clear all cache data
        /// 
        public virtual void Clear()
        {
            var items = this.GetItems();
            if (items != null)
            {
                items.Clear();
            }
        }
    }

Http级别缓存助手类(ASP.Net Core)

标签:collect   sed   key   value   tac   add   rom   sum   res   

原文地址:https://www.cnblogs.com/fanfan-90/p/13587414.html


评论


亲,登录后才可以留言!