Asp.NetにおけるCache操作クラスの例の詳細

5119 ワード

本文は一つのAsp.NetのCacheはクラスインスタンスコードを操作してcacheキャッシュの構造と実現方法を詳しく説明し、完全なコードは以下の通りである.

/// 
/// 
///     (  UserInfo  )
/// 
/// 
///   Cache      
///       (TimeOut)  ,    Cache  ,
///          ,            
///   � �       ���.
/// 
/// 
///  ChengKing  
/// 
/// 
using System;
using System.Web;
using System.Web.Caching;
namespace Common
{   
 /// 
 ///    (  UserInfo  )
 /// 
 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
      {
    ///     
      UserInf = 0,
    ///     
    PageInf = 1, 
    ///     
        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
 } 
}