169 lines
2.9 KiB
C#
169 lines
2.9 KiB
C#
|
|
using MAX.Core.DependencyInjection;
|
|
using MAX.Core.Interfaces;
|
|
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Linq;
|
|
|
|
namespace MAX.Core.Caches;
|
|
|
|
/// <summary>
|
|
/// 内存缓存
|
|
/// </summary>
|
|
public class MemoryCache : ICache, ISingletonDependency
|
|
{
|
|
//private static readonly Object _locker = new object();
|
|
private readonly ConcurrentDictionary<string, object> _cache = new();
|
|
|
|
public virtual void Clear()
|
|
{
|
|
_cache.Clear();
|
|
}
|
|
|
|
public virtual bool Exists(string key)
|
|
{
|
|
return _cache.ContainsKey(key);
|
|
}
|
|
public virtual T Get<T>(string key)
|
|
{
|
|
if (Exists(key))
|
|
{
|
|
return (T)_cache[key];
|
|
}
|
|
return default;
|
|
}
|
|
public virtual TResult Get<TResult>(string key, Func<TResult> func)
|
|
{
|
|
if (Exists(key))
|
|
{
|
|
return (TResult)_cache[key];
|
|
}
|
|
else
|
|
{
|
|
if (func != null)
|
|
{
|
|
TResult t = func();
|
|
TryAdd(key, t);
|
|
return t;
|
|
}
|
|
}
|
|
return default;
|
|
}
|
|
|
|
public virtual bool OverlayAdd<T>(string key, T value)
|
|
{
|
|
if (Exists(key))
|
|
{
|
|
_cache[key] = value;
|
|
}
|
|
else
|
|
{
|
|
_cache.TryAdd(key, value);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public virtual void Remove(string key)
|
|
{
|
|
_cache.TryRemove(key, out _);
|
|
}
|
|
|
|
public virtual bool TryAdd<T>(string key, T value)
|
|
{
|
|
_cache.AddOrUpdate(key, value, (oldkey, oldvalue) => value);
|
|
return true;
|
|
}
|
|
/// <summary>
|
|
/// 获取所有Key
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public string[] AllKeys()
|
|
{
|
|
return _cache.Keys.ToArray();
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 单例缓存抽象类
|
|
/// </summary>
|
|
/// <typeparam name="TModel">单例对象</typeparam>
|
|
/// <typeparam name="TValue">缓存类型</typeparam>
|
|
public abstract class SingleInstanceLittleCache<TModel, TValue> : SingleInstance<TModel>, ICache<TValue> where TModel : class
|
|
{
|
|
private readonly ConcurrentDictionary<string, TValue> _cache = new();
|
|
/// <summary>
|
|
/// 自定义前缀
|
|
/// </summary>
|
|
//public abstract string KeyPrefix { get; }
|
|
//private string FormatKey(string key)
|
|
//{
|
|
// return KeyPrefix + key;
|
|
//}
|
|
|
|
public virtual void Clear()
|
|
{
|
|
_cache.Clear();
|
|
}
|
|
|
|
public virtual bool Exists(string key)
|
|
{
|
|
return _cache.ContainsKey(key);
|
|
}
|
|
public virtual TValue Get(string key)
|
|
{
|
|
if (Exists(key))
|
|
{
|
|
return _cache[key];
|
|
}
|
|
return default;
|
|
}
|
|
public virtual TValue Get(string key, Func<TValue> func)
|
|
{
|
|
if (Exists(key))
|
|
{
|
|
return _cache[key];
|
|
}
|
|
else
|
|
{
|
|
if (func != null)
|
|
{
|
|
TValue t = func();
|
|
TryAdd(key, t);
|
|
return t;
|
|
}
|
|
}
|
|
return default;
|
|
}
|
|
|
|
public virtual bool OverlayAdd(string key, TValue value)
|
|
{
|
|
if (Exists(key))
|
|
{
|
|
_cache[key] = value;
|
|
}
|
|
else
|
|
{
|
|
_cache.TryAdd(key, value);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public virtual void Remove(string key)
|
|
{
|
|
_cache.TryRemove(key, out _);
|
|
}
|
|
|
|
public virtual bool TryAdd(string key, TValue value)
|
|
{
|
|
_cache.AddOrUpdate(key, value, (oldkey, oldvalue) => value);
|
|
return true;
|
|
}
|
|
/// <summary>
|
|
/// 获取所有Key
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public string[] AllKeys()
|
|
{
|
|
return _cache.Keys.ToArray();
|
|
}
|
|
}
|