Asp.Net中Cache操作类实例详解

所属分类: 网络编程 / ASP.NET 阅读数: 611
收藏 0 赞 0 分享

本文以一个Asp.Net的Cache操作类实例代码来详细描述了cache缓存的结构及实现方法,完整代码如下所示:

/// <head>
/// <function>
///  存储类(存储UserInfo信息)
/// </function>
/// <description>
///  用Cache存储用户信息
///  在指定间隔(TimeOut)内取,则可以从Cache中取,
///  如果超出存储时间,则从数据库取用户信息数据
///  作為所有用户信息的存儲類.
/// </description>
/// <author>
///  <name>ChengKing</name>  
/// </author>
/// </head>
using System;
using System.Web;
using System.Web.Caching;
namespace Common
{   
 /// <summary>
 /// 存储类(存储UserInfo信息)
 /// </summary>
 public class Storage
 {
 public Storage()
 {
  //
  // TODO: 在此处添加构造函数逻辑
  //
 }
 #region 方法
 //实现“一键一值”存储方法,最普通的存储方法  
        //(“一键一值”指一个Identify存储一个值,下面还有一个“一键多值”方法,因为有时候需要一个键存储多个变量对象值)
        public static bool InsertIdentify(string strIdentify,object Info)
 {  
  if(strIdentify != null && strIdentify.Length != 0 && userInfo != null)
  {
  //建立回调委托的一个实例
    CacheItemRemovedCallback callBack =new CacheItemRemovedCallback(onRemove);
  //以Identify为标志,将userInfo存入Cache
  HttpContext.Current.Cache.Insert(strIdentify,userInfo,null, 
        System.DateTime.Now.AddSeconds(300),
        System.Web.Caching.Cache.NoSlidingExpiration, 
        System.Web.Caching.CacheItemPriority.Default,
        callBack);
  return true;
  }  
  else
  {
  return false;
  }
 }
 //判断存储的"一键一值"值是否还存在(有没有过期失效或从来都未存储过)
      public static bool ExistIdentify(string strIdentify)
 {
  if(HttpContext.Current.Cache[strIdentify] != null)
  {
  return true;
  }
  else
  {
  return false;
  }
 }
 //插入"一键多值"方法
 //***其中 StorageInfType是一个Enum,里面存有三种类型: UserInf SysInf PageInf 
 //这个枚举如下:
 /*
      public enum StorageInfType
      {
    /// <summary>用户信息</summary>
      UserInf = 0,
    /// <summary>页面信息</summary>
    PageInf = 1, 
    /// <summary>系统信息</summary>
        SysInf = 2
       }
        //此枚举是自己定义的.可根据需要定义不同的枚举 
        //加个枚举目的是实现“一键多值”存储方法,事实上Cache中是存放了多个变量的,只不过被这个类封装了,
        //程序员感到就好像是“一键一值”.  这样做目的是可以简化开发操作,否则程序员要存储几个变量就得定义几个Identify.
 public static bool InsertCommonInf(string strIdentify,StorageInfType enumInfType,object objValue)
 {  
  if(strIdentify != null && strIdentify != "" && strIdentify.Length != 0 && objValue != null)
  {
  //RemoveCommonInf(strIdentify,enumInfType); 
  //建立回调委托的一个实例
        CacheItemRemovedCallback callBack =new CacheItemRemovedCallback(onRemove);
  if(enumInfType == StorageInfType.UserInf)
  {  
    //以用户UserID+信息标志(StorageInfType枚举),将userInfo存入Cache
    HttpContext.Current.Cache.Insert(strIdentify+StorageInfType.UserInf.ToString(),objValue,null, 
         System.DateTime.Now.AddSeconds(18000),    //单位秒
         System.Web.Caching.Cache.NoSlidingExpiration, 
         System.Web.Caching.CacheItemPriority.Default,
         callBack); 
  }
  if(enumInfType == StorageInfType.PageInf)
  {
   //以用户UserID+信息标志(StorageInfType枚举),将PageInfo存入Cache
     HttpContext.Current.Cache.Insert(strIdentify+StorageInfType.PageInf.ToString(),objValue,null, 
          System.DateTime.Now.AddSeconds(18000),
          System.Web.Caching.Cache.NoSlidingExpiration, 
          System.Web.Caching.CacheItemPriority.Default,
          callBack); 
  }
  if(enumInfType == StorageInfType.SysInf)
  {
   //以用户UserID+信息标志(StorageInfType枚举),将SysInfo存入Cache
   HttpContext.Current.Cache.Insert(strIdentify+StorageInfType.SysInf.ToString(),objValue,null, 
          System.DateTime.Now.AddSeconds(18000),
           System.Web.Caching.Cache.NoSlidingExpiration, 
          System.Web.Caching.CacheItemPriority.Default,
          callBack); 
  }
  return true;
  }
  return false;
 }
        //读取“一键多值”Identify的值
        public static bool ReadIdentify(string strIdentify,out UserInfo userInfo)
 {
  //取出值
  if((UserInfo)HttpContext.Current.Cache[strIdentify] != null)
  {
  userInfo = (UserInfo)HttpContext.Current.Cache[strIdentify];
  if(userInfo == null)
  {
   return false;
  }
  return true;
  }  
  else
  {
  userInfo = null;
  return false;
  }  
 }
 //手动移除“一键一值”对应的值
        public static bool RemoveIdentify(string strIdentify)
 {
  //取出值
  if((UserInfo)HttpContext.Current.Cache[strIdentify] != null)
  {
  HttpContext.Current.Cache.Remove(strIdentify);    
  }  
  return true; 
 }
        //此方法在值失效之前调用,可以用于在失效之前更新数据库,或从数据库重新获取数据
 private static void onRemove(string strIdentify, object userInfo,CacheItemRemovedReason reason)
 {
 }
 #endregion
 } 
}

更多精彩内容其他人还在看

asp.net 虚方法、抽象方法、接口疑问

asp.net 虚方法、抽象方法、接口疑问等说明。
收藏 0 赞 0 分享

c#  操作符?? null coalescing operator

?? "null coalescing" operator 是c#新提供的一个操作符,这个操作符提供的功能是判断左侧的操作数是否是null,如果是则返回结果是右侧的操作数;非null则返回左侧的操作数。
收藏 0 赞 0 分享

.net 反序题目的详细解答第1/2页

在各种答案,以及平时面试过程中,这道题总归会有一些非常典型的错误发生。其中给老赵的感觉也非常有意思,不知其中的“思路”是否如老赵猜测那样。
收藏 0 赞 0 分享

implicitly convert type 'int' to 'short'的原因与解决方法

implicitly convert type 'int' to 'short'的原因与解决方法
收藏 0 赞 0 分享

比较完整的 asp.net 学习流程

好多朋友想学习后台编程语言,但请注意的事,学习后台是个循序渐进的过程,不可能一下就到位,其实不只是asp.net其它的编程语言都需要下面的一些知识。
收藏 0 赞 0 分享

官网 Ext direct包中.NET版的问题

下载了官网的 Ext direct 包进行研究,发现服务器端返回结果存在一点小问题。
收藏 0 赞 0 分享

C# XML操作 代码大全(读XML,写XML,更新,删除节点,与dataset结合等)第1/2页

C#操作XML(读XML,写XML,更新,删除节点,与dataset结合等),以下就是操作XML的所有方法,相信可以满足很大一部份的使用了。
收藏 0 赞 0 分享

c# 连接字符串数据库服务器端口号 .net状态服务器端口号

正常的数据库连接字符串配置,这是在MSSQL服务器端口是1433(默认)的情况下。
收藏 0 赞 0 分享

ASP.NET 路径问题的解决方法

相对路径和绝对路径在ASP.NET中可以用~/来解决.
收藏 0 赞 0 分享

asp.net TemplateField模板中的Bind方法和Eval方法

在TemplateField模板中为了能够有限制的或者取出数据库中某列的值时,可以用Bind和Eval方法来实现。以下是Bind方法的格式,Eval的格式也是和Bind一样的。 Bind("列的名称","显示的格式文")
收藏 0 赞 0 分享
查看更多