基础缓存操作类
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了基础缓存操作类,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3801字,纯文字阅读大概需要6分钟。
内容图文
![基础缓存操作类](/upload/InfoBanner/zyjiaocheng/1071/399b3b8c95b44d9b8a4e81dc60187a1a.jpg)
1 /// <summary> 2 /// HttpRuntime Cache读取设置缓存信息封装 3 /// 使用描述:给缓存赋值使用HttpRuntimeCache.Set(key,value....)等参数(第三个参数可以传递文件的路径(HttpContext.Current.Server.MapPath())) 4 /// 读取缓存中的值使用JObject jObject=HttpRuntimeCache.Get(key) as JObject,读取到值之后就可以进行一系列判断 5 /// </summary> 6 public class CacheExtend 7 { 8 /// <summary> 9 /// 设置缓存时间,配置(从配置文件中读取) 10 /// </summary> 11 private const double Seconds = 30*24*60*60; 12 13///<summary> 14/// 缓存指定对象,设置缓存 15///</summary> 16publicstaticbool Set(string key, object value) 17{ 18return Set(key, value, null, DateTime.Now.AddSeconds(Seconds), Cache.NoSlidingExpiration, 19 CacheItemPriority.Default, null); 20} 21 22///<summary> 23/// 缓存指定对象,设置缓存 24///</summary> 25publicstaticbool Set(string key, object value, string path) 26{ 27try 28{ 29var cacheDependency = new CacheDependency(path); 30return Set(key, value, cacheDependency); 31} 32catch 33{ 34returnfalse; 35} 36} 37 38///<summary> 39/// 缓存指定对象,设置缓存 40///</summary> 41publicstaticbool Set(string key, object value, CacheDependency cacheDependency) 42{ 43return Set(key, value, cacheDependency, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, 44 CacheItemPriority.Default, null); 45} 46 47///<summary> 48/// 缓存指定对象,设置缓存 49///</summary> 50publicstaticbool Set(string key, object value, double seconds, bool isAbsulute) 51{ 52return Set(key, value, null, (isAbsulute ? DateTime.Now.AddSeconds(seconds) : Cache.NoAbsoluteExpiration), 53 (isAbsulute ? Cache.NoSlidingExpiration : TimeSpan.FromSeconds(seconds)), CacheItemPriority.Default, 54null); 55} 56 57///<summary> 58/// 获取缓存对象 59///</summary> 60publicstaticobject Get(string key) 61{ 62return GetPrivate(key); 63} 64 65///<summary> 66/// 判断缓存中是否含有缓存该键 67///</summary> 68publicstaticbool Exists(string key) 69{ 70return (GetPrivate(key) != null); 71} 72 73///<summary> 74/// 移除缓存对象 75///</summary> 76///<param name="key"></param> 77///<returns></returns> 78publicstaticbool Remove(string key) 79{ 80if (string.IsNullOrEmpty(key)) 81{ 82returnfalse; 83} 84HttpRuntime.Cache.Remove(key); 85returntrue; 86} 87 88///<summary> 89/// 移除所有缓存 90///</summary> 91///<returns></returns> 92publicstaticbool RemoveAll() 93{ 94 IDictionaryEnumerator iDictionaryEnumerator = HttpRuntime.Cache.GetEnumerator(); 95while (iDictionaryEnumerator.MoveNext()) 96{ 97HttpRuntime.Cache.Remove(Convert.ToString(iDictionaryEnumerator.Key)); 98} 99returntrue; 100} 101102103///<summary>104/// 尝试获取缓存,不存在则执行匿名委托 105///</summary>106///<typeparam name="T"></typeparam>107///<param name="key"></param>108///<param name="func"></param>109///<returns></returns>110publicstatic T TryGet<T>(string key, Func<T> func) 111{ 112if (Exists(key)) return (T) Get(key); 113var result = func.Invoke(); 114Set(key, result); 115return result; 116} 117118119120//------------------------提供给上面方法进行调用-----------------------------------121///<summary>122/// 设置缓存 123///</summary>124publicstaticbool Set(string key, object value, CacheDependency cacheDependency, DateTime dateTime, 125TimeSpan timeSpan, CacheItemPriority cacheItemPriority, CacheItemRemovedCallback cacheItemRemovedCallback) 126{ 127if (string.IsNullOrEmpty(key) || value == null) 128{ 129returnfalse; 130} 131HttpRuntime.Cache.Insert(key, value, cacheDependency, dateTime, timeSpan, cacheItemPriority, 132cacheItemRemovedCallback); 133returntrue; 134} 135136///<summary>137/// 获取缓存 138///</summary>139privatestaticobject GetPrivate(string key) 140{ 141returnstring.IsNullOrEmpty(key) ? null : HttpRuntime.Cache.Get(key); 142} 143 }
原文:http://www.cnblogs.com/creater/p/6321999.html
内容总结
以上是互联网集市为您收集整理的基础缓存操作类全部内容,希望文章能够帮你解决基础缓存操作类所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。