本文共 4957 字,大约阅读时间需要 16 分钟。
一、首先建立服务器缓存类
////// 服务器缓存帮助类 /// public class CacheHelper { ////// 缓存数据 /// /// 文件名(需要绝对路径) /// 数据 ///public static bool CacheDatas(string filename, object datas) { IFormatter fm = new BinaryFormatter(); try { Stream fStream = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.Write); fm.Serialize(fStream, datas); fStream.Flush(); fStream.Close(); return true; } catch (Exception ex) { return false; } } /// /// 读取缓存数据 /// /// 文件名 ///缓存数据 public static object ReadDatas(string filename) { IFormatter fm = new BinaryFormatter(); if (File.Exists(filename)) { //暂不进行时间过期判断 //if (DateTime.Now > File.GetCreationTime(filePath).AddMinutes(expires)) //{ // return null; //} try { Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read); object datas = fm.Deserialize(stream);//反序列化 return datas; } catch { return null; } } else { return null; } } ////// 创建缓存项的文件依赖 /// /// 缓存Key /// object对象 /// 文件绝对路径 public static void InsertFile(string key, object obj, string fileName) { //创建缓存依赖项 CacheDependency dep = new CacheDependency(fileName); //创建缓存 HttpContext.Current.Cache.Insert(key, obj, dep); } ////// 创建缓存项过期 /// /// 缓存Key /// object对象 public static void Insert(string key, object obj) { if (obj != null) { int expires = CommonHelper.GetInt(ConfigHelper.GetValue("TimeCache")); Insert(key, obj, expires); } } ////// 创建缓存项过期 /// /// 缓存Key /// object对象 /// 设置时间 public static void Insert(string key, object obj, int expires) { if (obj != null) { HttpContext.Current.Cache.Insert(key, obj, null, Cache.NoAbsoluteExpiration, new TimeSpan(0, expires, 0)); } } ////// 判断缓存对象是否存在 /// /// 缓存键值名称 ///是否存在true 、false public static bool IsExist(string strKey) { return HttpContext.Current.Cache[strKey] != null; } ////// 获取缓存对象 /// /// 缓存Key ///object对象 public static object GetCache(string key) { if (string.IsNullOrEmpty(key)) return null; if (ConfigHelper.GetValue("IsCache") == "false") { return null; } return HttpContext.Current.Cache.Get(key); } ////// 获取缓存对象 /// ///T对象 /// 缓存Key ///public static T Get (string key) { object obj = GetCache(key); return obj == null ? default(T) : (T)obj; } /// /// 移除指定数据缓存 /// public static void RemoveAllCache(string CacheKey) { System.Web.Caching.Cache _cache = HttpRuntime.Cache; _cache.Remove(CacheKey); } ////// 移除全部缓存 /// public static void RemoveAllCache() { System.Web.Caching.Cache _cache = HttpRuntime.Cache; IDictionaryEnumerator CacheEnum = _cache.GetEnumerator(); while (CacheEnum.MoveNext()) { _cache.Remove(CacheEnum.Key.ToString()); } } }
二、调用的例子
//上述函数的调用示例1:從數據庫表中取出的記錄存入緩存中 string fileName = HttpUtility.UrlEncode(Request.Url.ToString()) + ".cache"; //文件名 , .cache为自定义文件类型 if (CacheHelper.ReadDatas(fileName) == null)//如果没有该文件(从数据库表查询的记录)的缓存,就写入缓存 { SqlDataAdapter ad = new SqlDataAdapter("select * from employee_basic", "server=.;database=SZMMY_ERP;uid=sa;pwd=sa;"); DataSet ds = new DataSet(); ad.Fill(ds); dt = ds.Tables[0]; this.CacheDatas(fileName, dt);//写入缓存 } else { dt = (DataTable)cache.ReadDatas(fileName); //读取缓存数据,并转为datatable } //調用示例2:將list存入緩存,例如用戶的按鈕權限list ////// 将【操作按钮权限】保存在服务器缓存中,提高性能。这样就不用每次去数据库读 /// /// 用户主键 /// ///public void SetButtonPermission(string UserId, IList list) { CacheHelper.Insert( UserId, list); }
转载地址:http://snmdi.baihongyu.com/