★(ChengKing)【夜战鹰】【庖丁解牛—纵向切入Asp.net 3.5控件和组件开发技术】★

[首页♂] 【夜战鹰的博客】【专注于DotNet技术】【ChengKing(ZhengJian)】

原创 Asp.Net中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
 } 
}


谢谢!

发表于 @ 2005年10月03日 13:49:00|评论(loading...)

新一篇: 自定义带结构的可序列化数据集DataSet. | 旧一篇: 自定义数据库物理表中各列字段的名称

用户操作
[即时聊天] [发私信] [加为好友]
【郑健(ChengKing)】【夜战鹰】
订阅我的博客
XML聚合  FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
【郑健(ChengKing)】【夜战鹰】的公告
点击阅读《庖丁解牛:纵向切入Asp.net 3.5控件和组件开发技术》一书内容

【夜战鹰的博客】
【专注于.Net技术】【.Net控件开发】
技术交流/合作联系方式:
【MSN/Email:King.Zheng@hotmail.com】

【下面是快速分类链接, 若想查看本Blog所有文章可以点击下面[文章]区域块的按技术分类的各个链接】


(1).★★★【庖丁解牛:纵向切入Asp.net 3.5控件和组件开发技术系列教程】★★★    问问题

(2). Net 教程系列(Asp.net/
Ajax/JQuery/SilverLight/控件开发等)


(3)Asp.net控件/组件开发

(4). NET系列丛书

(5). C# GAME

(6). 网站(学习/源代码资源下载)

(7). 用友软件园工作环境

(8). 我上传的资源下载

(9). 《每天花五分钟时间赚点钱》

(2006~2009 MS-MVP)


请选择下面链接,可以分类别的阅读文章:

文章分类
收藏
    Good Blogs
    BennyMavis(水晶报表)(RSS)
    Bruce Zhang(RSS)
    cityhunter172(RSS)
    Clingingboy(RSS)
    dudu(RSS)
    goody9807(RSS)
    huangkw007(RSS)
    LoveCherry(RSS)
    mapserver(RSS)
    Rickie Lee(RSS)
    saucer(RSS)
    Scott Guthrie 博客中文版[CSDN,译者思归](RSS)
    Scott Guthrie 博客中文版[博客堂](RSS)
    Silverlight上海团队(RSS)
    singlepine(RSS)
    TerryLee(RSS)
    zjcxc(RSS)
    中国龙(RSS)
    冰冰子(RSS)
    叶帆(刘洪峰)(RSS)
    吕震宇(RSS)
    孟子E章(.net)(RSS)
    孟宪会(RSS)
    孟岩(RSS)
    寒羽枫(RSS)
    张友邦[架构专家](RSS)
    推荐系列(自TerryLee)(RSS)
    曾登高(RSS)
    朱春雷[软件工程](RSS)
    李宁(RSS)
    李维(RSS)
    清清月儿[黄鸣](RSS)
    王洪伟(SOA专家)(RSS)
    王磊(webabcd)(RSS)
    白慧冬(青润)[软件工程](RSS)
    胡百敬(RSS)
    蝈蝈俊.net(RSS)
    袁峰(RSS)
    谭振林(RSS)
    赵劼(RSS)
    速马(RSS)
    郑昀(RSS)
    陈黎夫(RSS)
    陕北吴旗娃(RSS)
    青润意愿(RSS)
    飞驰之剑(RSS)
    马宁(RSS)
    高阳(RSS)
    Good Sites
    4guysfromrolla.com(RSS)
    asp.net(RSS)
    aspalliance.com(RSS)
    beansoftware.com(RSS)
    codeplex.com(RSS)
    codeproject.com(RSS)
    c-sharpcorner.com(RSS)
    dotnetbips.com(RSS)
    dotnetslackers.com(RSS)
    hookedonlinq.com(RSS)
    MSDN Code Gallery(RSS)
    msdn en(RSS)
    Nikhil Kothari(RSS)
    Scott Guthrie(RSS)
    sourceforge.net(RSS)
    其它链接
    2006年100首好歌(RSS)
    2006年十二星座运程目录(RSS)
    2006年十二生肖运程运势(RSS)
    地图(RSS)
    社区/官方文档
    CSDN社区(RSS)
    MSDN网站(RSS)
    博客堂(RSS)
    微软Asp.net 2.0官方网站(RSS)
    微软Visual Studio 专区(RSS)
    微软中国社区(RSS)
    微软中文技术社区(RSS)
    文章链接
    C# Excel报表(RSS)
    CSDN新版图形验证码初级识别 (RSS)
    JavaScript技巧(一)(RSS)
    JavaScript技巧(二)(RSS)
    MINE类型(RSS)
    Share Point(RSS)
    多级下拉菜单(singlepine)(RSS)
    星月同辉 e路随行(文件下载)(RSS)
    我的其它Blog
    My IT博客网 Blog(RSS)
    My 博客园 Blog(RSS)
    My 博客园 blog(RSS)
    学习站点
    ASP.NET架构师[群组](RSS)
    Asp.net频道专家圈(RSS)
    King's CSDN下载资源(RSS)
    Microsoft XNA游戏开发网(RSS)
    象棋研究(RSS)
    优秀团队列表
    .Net 商业智能(RSS)
    .NetFramework3.0&WinFX团队(RSS)
    .NET控件与组件开发(RSS)
    ASP.NET AJAX学习(RSS)
    Asp.net(C#)四层架构自助建站系统(RSS)
    Design&Pattern团队(RSS)
    Dot Net Web服务和Windows服务开发(RSS)
    DotNet频道→ASP.NET(RSS)
    Enterprise Library(RSS)
    O/R Mapping团队(RSS)
    SharePoint团队(RSS)
    Skin设计小组(RSS)
    SOA研究室(RSS)
    Windows Mobile 应用开发(RSS)
    博客园培训团队(RSS)
    开源项目团队(RSS)
    自动识别与数据采集(RSS)
    计算机图形学(RSS)
    存档
    Csdn Blog version 3.1a
    Copyright © 【郑健(ChengKing)】【夜战鹰】