204 lines
6.2 KiB
C#
204 lines
6.2 KiB
C#
|
using System;
|
|||
|
using System.Collections.Concurrent;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace Api.Framework.Tools
|
|||
|
{
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 自定义内存缓存助手
|
|||
|
/// </summary>
|
|||
|
public sealed class CacheTool
|
|||
|
{
|
|||
|
#region 单例模式
|
|||
|
//创建私有化静态obj锁
|
|||
|
private static readonly object _ObjLock = new object();
|
|||
|
//创建私有静态字段,接收类的实例化对象
|
|||
|
private static volatile CacheTool _CacheTool = null;
|
|||
|
//构造函数私有化
|
|||
|
private CacheTool() { }
|
|||
|
//创建单利对象资源并返回
|
|||
|
public static CacheTool GetSingleObj()
|
|||
|
{
|
|||
|
if (_CacheTool == null)
|
|||
|
{
|
|||
|
lock (_ObjLock)
|
|||
|
{
|
|||
|
if (_CacheTool == null)
|
|||
|
_CacheTool = new CacheTool();
|
|||
|
}
|
|||
|
}
|
|||
|
return _CacheTool;
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 缓存字典 => 【key|value|time】
|
|||
|
/// </summary>
|
|||
|
private static volatile ConcurrentDictionary<string, KeyValuePair<object, DateTime?>> _CacheDictionary = new ConcurrentDictionary<string, KeyValuePair<object, DateTime?>>();
|
|||
|
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 1.主动过期
|
|||
|
/// </summary>
|
|||
|
static CacheTool()
|
|||
|
{
|
|||
|
Task.Run(() =>
|
|||
|
{
|
|||
|
while (true)
|
|||
|
{
|
|||
|
Thread.Sleep(1000 * 60); //1分钟检查一次
|
|||
|
if (_CacheDictionary != null && _CacheDictionary.Keys.Count > 0)
|
|||
|
{
|
|||
|
var listKey = new List<string>();
|
|||
|
foreach (var key in _CacheDictionary.Keys)
|
|||
|
{
|
|||
|
listKey.Add(key);
|
|||
|
}
|
|||
|
foreach (var key in listKey)
|
|||
|
{
|
|||
|
var valueTime = _CacheDictionary[key];
|
|||
|
if (valueTime.Value < DateTime.Now)
|
|||
|
_CacheDictionary.TryRemove(key, out KeyValuePair<object, DateTime?> value);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 索引器
|
|||
|
/// </summary>
|
|||
|
/// <param name="key"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public object this[string key]
|
|||
|
{
|
|||
|
get => _CacheDictionary[key];
|
|||
|
set => _CacheDictionary[key] = new KeyValuePair<object, DateTime?>(value, null);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 设置相对过期缓存
|
|||
|
/// </summary>
|
|||
|
/// <param name="key">键</param>
|
|||
|
/// <param name="data">数据包</param>
|
|||
|
/// <param name="minute">相对过期时间</param>
|
|||
|
public void Set(string key, object data, int minute = 10)
|
|||
|
{
|
|||
|
// var v = string.Empty;
|
|||
|
// if (data.GetType() != typeof(String))
|
|||
|
// {
|
|||
|
//v = CsharpHttpHelper.HttpHelper.ObjectToJson();
|
|||
|
// }
|
|||
|
_CacheDictionary[key] = new KeyValuePair<object, DateTime?>(data, DateTime.Now.AddMinutes(minute));
|
|||
|
}
|
|||
|
|
|||
|
///// <summary>
|
|||
|
///// 设置绝对过期缓存
|
|||
|
///// </summary>
|
|||
|
///// <param name="key">键<</param>
|
|||
|
///// <param name="data">数据包</param>
|
|||
|
//public void Set(string key, object data)
|
|||
|
//{
|
|||
|
// this[key] = data; //_CacheDictionary[key] = new KeyValuePair<object, DateTime?>(data, null);
|
|||
|
//}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 通过key获取缓存value
|
|||
|
/// 2.被动过期
|
|||
|
/// </summary>
|
|||
|
/// <typeparam name="T"></typeparam>
|
|||
|
/// <param name="key"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public T Get<T>(string key)
|
|||
|
{
|
|||
|
if (Exist(key))
|
|||
|
{
|
|||
|
var valueTime = _CacheDictionary[key];
|
|||
|
return (T)valueTime.Key; //return (T)this[key];
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
return default(T);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 获取缓存个数
|
|||
|
/// </summary>
|
|||
|
/// <returns></returns>
|
|||
|
public int Count()
|
|||
|
{
|
|||
|
int count = 0;
|
|||
|
if (_CacheDictionary != null)
|
|||
|
count = _CacheDictionary.Count;
|
|||
|
return count;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 删除指定key的value
|
|||
|
/// </summary>
|
|||
|
/// <param name="key"></param>
|
|||
|
public bool Remove(string key)
|
|||
|
{
|
|||
|
for (int i = 0; i < 5; i++)
|
|||
|
{
|
|||
|
var flag = _CacheDictionary.TryRemove(key, out KeyValuePair<object, DateTime?> value);
|
|||
|
if (!flag) Thread.Sleep(100);
|
|||
|
else return true;
|
|||
|
}
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 清空所有缓存
|
|||
|
/// </summary>
|
|||
|
public void Cleaner()
|
|||
|
{
|
|||
|
if (_CacheDictionary != null && _CacheDictionary.Count > 0)
|
|||
|
{
|
|||
|
foreach (var key in _CacheDictionary.Keys)
|
|||
|
{
|
|||
|
_CacheDictionary.TryRemove(key, out KeyValuePair<object, DateTime?> value);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 检查key是否存在
|
|||
|
/// 2.被动过期,保证任何过期缓存都无法取值
|
|||
|
/// </summary>
|
|||
|
/// <param name="key"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public bool Exist(string key)
|
|||
|
{
|
|||
|
if (string.IsNullOrWhiteSpace(key))
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
else if (_CacheDictionary.ContainsKey(key))
|
|||
|
{
|
|||
|
var valTime = _CacheDictionary[key];
|
|||
|
if (valTime.Value != null && valTime.Value > DateTime.Now)
|
|||
|
{
|
|||
|
return true; //缓存没过期
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
_CacheDictionary.TryRemove(key, out KeyValuePair<object, DateTime?> value); //缓存过期清理
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
}
|