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

时间:2020-09-12 03:20:38 来源:

【摘要】 Http级别缓存助手类(ASP.Net Core)考必过小编为大家整理了关于Http级别缓存助手类(ASP.Net Core)的信息,希望可以帮助到大家!

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

标签:collectsedkeyvaluetacaddromsumres

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

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

        private readonly IHttpContextAccessor _httpContextAccessor;

        pubpc 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;
        }

        pubpc virtual object Get(string key)
        {
            object respt = npl;
            var items = this.GetItems();
            if (items != npl)
            {
                respt = items[key];
            }
            return respt;
        }

        /// <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>
        pubpc virtual T Get<T>(string key)
        {
            T respt = defapt(T);
            var items = this.GetItems();
            if (items != npl)
            {
                respt = (T)this.Get(key);
            }
            return respt;
        }

        /// <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>
        pubpc virtual void Set(string key, object data)
        {
            var items = this.GetItems();
            if (items != npl && data != npl)
            {
                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>
        pubpc virtual bool IsSet(string key)
        {
            bool respt = false;
            var items = this.GetItems();
            if (items != npl)
            {
                respt = items[key] != npl;
            }
            return respt;
        }

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

        /// <summary>
        /// Removes items by key pattern
        /// </summary>
        /// <param name="pattern">String key pattern</param>
        pubpc virtual void RemoveByPattern(string pattern)
        {
            var items = this.GetItems();
            if (items != npl)
            {
                //get cache keys that matches pattern
                var regex = new Regex(pattern, RegexOptions.Singlepne | 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>
        pubpc virtual void Clear()
        {
            var items = this.GetItems();
            if (items != npl)
            {
                items.Clear();
            }
        }
    }

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

标签:collectsedkeyvaluetacaddromsumres

以上就是Http级别缓存助手类(ASP.Net Core)的内容,更多资讯请及时关注考必过网站,最新消息小编会第一时间发布,大家考试加油!

上一篇      下一篇
前端相关推荐 更多>>
Netty那点事-前导篇 ASP.NET MVC 表单提交多层子级实体集合数据到控制器中 CenterNet 数据加载解析 .net mvc中SignalR的使用配置 ASP.Net Core 发布后,静态资源访问不了 kubernetes(十三) k8s 业务上线流程(手动版) asp.net core 3.1 自定义中间件实现jwt token认证 centos 7 Docker容器启动报WARNING: IPv4 forwarding
前端热点专题 更多>>
热点问答
国家公务员考试年龄限制是多少 公务员国考和省考考试内容有什么区别 函授大专学历能不能考公务员 国家公务员考试考点能自己选择吗 新闻学专业能报考2022年公务员考试吗 什么是联合培养研究生 什么是破格录取研究生 什么人不适合读研 研究生报名户口所在地填什么 研究生结业和毕业有什么区别
网站首页 网站地图 返回顶部
考必过移动版 https://m.kaobiguo.net