File Cache, PHP File Cache
The cache is stored in the file and can be deleted manually based on the expiration time. Cache is not lost when IIS recycles the process.
Code:
Using System; using System. collections. generic; using System. IO; using System. linq; using System. security. cryptography; using System. text; using System. threading. tasks; using System. windows. forms; using System. runtime. serialization. formatters. binary; using System. threading; namespace Common. utils {// <summary> // cache tool class // </summary> public static class CacheUtil {# region variable // <summary> // cache path/ // </s Ummary> private static string folderPath = Application. startupPath + "\ cache"; // <summary> // lock // </summary> private static object _ lock = new object (); private static BinaryFormatter formatter = new BinaryFormatter (); # endregion # region constructor static CacheUtil () {if (! Directory. exists (folderPath) {Directory. createDirectory (folderPath );}} # endregion # region SetValue Save the key-value Pair // <summary> // Save the key-value Pair /// </summary> public static void SetValue (string key, object value, int expirationMinutes = 0) {CacheData data = new CacheData (key, value); data. updateTime = DateTime. now; data. expirationMinutes = expirationMinutes; string keyMd5 = GetMD5 (key); string path = folderPath + "\" + keyMd5 + ". txt "; lock (_ lock) {using (FileStream fs = new FileStream (path, FileMode. openOrCreate, FileAccess. write) {fs. setLength (0); formatter. serialize (fs, data); fs. close ();}}} # endregion # region GetValue obtain the key-Value Pair /// <summary> /// obtain the key-Value Pair /// </summary> public static object GetValue (string key) {string keyMd5 = GetMD5 (key); string path = folderPath + "\" + keyMd5 + ". txt "; if (File. exists (path) {using (FileStream fs = new FileStream (path, FileMode. open, FileAccess. read) {CacheData data = (CacheData) formatter. deserialize (fs); fs. close (); if (data. expirationMinutes> 0 & DateTime. now. subtract (data. updateTime ). totalMinutes> data. expirationMinutes) {File. delete (path); return null;} return data. value ;}} return null ;}# endregion # region Delete /// <summary> /// Delete /// </summary> public static void Delete (string key) {string keyMd5 = GetMD5 (key); string path = folderPath + "\" + keyMd5 + ". txt "; if (File. exists (path) {lock (_ lock) {File. delete (path) ;}}# endregion # region DeleteAll Delete All /// <summary> // Delete All /// </summary> public static void DeleteAll () {string [] files = Directory. getFiles (folderPath); foreach (string file in files) {File. delete (file );}} # endregion # region calculates the MD5 value /// <summary> /// calculates the MD5 value /// </summary> private static string GetMD5 (string value) {string base64 = Convert. toBase64String (ASCIIEncoding. ASCII. getBytes (value )). replace ("/", "-"); if (base64.Length> 200) {MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider (); byte [] bArr = md5.ComputeHash (ASCIIEncoding. ASCII. getBytes (value); StringBuilder sb = new StringBuilder (); foreach (byte B in bArr) {sb. append (B. toString ("x2");} return sb. toString ();} return base64 ;} # endregion} # region CacheData cache data // <summary> // cache data /// </summary> [Serializable] public class CacheData {// <summary>/ // key /// </summary> public string key {get; set ;}//< summary> /// value ///</summary> public object value {get; set ;} /// <summary> /// cache Update Time /// </summary> public DateTime updateTime {get; set ;} /// <summary> /// expiration time (in minutes). 0 indicates never expiration. // </summary> public int expirationMinutes {get; set ;} public CacheData (string key, object value) {this. key = key; this. value = value ;}# endregion}View Code