文件緩存,php文件緩存
緩存存儲在文件中,根據過期時間過期,也可以手動刪除。IIS回收進程時緩存不丟失。
代碼:

![]()
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>
/// 緩存工具類
/// </summary>
public static class CacheUtil
{
#region 變量
/// <summary>
/// 緩存路徑
/// </summary>
private static string folderPath = Application.StartupPath + "\\cache";
/// <summary>
/// 鎖
/// </summary>
private static object _lock = new object();
private static BinaryFormatter formatter = new BinaryFormatter();
#endregion
#region 構造函數
static CacheUtil()
{
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
}
#endregion
#region SetValue 保存鍵值對
/// <summary>
/// 保存鍵值對
/// </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 獲取鍵值對
/// <summary>
/// 獲取鍵值對
/// </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>
/// 刪除
/// </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 全部刪除
/// <summary>
/// 全部刪除
/// </summary>
public static void DeleteAll()
{
string[] files = Directory.GetFiles(folderPath);
foreach (string file in files)
{
File.Delete(file);
}
}
#endregion
#region 計算MD5值
/// <summary>
/// 計算MD5值
/// </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 緩存數據
/// <summary>
/// 緩存數據
/// </summary>
[Serializable]
public class CacheData
{
/// <summary>
/// 鍵
/// </summary>
public string key { get; set; }
/// <summary>
/// 值
/// </summary>
public object value { get; set; }
/// <summary>
/// 緩存更新時間
/// </summary>
public DateTime updateTime { get; set; }
/// <summary>
/// 過期時間(分鐘),0表示永不過期
/// </summary>
public int expirationMinutes { get; set; }
public CacheData(string key, object value)
{
this.key = key;
this.value = value;
}
}
#endregion
}
View Code