說明:不是很理解併發和緩存邏輯的朋友,請務必自己敲一下代碼,好好理解一下,,老司機不用多說了,,拿來就可以用。 ————————————————————— 華麗的分割線——————————————————————————— public static class CustomCache { priv ...
說明:不是很理解併發和緩存邏輯的朋友,請務必自己敲一下代碼,好好理解一下,,老司機不用多說了,,拿來就可以用。
————————————————————— 華麗的分割線———————————————————————————
public static class CustomCache
{
private static List<ConcurrentDictionary<string, DataModel>> cacheContainer = null;
private static readonly int cpuCount = Environment.ProcessorCount;//獲取CPU個數
static CustomCache()
{
cacheContainer = new List<ConcurrentDictionary<string, DataModel>>();
for (int i = 0; i < cpuCount; i++)
{
cacheContainer.Add(new ConcurrentDictionary<string, DataModel>());
}
AutoClaerCache(900000);//每隔15分鐘自動清理
}
/// <summary>
/// 自動清理緩存
/// </summary>
/// <param name="intervalTime">間隔時間 毫秒為單位</param>
private static void AutoClaerCache(int intervalTime)
{
Task.Factory.StartNew(() =>
{
while (true)
{
Thread.Sleep(intervalTime);
for (int i = 0; i < cacheContainer.Count; i++)
{
var cache = cacheContainer[i].Where(w => w.Value.LifeCycleType == LifeCycle.Temp);
foreach (var item in cache)
{
if (DateTime.Now > item.Value.ExpirationTime)
{
cacheContainer[i].TryRemove(item.Key, out DataModel dataModel);
}
}
}
}
});
}
/// <summary>
/// 添加緩存
/// </summary>
/// <param name="key">鍵</param>
/// <param name="keyValue">鍵值</param>
public static void Add(string key, object keyValue)
{
if (ExistsCacheData(key, out int i))
{
cacheContainer[i][key] = new DataModel { LifeCycleType = LifeCycle.Lasting, CacheData = keyValue };
}
else
{
cacheContainer[Math.Abs(key.GetHashCode() % cpuCount)].TryAdd(key, new DataModel { LifeCycleType = LifeCycle.Lasting, CacheData = keyValue });
}
}
/// <summary>
/// 添加緩存
/// </summary>
/// <param name="key">鍵</param>
/// <param name="keyValue">鍵值</param>
/// <param name="timeOut">超時 分鐘為單位</param>
public static void Add(string key, object keyValue, int timeOut)
{
if (ExistsCacheData(key, out int i))
{
cacheContainer[i][key] = new DataModel { LifeCycleType = LifeCycle.Temp, ExpirationTime = DateTime.Now.AddMinutes(timeOut), CacheData = keyValue };
}
else
{
cacheContainer[Math.Abs(key.GetHashCode() % cpuCount)].TryAdd(key, new DataModel { LifeCycleType = LifeCycle.Temp, ExpirationTime = DateTime.Now.AddMinutes(timeOut), CacheData = keyValue });
}
}
/// <summary>
/// 刪除緩存
/// </summary>
/// <param name="key">鍵</param>
public static void Remove(string key)
{
cacheContainer[Math.Abs(key.GetHashCode() % cpuCount)].TryRemove(key, out DataModel dataModel);
}
/// <summary>
/// 獲取緩存數據
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key">鍵</param>
/// <param name="func">委托</param>
/// <param name="timeOut">超時時間 分鐘為單位 預設為0 則 持久</param>
/// <returns></returns>
public static T GetCacheData<T>(string key, Func<T> func, int timeOut = 10)
{
if (ExistsCacheData(key, out int i))
{
var temp = cacheContainer[i][key];
bool flag = temp.CacheData.GetType() == typeof(T);//判斷轉換類型是否一致
if (flag)
{
return (T)temp.CacheData;
}
}
var data = func.Invoke();
if (timeOut > 0) Add(key, data, timeOut); else Add(key, data);
return data;
}
/// <summary>
/// 檢查緩存數據
/// </summary>
/// <param name="key">鍵</param>
/// <param name="containerIndex">容器索引</param>
/// <returns></returns>
public static bool ExistsCacheData(string key, out int containerIndex)
{
containerIndex = Math.Abs(key.GetHashCode() % cpuCount);
if (cacheContainer[containerIndex].ContainsKey(key))
{
var cache = cacheContainer[containerIndex][key];
if (cache.LifeCycleType == LifeCycle.Temp && DateTime.Now > cache.ExpirationTime)
{
cacheContainer[containerIndex].TryRemove(key, out DataModel dataModel);
return false;
}
return true;
}
return false;
}
}
internal class DataModel
{
/// <summary>
/// 生命周期類型
/// </summary>
public LifeCycle LifeCycleType { get; set; }
/// <summary>
/// 到期時間
/// </summary>
public DateTime ExpirationTime { get; set; }
/// <summary>
/// 緩存數據
/// </summary>
public object CacheData { get; set; }
}
/// <summary>
/// 生命周期枚舉
/// </summary>
internal enum LifeCycle
{
Lasting = 0,//持久
Temp = 1//臨時
}
————————————————————— 華麗的分割線———————————————————————————