C#网站缓存管理类分享
admin
2024-05-13 06:32:38
0

        web站点中缓存的重要性毋庸置疑,我想很多网站开发人员在开发web应用系统的时候优先考虑使用的缓存并不是第三方缓存解决方案(比如 分布式缓存memcached、redis等等),而应该是.net framework已经提供的缓存解决方案。

        这里分享个人整理的网站缓存管理类,虽然没有第三方缓存方案那么的完整和高可用,但是易用性、轻量级的优势显而易见。

一、缓存管理类主要方法目录

1、网站缓存开关,当前的缓存是否可用一键管理,可以通过配置文件管理

2、检查缓存中是否存在指定的键

3、检查系统中是否存在指定的缓存

4、从缓存中获取指定键的值

5、获取缓存中键值的数量

6、添加缓存

7、返回指定的缓存

8、移除键中某关键字的缓存并返回相应的值

9、移除键中所有的缓存

10、对缓存优先级做一个默认的转换

二、源码分享:

1、网站缓存开关,当前的缓存是否可用一键管理,可以通过配置文件管理

private static readonly object lockObj = new object();
/// 
/// 当前的缓存是否可用
/// 
private bool enable = false;
/// 
/// 默认实例
/// 
private static WebCache instance = null;
/// 
/// 返回默认WebCache缓存实例
/// 
/// 是否可用最好放到配置项里配置下
public static WebCache GetCacheService(bool enable)
{if (instance == null){lock (lockObj){if (instance == null){instance = new WebCache(enable);}}}return instance;
}
/// 
/// 构造方法
/// 
private WebCache(bool enable)
{this.enable = enable;
}
/// 
/// 获取一个值,指示当前的缓存是否可用
/// 
public bool EnableCache
{get{return this.enable;}
}

2、检查缓存中是否存在指定的键

/// 
/// 检查缓存中是否存在指定的键
/// 
/// 要检查的键
/// 返回一个值,指示检查的键是否存在
public bool Contains(string key)
{if (this.enable){return HttpRuntime.Cache[key] != null;}return false;
}

3、检查系统中是否存在指定的缓存

/// 
/// 检查系统中是否存在指定的缓存
/// 
/// 类型
/// 缓存key
/// 返回这个类型的值是否存在
public bool Contains(string key)
{object value = HttpRuntime.Cache[key];if (value is T){return true;}return false;
}

4、从缓存中获取指定键的值

/// 
/// 从缓存中获取指定键的值
/// 
/// 要获取的键
/// 返回指定键的值
public T Get(string key)
{if (this.enable){return (T)HttpRuntime.Cache[key];}return default(T);
}

5、获取缓存中键值的数量

/// 
/// 获取缓存中键值的数量
/// 
public int Count
{get{if (this.enable){return HttpRuntime.Cache.Count;}return 0;}
}

6、添加缓存

/// 
/// 添加缓存
/// 
/// 关键字
/// 缓存值
public void Add(string key, T value)
{if (this.enable){HttpRuntime.Cache.Insert(key, value);}return;
}
/// 
/// 添加缓存
/// 
/// 关键字
/// 缓存值
/// 过期时间
public void Add(string key, T value, DateTime absoluteExpiration)
{if (this.enable){HttpRuntime.Cache.Insert(key, value, null, absoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration);}return;
}
/// 
/// 添加缓存
/// 
/// 关键字
/// 缓存值
/// 保存时间
public void Add(string key, T value, TimeSpan slidingExpiration)
{if (this.enable){HttpRuntime.Cache.Insert(key, value, null, System.Web.Caching.Cache.NoAbsoluteExpiration, slidingExpiration);}return;
}
/// 
/// 添加缓存
/// 
/// 关键字
/// 缓存值
/// 保存时间(分钟)
public void Add(string key, T value, int minutes)
{if (this.enable){HttpRuntime.Cache.Insert(key, value, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, minutes, 0));}return;
}
/// 
/// 添加缓存
/// 
/// 关键字
/// 缓存值
/// 优先级
/// 保存时间
public void Add(string key, T value, CachePriority priority, TimeSpan slidingExpiration)
{if (this.enable){HttpRuntime.Cache.Insert(key, value, null, System.Web.Caching.Cache.NoAbsoluteExpiration, slidingExpiration, CacheItemPriorityConvert(priority), null);}return;
}
/// 
/// 添加缓存
/// 
/// 关键字
/// 缓存值
/// 优先级
/// 过期时间
public void Add(string key, T value, CachePriority priority, DateTime absoluteExpiration)
{if (this.enable){HttpRuntime.Cache.Insert(key, value, null, absoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, CacheItemPriorityConvert(priority), null);}return;
}

