Asp.Net operation class example details

  • 2021-01-06 00:31:11
  • OfStack

This paper describes the structure and implementation method of cache cache in detail with an example code of Cache operation class of Asp.Net. The complete code is shown as follows:


/// <head>
/// <function>
///   Storage class ( storage UserInfo information )
/// </function>
/// <description>
///   with Cache Storing user information 
///   At specified intervals (TimeOut) If you take it inside, you can take it from Cache To take, 
///   If the storage time runs out , Fetches user information data from the database 
///   � all user information storage � � .
/// </description>
/// <author>
///  <name>ChengKing</name>  
/// </author>
/// </head>
using System;
using System.Web;
using System.Web.Caching;
namespace Common
{   
 /// <summary>
 ///  Storage class ( storage UserInfo information )
 /// </summary>
 public class Storage
 {
 public Storage()
 {
  //
  // TODO:  Add the constructor logic here 
  //
 }
 #region  methods 
 // To achieve" 1 key 1 Value "storage method , The most common storage method   
        // (" 1 key 1 Value "refers to 1 a Identify storage 1 A value , There are 1 A" 1 Keymany-valued "methods, because sometimes you need them 1 A key to store multiple variable object values. 
        public static bool InsertIdentify(string strIdentify,object Info)
 {  
  if(strIdentify != null && strIdentify.Length != 0 && userInfo != null)
  {
  // Establishes a callback delegate 1 An instance 
    CacheItemRemovedCallback callBack =new CacheItemRemovedCallback(onRemove);
  // In order to Identify For the mark, will userInfo deposit 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;
  }
 }
 // Judgment stored "1 key 1 value " Whether the value still exists (has it expired or never been stored) 
      public static bool ExistIdentify(string strIdentify)
 {
  if(HttpContext.Current.Cache[strIdentify] != null)
  {
  return true;
  }
  else
  {
  return false;
  }
 }
 // insert "1 Key value more " methods 
 //*** Among them  StorageInfType is 1 a Enum, Inside there 3 type : UserInf SysInf PageInf 
 // The enumeration is as follows :
 /*
      public enum StorageInfType
      {
    /// <summary> The user information </summary>
      UserInf = 0,
    /// <summary> Page information </summary>
    PageInf = 1, 
    /// <summary> System information </summary>
        SysInf = 2
       }
        // This enumeration is self-defined . Different enumerations can be defined as needed  
        // The purpose of this enumeration is to implement the 1 Key multi-value "storage method, in fact Cache Is a class that holds multiple variables, but is encapsulated by this class. 
        // Programmers feel like," 1 key 1 Value" .   The goal is to simplify development operations , Otherwise, if the programmer wants to store several variables, he has to define several Identify.
 public static bool InsertCommonInf(string strIdentify,StorageInfType enumInfType,object objValue)
 {  
  if(strIdentify != null && strIdentify != "" && strIdentify.Length != 0 && objValue != null)
  {
  //RemoveCommonInf(strIdentify,enumInfType); 
  // Establishes a callback delegate 1 An instance 
        CacheItemRemovedCallback callBack =new CacheItemRemovedCallback(onRemove);
  if(enumInfType == StorageInfType.UserInf)
  {  
    // To the user UserID+ Information signs (StorageInfType The enumeration ) That will be userInfo deposit Cache
    HttpContext.Current.Cache.Insert(strIdentify+StorageInfType.UserInf.ToString(),objValue,null, 
         System.DateTime.Now.AddSeconds(18000),    // Unit s 
         System.Web.Caching.Cache.NoSlidingExpiration, 
         System.Web.Caching.CacheItemPriority.Default,
         callBack); 
  }
  if(enumInfType == StorageInfType.PageInf)
  {
   // To the user UserID+ Information signs (StorageInfType The enumeration ) That will be PageInfo deposit 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)
  {
   // To the user UserID+ Information signs (StorageInfType The enumeration ) That will be SysInfo deposit 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;
 }
        // Read" 1 Key value" Identify The value of the 
        public static bool ReadIdentify(string strIdentify,out UserInfo userInfo)
 {
  // Take out the values 
  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;
  }  
 }
 // Manual removal" 1 key 1 Value "corresponds to the value of 
        public static bool RemoveIdentify(string strIdentify)
 {
  // Take out the values 
  if((UserInfo)HttpContext.Current.Cache[strIdentify] != null)
  {
  HttpContext.Current.Cache.Remove(strIdentify);    
  }  
  return true; 
 }
        // This method is called before the value is invalidated and can be used to update the database before the value is invalidated or to retrieve data from the database 
 private static void onRemove(string strIdentify, object userInfo,CacheItemRemovedReason reason)
 {
 }
 #endregion
 } 
}


Related articles: