You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

214 lines
6.7 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Leit.FrameWork
{
public class CustomCache
{
/// <summary>
/// private 保护数据
/// static 全局唯一 不释放
/// Dictionary 保存多项数据
/// </summary>
private static Dictionary<string, CustomerCatchModel> CustomCacheDictionary;
//private static System.Collections.Concurrent.ConcurrentDictionary
/// <summary>
/// 主动清理:只要是过期,最多超过10分钟,一定会被清理
/// </summary>
static CustomCache()
{
CustomCacheDictionary = new Dictionary<string, CustomerCatchModel>();
Task.Run(
() => {
while (true)
{
LockAction(new Action(() =>
{
List<string> list = new List<string>();
foreach (var key in CustomCacheDictionary.Keys)
{
var valueTime = CustomCacheDictionary[key];
if (valueTime.TimeOut == 0)
{
//0表示永久保存
continue;
}
if ((DateTime.Now - valueTime.InsertTime).TotalSeconds > valueTime.TimeOut)//过期
{
//过期
list.Add(key);
}
}
list.ForEach(key => {
var timeoutCache = CustomCacheDictionary[key];
timeoutCache.CatchTimeoutClearnChanging?.Invoke(timeoutCache.Value);
CustomCacheDictionary.Remove(key);
timeoutCache.CatchTimeoutClearnChanged?.Invoke(timeoutCache.Value);
});
}));
///此处可以放在配置文件
Thread.Sleep(1000 * 60 * 1);//1分钟来一遍
}
}
);
}
/// <summary>
/// 添加数据 key重复会异常的
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public static void Add(string key, object value, Action<object> changingEvent, Action<object> changedEvent, int timeout = 0)
{
CustomerCatchModel customerCatchModel = new CustomerCatchModel();
customerCatchModel.TimeOut = timeout;
customerCatchModel.Value = value;
LockAction(new Action(() =>
{
customerCatchModel.InsertTime = DateTime.Now;
CustomCacheDictionary.Add(key, customerCatchModel);
}));
}
private static readonly object CustomCache_Lock = new object();
private static void LockAction(Action action)
{
lock (CustomCache_Lock)
{
action.Invoke();
}
}
/// <summary>
/// 保存数据,有就覆盖 没有就新增
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public static void SaveOrUpdate(string key, object value, Action<object> changingEvent, Action<object> changedEvent, int timeout = 0)
{
CustomerCatchModel customerCatchModel = new CustomerCatchModel();
customerCatchModel.TimeOut = timeout;
customerCatchModel.Value = value;
LockAction(new Action(() =>
{
customerCatchModel.InsertTime = DateTime.Now;
CustomCacheDictionary[key] = customerCatchModel;
}));
}
/// <summary>
/// 获取数据 没有会异常的
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public static T Get<T>(string key)
{
CustomCacheDictionary[key].InsertTime = DateTime.Now;
return (T)CustomCacheDictionary[key].Value;
}
/// <summary>
/// 获取数据 没有会异常的 不会更新有效期
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public static T GetOnly<T>(string key)
{
CustomCacheDictionary[key].InsertTime = DateTime.Now;
return (T)CustomCacheDictionary[key].Value;
}
/// <summary>
/// 检查是否存在
///
/// 清理一下,除非我们去访问这条缓存,才会去清理 被动清理,任何过期的数据,都不可以被查到
/// 可能有垃圾留在缓存里面
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static bool Exsit(string key)
{
if (CustomCacheDictionary.ContainsKey(key))
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 移除数据 没有会异常
/// </summary>
/// <param name="key"></param>
public static void Remove(string key)
{
LockAction(new Action(() =>
{
var timeoutCache = CustomCacheDictionary[key];
timeoutCache.CatchTimeoutClearnChanged = null;
timeoutCache.CatchTimeoutClearnChanged = null;
CustomCacheDictionary.Remove(key);
}));
}
public static void RemoveAll()
{
LockAction(new Action(() =>
{
CustomCacheDictionary.Clear();
}));
}
public static void RemoveCondition(Func<string, bool> func)
{
LockAction(new Action(() =>
{
List<string> list = new List<string>();
foreach (var key in CustomCacheDictionary.Keys)
{
if (func.Invoke(key))
{
list.Add(key);
}
}
list.ForEach(key => CustomCacheDictionary.Remove(key));
}));
}
public static List<CustomerCatchModel> GetAllCache()
{
return CustomCacheDictionary.Values.ToList<CustomerCatchModel>();
}
}
}