.net简单的缓存类
来源:互联网 发布:mac运行32位软件 编辑:程序博客网 时间:2024/06/06 03:16
接着上一篇内容,下面处理webapi宿主到windows服务中时,简单的缓存类处理:
using System;using System.Collections;using System.Web;using System.Web.Caching;namespace aaaaa.Api.Business{ /// <summary> /// 缓存类 /// </summary> public class CacheHelper { /// <summary> /// 增加一个缓存对象 /// </summary> /// <param name="strKey">键值名称</param> /// <param name="valueObj">被缓存对象</param> /// <param name="durationMin">缓存失效时间(默认为5分钟)</param> /// <param name="cachePriority">保留优先级(枚举数值)</param> /// <returns>缓存写入是否成功true 、false</returns> public static bool InsertCach(string strKey, object valueObj, int durationMin, CacheItemPriority cachePriority = CacheItemPriority.Default) { TimeSpan ts; if (!string.IsNullOrWhiteSpace(strKey) && valueObj != null) { //onRemove是委托执行的函数,具体方法看下面的onRemove(...) CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove); ts = durationMin == 0 ? new TimeSpan(0, 5, 0) : new TimeSpan(0, durationMin, 0); //HttpContext.Current.Cache.Insert( HttpRuntime.Cache.Insert( strKey, valueObj, null, DateTime.Now.Add(ts), Cache.NoSlidingExpiration, cachePriority, callBack ); return true; } else { return false; } } /// <summary> /// 判断缓存对象是否存在 /// </summary> /// <param name="strKey">缓存键值名称</param> /// <returns>是否存在true 、false</returns> public static bool IsExist(string strKey) { //return HttpContext.Current.Cache[strKey] != null; return HttpRuntime.Cache.Get(strKey) != null; } /// <summary> /// 读取缓存对象 /// </summary> /// <param name="strKey">缓存键值名称</param> /// <returns>缓存对象,objec类型</returns> public static object GetCache(string strKey) { //if (HttpContext.Current.Cache[strKey] != null) if (IsExist(strKey)) { object obj = HttpRuntime.Cache.Get(strKey); return obj ?? null; } else { return null; } } /// <summary> /// 移除缓存对象 /// </summary> /// <param name="strKey">缓存键值名称</param> public static void Remove(string strKey) { //if (HttpContext.Current.Cache[strKey] != null) if (IsExist(strKey)) { HttpRuntime.Cache.Remove(strKey); } } /// <summary> /// 清除所有缓存 /// </summary> public static void Clear() { IDictionaryEnumerator enu = HttpRuntime.Cache.GetEnumerator(); while (enu.MoveNext()) { Remove(enu.Key.ToString()); } } public static CacheItemRemovedReason reason; /// <summary> /// 此方法在值失效之前调用,可以用于在失效之前更新数据库,或从数据库重新获取数据 /// </summary> /// <param name="strKey"></param> /// <param name="obj"></param> /// <param name="reason"></param> private static void onRemove(string strKey, object obj, CacheItemRemovedReason r) { reason = r; } //... }}
0 0
- .net简单的缓存类
- Asp.net缓存的简单使用
- PHP简单的缓存类
- .NET简单缓存的快速实现和分析
- 简单ASP.NET输出缓存
- asp.net的DataTable 缓存类。
- .Net 缓存代理类 的初步考虑
- asp.net的缓存
- ASP.NET的缓存
- ASP.NET的缓存
- ASP.NET的缓存
- asp.net的缓存
- ASP.NET的缓存
- 一个简单的页面缓存类
- 简单至极的 PHP 缓存类
- 简单实用的PHP文本缓存类
- 自定义简单的封装SharedPreferences缓存类
- 简单实用的ajax缓存类
- .net实战项目零基础学习视频教程
- jquery mobile按钮
- HDU 4049
- 定时发邮件
- EF CodeFirst 级联删除
- .net简单的缓存类
- 在vs2010下编译boost
- 多线程执行多类型任务(线程通信wait/notify(notifyAll)):生产者消费者模型(二)
- spring中使用logback日志组件替换log4j
- Android中判断输入的字符串是否为数字,可以解决如输入为点出现的问题
- Android最新组件RecyclerView,替代ListView
- VS2010与mysql
- STL源码之实现一个简易的Vector容器
- Dongle烧写模块重构(四)--用工厂模式将方案商从功能代码中抽离