Cache如何在Asp.Net中使用

发布时间:2020-12-09 17:57:46 作者:Leah
来源:亿速云 阅读:127

这期内容当中小编将会给大家带来有关Cache如何在Asp.Net中使用,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

使用方法如下

/// <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
 } 
}

上述就是小编为大家分享的Cache如何在Asp.Net中使用了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. 如如何使用journalctl命令?
  2. cache如何在Spring boot中使用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

asp.net cache

上一篇:如何正确的使用phpStudy 2016

下一篇:如何在PHP中正确的使用命名空间

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》