/// <Head>
/// <Function>
/// Storage Class (storing UserInfo Information)
/// </Function>
/// <Description>
/// Use Cache to store user information
/// Obtain from the Cache at the specified interval (TimeOut,
/// If the storage time exceeds the limit, the user information data is retrieved from the database.
/// Save all user information.
/// </Description>
/// <Author>
/// <Name> ChengKing </name>
/// </Author>
/// </Head>
Using System;
Using System. Web;
Using System. Web. Caching;
Namespace Common
{
/// <Summary>
/// Storage Class (storing UserInfo Information)
/// </Summary>
Public class Storage
{
Public Storage ()
{
//
// TODO: add the constructor logic here
//
}
# Region Method
// Implement the "one-click one-value" storage method, the most common storage method
// ("One-click one value" means that one Identify stores one value. There is also a "one-click multi-value" method, because sometimes one key is required to store multiple variable object values)
Public static bool InsertIdentify (string strIdentify, object Info)
{
If (strIdentify! = Null & strIdentify. Length! = 0 & userInfo! = Null)
{
// Create an instance of the callback delegate
CacheItemRemovedCallback callBack = new CacheItemRemovedCallback (onRemove );
// Save userInfo to Cache with Identify as the flag
HttpContext. Current. Cache. Insert (strIdentify, userInfo, null,
System. DateTime. Now. addsecondds (300 ),
System. Web. Caching. Cache. NoSlidingExpiration,
System. Web. Caching. CacheItemPriority. Default,
CallBack );
Return true;
}
Else
{
Return false;
}
}
// Determine whether the stored "one-click value" still exists (whether it expires or has never been stored)
Public static bool ExistIdentify (string stridenfy)
{
If (HttpContext. Current. Cache [strIdentify]! = Null)
{
Return true;
}
Else
{
Return false;
}
}
// Insert the "one-key multi-value" Method
// *** 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 enumeration is defined by yourself. You can define different enumerations as needed.
// Add an enumeration to implement the "one-click multi-value" storage method. In fact, the Cache stores multiple variables, but is encapsulated by this class,
// The programmer feels like a "one-click value". This can simplify development operations. Otherwise, the programmer must define several Identify to store several variables.
Public static bool InsertCommonInf (string strideninf, StorageInfType enumInfType, object objValue)
{
If (strIdentify! = Null & strIdentify! = "" & StrIdentify. Length! = 0 & objValue! = Null)
{
// RemoveCommonInf (strideninf, enumInfType );
// Create an instance of the callback delegate
CacheItemRemovedCallback callBack = new CacheItemRemovedCallback (onRemove );
If (enumInfType = StorageInfType. UserInf)
{
// Store userInfo into the Cache using the UserID + Information Mark (StorageInfType enumeration)
HttpContext. Current. Cache. Insert (strIdentify + StorageInfType. UserInf. ToString (), objValue, null,
System. DateTime. Now. AddSeconds (18000), // unit: seconds
System. Web. Caching. Cache. NoSlidingExpiration,
System. Web. Caching. CacheItemPriority. Default,
CallBack );
}
If (enumInfType = StorageInfType. PageInf)
{
// Saves PageInfo to the Cache using the UserID + Information Mark (StorageInfType enumeration)
HttpContext. Current. Cache. Insert (strIdentify + StorageInfType. PageInf. ToString (), objValue, null,
System. DateTime. Now. addsecondds (18000 ),
System. Web. Caching. Cache. NoSlidingExpiration,
System. Web. Caching. CacheItemPriority. Default,
CallBack );
}
If (enumInfType = StorageInfType. SysInf)
{
// Store SysInfo to the Cache using the UserID + Information Mark (StorageInfType enumeration)
HttpContext. Current. Cache. Insert (strIdentify + StorageInfType. SysInf. ToString (), objValue, null,
System. DateTime. Now. addsecondds (18000 ),
System. Web. Caching. Cache. NoSlidingExpiration,
System. Web. Caching. CacheItemPriority. Default,
CallBack );
}
Return true;
}
Return false;
}
// Read the value of "one-click multi-value" Identify
Public static bool ReadIdentify (string stridenfy, out UserInfo userInfo)
{
// Retrieve the value
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;
}
}
// Manually remove the value corresponding to one-click Value
Public static bool RemoveIdentify (string stridenfy)
{
// Retrieve the value
If (UserInfo) HttpContext. Current. Cache [strIdentify]! = Null)
{
HttpContext. Current. Cache. Remove (strIdentify );
}
Return true;
}
// This method is called before the value expires. It can be used to update the database or obtain data from the database before the value expires.
Private static void onRemove (string strIdentify, object userInfo, CacheItemRemovedReason reason)
{
}
# Endregion
}
}