当前位置: 移动技术网 > IT编程>开发语言>.net > Asp.Net中Cache操作类实例详解

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

2017年12月12日  | 移动技术网IT编程  | 我要评论

袁岳的老婆,超级QQ怎么开通,piaoshihou

本文以一个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
 } 
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网