欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

支持高并发高性能 通用缓存容器 浓缩的精华,有什么更好的建议欢迎提出讨论

程序员文章站 2022-06-28 20:22:43
说明:不是很理解并发和缓存逻辑的朋友,请务必自己敲一下代码,好好理解一下,,老司机不用多说了,,拿来就可以用。 ————————————————————— 华丽的分割线——————————————————————————— 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//临时
}

————————————————————— 华丽的分割线———————————————————————————