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

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

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

        private readonly IHttpContextAccessor _httpContextAccessor;

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

        /// <summary>
        /// Gets a key/value collection that can be used to share data within the scope of this request 
        /// </summary>
        protected virtual IDictionary<object, object> 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;
        }

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

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

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

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

        /// <summary>
        /// Removes items by key pattern
        /// </summary>
        /// <param name="pattern">String key pattern</param>
        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);
                }
            }
        }

        /// <summary>
        /// Clear all cache data
        /// </summary>
        public virtual void Clear()
        {
            var items = this.GetItems();
            if (items != null)
            {
                items.Clear();
            }
        }
    }
原文地址:https://www.cnblogs.com/fanfan-90/p/13587414.html