7、返回指定的缓存

/// 
/// 尝试返回指定的缓存
/// 
/// 缓存内容的类型
/// 缓存的key
/// 缓存的内容
/// 是否存在这个缓存
public bool TryGetValue(string key, out T value)
{object temp = HttpRuntime.Cache[key];if (temp != null && temp is T){value = (T)temp;return true;}value = default(T);return false;
}

8、移除键中某关键字的缓存并返回相应的值

/// 
/// 移除键中某关键字的缓存并返回相应的值
/// 
/// 关键字
public void Remove(string key)
{object result = null;if (this.enable){if (HttpRuntime.Cache[key] != null){result = HttpRuntime.Cache.Remove(key);}}return;
}
/// 
/// 移除键中带某关键字的缓存
/// 
/// 关键字
public int RemoveContains(string key)
{int result = 0;if (this.enable){System.Collections.IDictionaryEnumerator CacheEnum = HttpRuntime.Cache.GetEnumerator();while (CacheEnum.MoveNext()){if (CacheEnum.Key.ToString().Contains(key)){HttpRuntime.Cache.Remove(CacheEnum.Key.ToString());result++;}}}return result;
}
/// 
/// 移除键中以某关键字开头的缓存
/// 
/// 关键字
public int RemoveStartWith(string key)
{int result = 0;if (this.enable){System.Collections.IDictionaryEnumerator CacheEnum = HttpRuntime.Cache.GetEnumerator();while (CacheEnum.MoveNext()){if (CacheEnum.Key.ToString().StartsWith(key)){HttpRuntime.Cache.Remove(CacheEnum.Key.ToString());result++;}}}return result;
}
/// 
/// 移除键中以某关键字结尾的缓存
/// 
/// 关键字
public int RemoveEndWith(string key)
{int result = 0;if (this.enable){System.Collections.IDictionaryEnumerator CacheEnum = HttpRuntime.Cache.GetEnumerator();while (CacheEnum.MoveNext()){if (CacheEnum.Key.ToString().EndsWith(key)){HttpRuntime.Cache.Remove(CacheEnum.Key.ToString());result++;}}}return result;
}

9、移除键中所有的缓存

/// 
/// 移除键中所有的缓存
/// 
public int Clear()
{int result = 0;if (this.enable){System.Collections.IDictionaryEnumerator CacheEnum = HttpRuntime.Cache.GetEnumerator();while (CacheEnum.MoveNext()){HttpRuntime.Cache.Remove(CacheEnum.Key.ToString());result++;}keys.Clear();}return result;
}
private List keys = new List();
/// 
/// 缓存中所有的键列表
/// 
public ReadOnlyCollection Keys
{get{if (this.enable){lock (keys){keys.Clear();System.Collections.IDictionaryEnumerator CacheEnum = HttpRuntime.Cache.GetEnumerator();while (CacheEnum.MoveNext()){keys.Add(CacheEnum.Key.ToString());}}}return new ReadOnlyCollection(keys);}
}

10、对缓存优先级做一个默认的转换

