SoFunction
Updated on 2025-03-07

Detailed explanation of Cache operation class instance

This article uses a Cache operation class instance code to describe the structure and implementation method of cache cache in detail. The complete code is as follows:

/// <head>
/// <function>
/// Storage class (storing UserInfo information)/// </function>
/// <description>
/// Use Cache to store user information/// If you take it within the specified interval (TimeOut), you can take it from the cache./// If the storage time is exceeded, the user information data will be retrieved from the database/// As a storage type of user information./// </description>
/// <author>
///  <name>ChengKing</name>  
/// </author>
/// </head>
using System;
using ;
using ;
namespace Common
{   
 /// <summary>
 /// Storage class (storing UserInfo information) /// </summary>
 public class Storage
 {
 public Storage()
 {
  //
  // TODO: Add constructor logic here  //
 }
 #region method //Implement the "one-key, one-value" storage method, the most common storage method        // ("One key, one value" means that an Identify stores a value, and there is also a "one key, multiple values" method below, because sometimes one key is needed to store multiple variable object values)        public static bool InsertIdentify(string strIdentify,object Info)
 {  
  if(strIdentify != null &&  != 0 && userInfo != null)
  {
  // Create an instance of callback delegation  CacheItemRemovedCallback callBack =new CacheItemRemovedCallback(onRemove);
  //Save userInfo into Cache with Identify as the flag  (strIdentify,userInfo,null, 
  (300),
  , 
  ,
  callBack);
  return true;
  }  
  else
  {
  return false;
  }
 }
 //Judge whether the stored "one-key-one value" value still exists (has it expired or has it never been stored)      public static bool ExistIdentify(string strIdentify)
 {
  if([strIdentify] != null)
  {
  return true;
  }
  else
  {
  return false;
  }
 }
 //Insert "one-key multi-value" method //***The StorageInfType is an Enum, which contains three types: UserInf SysInf PageInf //This enumeration is as follows: /*
      public enum StorageInfType
      {
    /// <summary>User Information</summary>      UserInf = 0,
    /// <summary>Page Information</summary>    PageInf = 1, 
    /// <summary>System Information</summary>        SysInf = 2
       }
        //This enum is defined by yourself. Different enums can be defined as needed.        //The purpose of adding an enum is to implement the "one-click and multiple-value" storage method. In fact, multiple variables are stored in the cache, but they are encapsulated by this class.        //The programmer feels like "one key, one value". The purpose of this is to simplify development operations, otherwise the programmer has to define several Identify if he wants to store several variables. public static bool InsertCommonInf(string strIdentify,StorageInfType enumInfType,object objValue)
 {  
  if(strIdentify != null &amp;&amp; strIdentify != "" &amp;&amp;  != 0 &amp;&amp; objValue != null)
  {
  //RemoveCommonInf(strIdentify,enumInfType); 
  // Create an instance of callback delegation      CacheItemRemovedCallback callBack =new CacheItemRemovedCallback(onRemove);
  if(enumInfType == )
  {  
    //Save userInfo into Cache with user UserID+information flag (StorageInfType enumeration).    (strIdentify+(),objValue,null, 
   (18000),    //Units of seconds   , 
   ,
   callBack); 
  }
  if(enumInfType == )
  {
   //Save PageInfo into Cache with UserID+Information Flag (StorageInfType Enumeration).     (strIdentify+(),objValue,null, 
    (18000),
    , 
    ,
    callBack); 
  }
  if(enumInfType == )
  {
   //Save SysInfo into Cache with the user UserID+information flag (StorageInfType enumeration).   (strIdentify+(),objValue,null, 
    (18000),
     , 
    ,
    callBack); 
  }
  return true;
  }
  return false;
 }
        //Read the value of "one key and multiple values" Identify        public static bool ReadIdentify(string strIdentify,out UserInfo userInfo)
 {
  //Take out the value  if((UserInfo)[strIdentify] != null)
  {
  userInfo = (UserInfo)[strIdentify];
  if(userInfo == null)
  {
   return false;
  }
  return true;
  }  
  else
  {
  userInfo = null;
  return false;
  }  
 }
 //Manually remove the corresponding value of "one key, one value"        public static bool RemoveIdentify(string strIdentify)
 {
  //Take out the value  if((UserInfo)[strIdentify] != null)
  {
  (strIdentify);    
  }  
  return true; 
 }
        //This method is called before the value expires and can be used to update the database before it expires, or to re-get data from the database private static void onRemove(string strIdentify, object userInfo,CacheItemRemovedReason reason)
 {
 }
 #endregion
 } 
}