基于.NET Standard 2.0标准库下实现C#内存缓存,实例如下:
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Common.Helper
{
///
/// 自定义内存缓存助手
///
public sealed class CustomCacheHelper
{
#region 单例模式
//创建私有化静态obj锁
private static readonly object _ObjLock = new object();
//创建私有静态字段,接收类的实例化对象
private static volatile CustomCacheHelper _CustomCacheHelper = null;
//构造函数私有化
private CustomCacheHelper() { }
//创建单利对象资源并返回
public static CustomCacheHelper GetSingleObj()
{
if (_CustomCacheHelper == null)
{
lock (_ObjLock)
{
if (_CustomCacheHelper == null)
_CustomCacheHelper = new CustomCacheHelper();
}
}
return _CustomCacheHelper;
}
#endregion
///
/// 缓存字典 => 【key|value|time】
///
private static volatile ConcurrentDictionary> _CacheDictionary = new ConcurrentDictionary>();
///
/// 1.主动过期
///
static CustomCacheHelper()
{
Task.Run(() => {
while (true)
{
Thread.Sleep(1000); //1秒检查一次
if (_CacheDictionary != null && _CacheDictionary.Keys.Count > 0)
{
var listKey = new List();
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
2.被动过期,部分修改:
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Common.Helper
{
///
/// 自定义内存缓存助手
///
public sealed class CustomCacheHelper
{
#region 单例模式
//创建私有化静态obj锁
private static readonly object _ObjLock = new object();
//创建私有静态字段,接收类的实例化对象
private static volatile CustomCacheHelper _CustomCacheHelper = null;
//构造函数私有化
private CustomCacheHelper() { }
//创建单利对象资源并返回
public static CustomCacheHelper GetSingleObj()
{
if (_CustomCacheHelper == null)
{
lock (_ObjLock)
{
if (_CustomCacheHelper == null)
_CustomCacheHelper = new CustomCacheHelper();
}
}
return _CustomCacheHelper;
}
#endregion
///
/// 缓存字典 => 【key|value|time】
///
private static volatile ConcurrentDictionary> _CacheDictionary = new ConcurrentDictionary>();
///
/// 1.主动过期
///
static CustomCacheHelper()
{
Task.Run(() => {
while (true)
{
Thread.Sleep(1000*60*10); //10分钟检查一次
if (_CacheDictionary != null && _CacheDictionary.Keys.Count > 0)
{
var listKey = new List();
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 value);
}
}
}
});
}
///
/// 索引器
///
///
///
public object this[string key]
{
get => _CacheDictionary[key];
set => _CacheDictionary[key] = new KeyValuePair(value, null);
}
///
/// 设置相对过期缓存
///
/// 键
/// 数据包
/// 相对过期时间
public void Set(string key, object data, int seconds)
{
_CacheDictionary[key] = new KeyValuePair(data, DateTime.Now.AddSeconds(seconds));
}
///
/// 设置绝对过期缓存
///
/// 键<
/// 数据包
public void Set(string key, object data)
{
this[key] = data; //_CacheDictionary[key] = new KeyValuePair(data, null);
}
///
/// 通过key获取缓存value
/// 2.被动过期
///
///
///
///
public T Get(string key)
{
if (Exist(key))
{
var valueTime = _CacheDictionary[key];
return (T)valueTime.Key; //return (T)this[key];
}
else
{
return default(T);
}
}
///
/// 获取缓存个数
///
///
public int Count()
{
int count = 0;
if (_CacheDictionary != null)
count = _CacheDictionary.Count;
return count;
}
///
/// 删除指定key的value
///
///
public void Remove(string key)
{
if (Exist(key))
_CacheDictionary.TryRemove(key, out KeyValuePair value);
}
///
/// 清空所有缓存
///
public void Cleaner()
{
if (_CacheDictionary != null && _CacheDictionary.Count > 0)
{
foreach (var key in _CacheDictionary.Keys)
{
_CacheDictionary.TryRemove(key, out KeyValuePair value);
}
}
}
///
/// 检查key是否存在
/// 2.被动过期,保证任何过期缓存都无法取值
///
///
///
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 value); //缓存过期清理
return false;
}
}
else
{
return false;
}
}
}
}