【摘要】 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)的内容,更多资讯请及时关注考必过网站,最新消息小编会第一时间发布,大家考试加油!