博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
web 编程中服务器cache缓存技术的应用
阅读量:4042 次
发布时间:2019-05-24

本文共 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/

你可能感兴趣的文章
2012年02月21日的日记
查看>>
iSecret 简介
查看>>
iSecret 1.0 正式上线啦!
查看>>
iSecret 1.0 销量尚可 希望大家多…
查看>>
获取当前的内存占用 (纯Linux底层…
查看>>
UIView的深入研究《转》
查看>>
29句最常用的英语谚语——管理…
查看>>
iSecret 用户反馈专用
查看>>
NSDateComponents 的基本使用以及…
查看>>
我的处女贴: UITableView reloadDa…
查看>>
Operation Queue(Obj-C中并发的…
查看>>
iOS 开发资源汇总《转》
查看>>
iOS push 相关知识备忘
查看>>
Mysql导入导出.sql文件《转》
查看>>
Mysql for MacOSX 安装和基本操作
查看>>
MAC 系统的启动过程和系统启动时运…
查看>>
svn 常用命令《转》
查看>>
Linux中的环境设置PATH与exp…
查看>>
Improved logging in Objective-C …
查看>>
ShareKit 分享到FaceBook, Twitter…
查看>>