using NewLife.Caching;
namespace Admin.NET.Core.Service;
///
/// 系统缓存服务
///
[ApiDescriptionSettings(Order = 190)]
public class SysCacheService : IDynamicApiController, ISingleton
{
private readonly ICache _cache;
public SysCacheService(ICache cache)
{
_cache = cache;
}
///
/// 获取所有缓存键名
///
///
[HttpGet("/sysCache/keyList")]
public List GetCacheKeys()
{
return _cache.Keys.ToList();
}
///
/// 增加缓存
///
///
///
///
[HttpPost("/sysCache/add")]
public void Set(string key, object value)
{
_cache.Set(key, value);
}
///
/// 增加缓存并设置过期时间
///
///
///
///
///
[HttpPost("/sysCache/add/expire")]
public void Set(string key, object value, TimeSpan expire)
{
_cache.Set(key, value, expire);
}
///
/// 获取缓存
///
///
///
///
[NonAction]
public T Get(string key)
{
return _cache.Get(key);
}
///
/// 删除缓存
///
///
///
[HttpPost("/sysCache/delete")]
public void Remove(string key)
{
_cache.Remove(key);
}
///
/// 检查缓存是否存在
///
/// 键
///
[NonAction]
public bool ExistKey(string key)
{
return _cache.ContainsKey(key);
}
///
/// 根据键名前缀删除缓存
///
/// 键名前缀
///
[HttpPost("/sysCache/delByParentKey")]
public int RemoveByPrefixKey(string prefixKey)
{
var delKeys = _cache.Keys.Where(u => u.StartsWith(prefixKey)).ToArray();
if (!delKeys.Any()) return 0;
return _cache.Remove(delKeys);
}
///
/// 获取缓存值
///
///
///
[HttpGet("/sysCache/value")]
public dynamic GetCacheValue(string key)
{
return _cache.Get(key);
}
}