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);
}
///
/// 获取机构Id集合
///
///
///
[NonAction]
public List GetOrgIdList(long userId)
{
var key = CacheConst.KeyOrgIdList + userId;
return _cache.Get>(key);
}
///
/// 缓存机构Id集合
///
///
///
///
[NonAction]
public bool SetOrgIdList(long userId, List orgIdList)
{
var key = CacheConst.KeyOrgIdList + userId;
return _cache.Set(key, orgIdList);
}
///
/// 获取权限集合(按钮)
///
///
///
[NonAction]
public List GetPermission(long userId)
{
var key = CacheConst.KeyPermission + userId;
return _cache.Get>(key);
}
///
/// 缓存权限集合(按钮)
///
///
///
///
[NonAction]
public bool SetPermission(long userId, List permissions)
{
var key = CacheConst.KeyPermission + userId;
return _cache.Set(key, permissions);
}
///
/// 获取最大角色数据范围
///
///
///
[NonAction]
public int? GetMaxDataScopeType(long userId)
{
var key = CacheConst.KeyMaxDataScopeType + userId;
return _cache.Get(key);
}
///
/// 缓存最大角色数据范围
///
///
///
///
[NonAction]
public bool SetMaxDataScopeType(long userId, int dataScopeType)
{
var key = CacheConst.KeyMaxDataScopeType + userId;
return _cache.Set(key, dataScopeType);
}
///
/// 获取缓存
///
///
///
[HttpGet("/sysCache/detail")]
public dynamic CacheDetail(string cacheKey)
{
return _cache.Get(cacheKey);
}
/////
///// 获取菜单缓存
/////
/////
/////
/////
//[NonAction]
//public async Task> GetMenu(long userId, string appCode)
//{
// var cacheKey = CacheConst.KeyMenu + $"{userId}-{appCode}";
// var res = await _cache.GetStringAsync(cacheKey);
// return string.IsNullOrWhiteSpace(res) ? null : JSON.Deserialize>(res);
//}
/////
///// 缓存菜单
/////
/////
/////
/////
/////
//[NonAction]
//public async Task SetMenu(long userId, string appCode, List menus)
//{
// var cacheKey = CommonConst.CACHE_KEY_MENU + $"{userId}-{appCode}";
// await _cache.SetStringAsync(cacheKey, JSON.Serialize(menus));
//}
}