/// 
/// 对缓存优先级做一个默认的转换
/// 
/// 原始的优先级
/// 目标优先级
private CacheItemPriority CacheItemPriorityConvert(CachePriority priority)
{CacheItemPriority p = CacheItemPriority.Default;switch (priority){case CachePriority.Low:{p = CacheItemPriority.Low;break;}case CachePriority.Normal:{p = CacheItemPriority.Normal;break;}case CachePriority.High:{p = CacheItemPriority.High;break;}case CachePriority.NotRemovable:{p = CacheItemPriority.NotRemovable;break;}}return p;
}
}

相关内容

热门资讯

斗金订购APP贵金属期货投资被...   斗金订购APP的投资者被广告宣传给诱导,注册就送什么现金,然后充值返现金卷等等这些宣传方式,都是...
哈易购APP非法期货交易欺骗投...   哈易购APP宣传可做白银铂金贵金属订购交易,但实际上并没有取得相关交易资质!哈易购APP本质上就...
消息称百度旗下昆仑芯瞄准500... 6 月 29 日消息,据《The Information》昨日援引知情人士消息,百度旗下 AI 芯片...
打造夏日消费新场景 第35届北... 北京商报讯(记者 翟枫瑞)6月29日消息,第35届北京国际燕京啤酒文化节新闻发布会在京举行。本届啤酒...
社保基金持仓数据出炉,一季度增... 最近各大上市公司一季度财报都公开了,咱们国家社保基金的持仓数据也全部曝光。目前社保拿着比亚迪价值44...
36氪首发 | 海思、中兴团队... 作者 | 乔钰杰 编辑 | 袁斯来 硬氪获悉,广州宸思通讯科技有限公司(以下简称“宸思科技”)近日完...
两天蒸发47亿市值!一纸税务通... 一纸税务通知书,能让一家百亿龙头两天蒸发47亿市值。 6月22日,北大荒(600598.SH)公告称...
SK海力士将投资1100万亿韩... SK集团会长崔泰源6月29日在韩国“三大重大计划”发布会上宣布,公司将投资1100万亿韩元扩大半导体...
两只A股,终止上市! 两家A股公司,即将摘牌。 6月29日,退市沪科(600608.SH)公告称,上海证券交易所将在202...
原创 M... 一家成立近十年的自动驾驶公司,在IPO时吸引了14家基石投资者认购近一半的发行股份,其中不乏奔驰、比...
基金忠言|国寿安保滤镜碎,三年... 图片来源:视觉中国 蓝鲸新闻6月29日讯(记者 祁和忠)保险系基金公司国寿安保总经理换人了。 6月2...
三星电机计划加码玻璃基板!相关... 6月29日,玻璃基板概念股午后有所回升, 华工科技(000988.SZ)逼近涨停, 彩虹股份(600...
拉萨海关持续壮大外贸经营主体 ...   新华网拉萨6月28日电(记者蒋梦辰)近日,记者从拉萨海关获悉,今年前5个月,西藏有进出口实绩的外...
机构:二季报临近,医药生物板块... 6月29日,华源证券发布了一篇医药生物行业的研究报告,报告指出,业绩期临近,产业链景气度有望再次迎来...
每日收评科创50放量涨超4.5... 财联社6月29日讯,三大指数全线收红,创业板指探底回升,科创50指数大涨4.61%。沪深两市成交额3...
6月多地土拍结构性升温:深圳单... 进入2026年6月,不少城市核心区地块集中诞生高溢价宗地,热度突出的城市包含深圳、杭州、长沙。 其中...
业绩炸裂!盛达资源半年预盈3.... 6月29日,贵金属矿山龙头盛达资源(000603.SZ)发布 2026 年半年度业绩预告,上半年业绩...
A股午后拉升三大股指收涨:半导... A股三大股指6月29日开盘涨跌互现。早盘沪强深弱,创指一度跌超2%。半导体午后拉升,带动两市上涨,沪...
原创 空... 前言 大家好,我是老金。 这几天,两幅极度割裂的画面放在一起,把我看笑了。 一边是在持续的热浪下,欧...