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

相关内容

热门资讯

电商平台报送收入大于申报收入,... 收到税务短信提示“平台报送收入大于申报收入”,很多企业第一反应是把订单金额直接改进申报表。但在分账、...
重磅警告!马斯克:如果没有人工... (图片来源:摄图网) 2月5日,埃隆·马斯克做客Dwarkesh Patel播客时,再次就美国债务问...
一次性出手8公斤金条!金价高位... 金价剧烈波动后,投资者心态正悄然生变。买卖两旺的现象重现金市,但投资主线已经转向“防风险”。 2月1...
港交所18C章规则优化+跨境服... 2025年以来,全球跨境资本市场呈现出上市规则优化与服务体系升级并行的发展态势,港交所与纳斯达克持续...
板块异动 | 招标概念板块表现... 截至2月11日9点50分,招标概念板块表现活跃,招标股份涨超14%,国义招标涨超11%。另外,广咨国...
一站式美国CE认证 CE Ma... 这是(scsgroup)整理的信息,希望能帮助到大家 作为一名关注工业品和消费品领域的小编,我经常收...
ETF规模速报 | 影视ETF... 昨日市场全天窄幅震荡,三大指数涨跌不一,科创50指数涨近1%。从板块来看,传媒板块大涨,人形机器人概...
雷军总结昨晚小年直播,称新一代... IT之家 2 月 11 日消息,昨晚的直播结束后,小米创办人、董事长兼 CEO 雷军对小年直播进行了...
热联集团赴港IPO:两年分红近... 瑞财经 王敏 2月9日,据港交所官网,杭州热联集团股份有限公司(以下简称“热联集团”)向港交所提交上...
谨防购金“一口价” 稀释市场诚... 舒朗秋 部分商家以“一口价”销售为由,弱化甚至刻意隐瞒商品克重、计价方式等关键信息;一些网络商家销售...
雷军:初代SU7不到2年交付3... 作者:龚进辉 昨天是北方小年,小米掌门人雷军带来了春节前最后一场直播,依然干货满满、猛料不断。他在直...
看好金价年底到6000美元!法... 财联社2月11日讯(编辑 潇湘)法国巴黎银行大宗商品策略主管David Wilson本周最新表示,随...
2026商业航天系列专题之卫星... 今天分享的是:2026商业航天系列专题之卫星篇:梳理中国星座计划 报告共计:16页 中国商业航天正步...
300亿基金经理辞职 近日,天弘基金发布“致天弘固收+投资者的一封信”,宣布基金经理姜晓丽因个人原因卸任其所管理的全部公募...
2026年太原GEO优化服务商... 2026年,太原正加速推进装备制造、新能源汽车、文旅消费等优势产业升级,企业对AI搜索场景下的流量获...
2026年的印度很自信:与欧美... 2026年开年,印度过得似乎还不赖。 先是在1月27日宣布,与欧盟达成有史以来最大的协定;然后在2月...
起底中财期货:做空白银三日狂赚... 本报(chinatimes.net.cn)记者胡金华 上海摄影报道 一则《边锡明中财期货精准做空白银...
原创 铁... 抗日战争时期,山东大地曾活跃着一支令日伪军心惊胆战的武装力量——铁道游击队。这支队伍如幽灵般出没于敌...