zoukankan      html  css  js  c++  java
  • 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();
                }
            }
        }
    
  • 相关阅读:
    洛谷P3406 海底高铁[差分 贪心]
    POJ3398Perfect Service[树形DP 树的最大独立集变形]
    POJ3928Ping pong[树状数组 仿逆序对]
    UVALive
    UVALive
    http协议进阶(二)URL与资源
    http协议进阶(一)HTTP概述
    http协议基础(十)实体首部字段
    http协议基础(九)响应首部字段
    http协议基础(八)请求首部字段
  • 原文地址:https://www.cnblogs.com/fanfan-90/p/13587414.html
Copyright © 2011-2022 走看看