// Copyright (c) 2022-Now 少林寺驻北固山办事处大神父王喇嘛
//
// SimpleAdmin 基于 Apache License Version 2.0 协议发布,可用于商业项目,但必须遵守以下补充条款:
// 1.请不要删除和修改根目录下的LICENSE文件。
// 2.请不要删除和修改SimpleAdmin源码头部的版权声明。
// 3.分发源码时候,请注明软件出处 https://gitee.com/dotnetmoyu/SimpleAdmin
// 4.基于本软件的作品,只能使用 SimpleAdmin 作为后台服务,除外情况不可商用且不允许二次分发或开源。
// 5.请不得将本软件应用于危害国家安全、荣誉和利益的行为,不能以任何形式用于非法为目的的行为。
// 6.任何基于本软件而产生的一切法律纠纷和责任,均于我司无关。
namespace SimpleAdmin.Cache;
///
///
/// 内存缓存
///
public partial class MemoryCacheService : ISimpleCacheService
{
private readonly MemoryCache _memoryCache;
public MemoryCacheService()
{
_memoryCache = new MemoryCache();
}
#region 普通操作
///
public T Get(string key)
{
var data = _memoryCache.Get(key);
return data.ToObject();
}
///
public int Remove(params string[] keys)
{
return _memoryCache.Remove(keys);
}
///
public bool Set(string key, T value, int expire = -1)
{
return _memoryCache.Set(key, value.ToJson(), expire);
}
///
public bool Set(string key, T value, TimeSpan expire)
{
return _memoryCache.Set(key, value.ToJson(), expire);
}
///
public bool SetExpire(string key, TimeSpan expire)
{
return _memoryCache.SetExpire(key, expire);
}
///
public TimeSpan GetExpire(string key)
{
return _memoryCache.GetExpire(key);
}
///
public bool ContainsKey(string key)
{
return _memoryCache.ContainsKey(key);
}
///
public void Clear()
{
_memoryCache.Clear();
}
///
public void DelByPattern(string pattern)
{
var keys = _memoryCache.Keys.ToList();//获取所有key
keys.ForEach(it =>
{
if (it.Contains(pattern))//如果匹配
_memoryCache.Remove(pattern);
});
}
#endregion 普通操作
#region 集合操作
///
public IDictionary GetAll(IEnumerable keys)
{
IDictionary? result = default;//定义集合
var data = _memoryCache.GetAll(keys);//获取数据
data.ForEach(it =>
{
result.Add(it.Key, it.Value.ToObject());//遍历数据,格式化并加到新的数据集合
});
return result;
}
///
public void SetAll(IDictionary values, int expire = -1)
{
IDictionary? result = default;//定义集合
values.ForEach(it =>
{
result.Add(it.Key, it.Value.ToJson());//遍历数据,格式化并加到新的数据集合
});
_memoryCache.SetAll(values, expire);
}
///
public IDictionary GetDictionary(string key)
{
IDictionary? result = default;//定义集合
var data = _memoryCache.GetDictionary(key);
data.ForEach(it =>
{
result.Add(it.Key, it.Value.ToObject());//遍历数据,格式化并加到新的数据集合
});
return result;
}
///
public IProducerConsumer GetQueue(string key)
{
return _memoryCache.GetQueue(key);
}
///
public IProducerConsumer GetStack(string key)
{
return _memoryCache.GetStack(key);
}
///
public ICollection GetSet(string key)
{
return _memoryCache.GetSet(key);
}
#endregion 集合操作
#region 高级操作
///
public bool Add(string key, T value, int expire = -1)
{
return _memoryCache.Add(key, value.ToJson(), expire);
}
///
public IList GetList(string key)
{
IList result = default;//定义集合
var data = _memoryCache.GetList(key);
data.ForEach(it =>
{
result.Add(it.ToObject());//遍历数据,格式化并加到新的数据集合
});
return result;
}
///
public T Replace(string key, T value)
{
return _memoryCache.Replace(key, value);
}
///
public bool TryGetValue(string key, out T value)
{
_ = _memoryCache.TryGetValue(key, out var result);
value = result.ToObject();
return value == null;
}
///
public long Decrement(string key, long value)
{
return _memoryCache.Decrement(key, value);
}
///
public double Decrement(string key, double value)
{
return _memoryCache.Decrement(key, value);
}
///
public long Increment(string key, long value)
{
return _memoryCache.Increment(key, value);
}
///
public double Increment(string key, double value)
{
return _memoryCache.Increment(key, value);
}
#endregion 高级操作
#region 事务
///
public int Commit()
{
return _memoryCache.Commit();
}
///
public IDisposable AcquireLock(string key, int msTimeout)
{
return _memoryCache.AcquireLock(key, msTimeout);
}
///
public IDisposable AcquireLock(string key, int msTimeout, int msExpire,
bool throwOnFailure)
{
return _memoryCache.AcquireLock(key, msTimeout, msExpire, throwOnFailure);
}
#endregion 事务
}