181 lines
5.3 KiB
C#
181 lines
5.3 KiB
C#
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Linq;
|
|
|
|
namespace PCRobot.Utils
|
|
{
|
|
/// <summary>
|
|
/// 本地缓存
|
|
/// </summary>
|
|
public class CacheHelper
|
|
{
|
|
|
|
|
|
private static DateTime _nextRemoveDatetime = DateTime.Now.AddMinutes(30);
|
|
/// <summary>
|
|
/// 删除 过期
|
|
/// </summary>
|
|
private static void RemoveTimeOut()
|
|
{
|
|
if (DateTime.Now < _nextRemoveDatetime)
|
|
{
|
|
return;
|
|
}
|
|
var array = _caches.Values.ToArray().Where(w => w.ExpirationTime < DateTime.Now).ToArray();
|
|
foreach (var v in array)
|
|
{
|
|
_caches.TryRemove(v.Key, out _);
|
|
}
|
|
_nextRemoveDatetime = DateTime.Now.AddMinutes(30);
|
|
}
|
|
|
|
private class CacheItem
|
|
{
|
|
/// <summary>
|
|
/// 缓存KEY
|
|
/// </summary>
|
|
public string Key { get; set; }
|
|
/// <summary>
|
|
/// 过期时间
|
|
/// </summary>
|
|
public DateTime ExpirationTime { get; set; }
|
|
|
|
/// <summary>
|
|
/// 对象
|
|
/// </summary>
|
|
public object Object { get; set; }
|
|
}
|
|
private static readonly ConcurrentDictionary<string, CacheItem> _caches = new ConcurrentDictionary<string, CacheItem>();
|
|
|
|
|
|
/// <summary>
|
|
/// 重置缓存,让这个程序的缓存全部失效
|
|
/// </summary>
|
|
public static void ResetCache()
|
|
{
|
|
_caches.Clear();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 增加缓存
|
|
/// </summary>
|
|
/// <param name="key">key</param>
|
|
/// <param name="value">值</param>
|
|
/// <param name="time">值(秒)</param>
|
|
public static bool Add(string key, object value, int time = 60)
|
|
{
|
|
_caches.AddOrUpdate(key, (k) =>
|
|
{
|
|
return new CacheItem()
|
|
{
|
|
Key = k,
|
|
Object = value,
|
|
ExpirationTime = time < 0 ? DateTime.MaxValue : DateTime.Now.AddSeconds(time)
|
|
};
|
|
}, (k, v) =>
|
|
{
|
|
v.Object = value;
|
|
v.ExpirationTime = time < 0 ? DateTime.MaxValue : DateTime.Now.AddSeconds(time);
|
|
return v;
|
|
});
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取缓存数据(如果不存在返回默认值)
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <returns></returns>
|
|
public static T Get<T>(string key)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(key))
|
|
{
|
|
return default(T);
|
|
}
|
|
if (_caches.TryGetValue(key, out var result))
|
|
{
|
|
if (result.ExpirationTime < DateTime.Now)
|
|
{
|
|
_caches.TryRemove(key, out _);
|
|
return default(T);
|
|
}
|
|
return (T)result.Object;
|
|
}
|
|
return default(T);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取缓存
|
|
/// </summary>
|
|
/// <param name="key">key</param>
|
|
/// <returns></returns>
|
|
public static object GetValue(string key)
|
|
{
|
|
RemoveTimeOut();
|
|
if (string.IsNullOrWhiteSpace(key))
|
|
{
|
|
return null;
|
|
}
|
|
if (_caches.TryGetValue(key, out var result))
|
|
{
|
|
if (result.ExpirationTime < DateTime.Now)
|
|
{
|
|
_caches.TryRemove(key, out _);
|
|
return null;
|
|
}
|
|
return result.Object;
|
|
}
|
|
return null;
|
|
}
|
|
/// <summary>
|
|
/// 获取缓存,不存在则新增
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="key"></param>
|
|
/// <param name="defFunc"></param>
|
|
/// <param name="time"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="Exception"></exception>
|
|
public static T GetValueOrAdd<T>(string key, Func<T> defFunc, int time = 60) where T : class
|
|
{
|
|
if (string.IsNullOrWhiteSpace(key))
|
|
{
|
|
throw new Exception("缓存key不能为空");
|
|
}
|
|
if (string.IsNullOrWhiteSpace(key))
|
|
{
|
|
return null;
|
|
}
|
|
if (_caches.TryGetValue(key, out var result))
|
|
{
|
|
if (result.ExpirationTime > DateTime.Now)
|
|
{
|
|
_caches.TryRemove(key, out _);
|
|
var obj = defFunc();
|
|
Add(key, obj, time);
|
|
return obj;
|
|
}
|
|
return (T)result.Object;
|
|
}
|
|
else
|
|
{
|
|
var obj = defFunc();
|
|
Add(key, obj, time);
|
|
return obj;
|
|
}
|
|
}
|
|
/// <summary>
|
|
///移除缓存
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
public static void Remove(string key)
|
|
{
|
|
_caches.TryRemove(key, out _);
|
|
}
|
|
public static bool Exist(string key)
|
|
{
|
|
return _caches.ContainsKey(key);
|
|
}
|
|
}
|
|
} |