码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2020-09-11 15:55:16      阅读:42      评论:0      收藏:0      [点我收藏+]

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

用于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();
            }
        }
    }

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

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

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!