using Furion.JsonSerialization;
using Microsoft.Extensions.Caching.Distributed;
using System.Text;
using YBEE.EQM.Core;
namespace YBEE.EQM.Application;
///
/// 缓存服务
///
public class CacheService : ICacheService, ISingleton
{
private readonly IDistributedCache _cache;
public CacheService(IDistributedCache cache)
{
_cache = cache;
}
///
/// 获取数据范围缓存(机构Id集合)
///
///
///
public async Task> GetDataScope(int userId)
{
var cacheKey = $"{CommonConst.CACHE_KEY_DATASCOPE}{userId}";
var res = await _cache.GetStringAsync(cacheKey);
return string.IsNullOrWhiteSpace(res) ? null : JSON.Deserialize>(res);
}
///
/// 缓存数据范围(机构Id集合)
///
///
///
///
public async Task SetDataScope(int userId, List dataScopes)
{
var cacheKey = $"{CommonConst.CACHE_KEY_DATASCOPE}{userId}";
await _cache.SetStringAsync(cacheKey, JSON.Serialize(dataScopes));
await AddCacheKey(cacheKey);
}
#region Permission
///
/// 获取权限缓存(按钮)
///
///
///
public async Task> GetPermission(int userId)
{
var cacheKey = $"{CommonConst.CACHE_KEY_PERMISSION}{userId}";
var res = await _cache.GetStringAsync(cacheKey);
return string.IsNullOrWhiteSpace(res) ? null : JSON.Deserialize>(res);
}
///
/// 删除权限缓存
///
///
///
public async Task RemovePermission(int userId)
{
var cacheKey = $"{CommonConst.CACHE_KEY_PERMISSION}{userId}";
await RemoveAsync(cacheKey);
}
///
/// 缓存权限
///
///
///
///
public async Task SetPermission(int userId, List permissions)
{
var cacheKey = $"{CommonConst.CACHE_KEY_PERMISSION}{userId}";
await _cache.SetStringAsync(cacheKey, JSON.Serialize(permissions));
await AddCacheKey(cacheKey);
}
#endregion
#region 角色数据范围权限缓存
///
/// 获取角色数据范围权限缓存
///
///
///
public async Task> GetRoleDataScope(int userId)
{
var cacheKey = $"{CommonConst.CACHE_KEY_ROLE_DATASCOPE}{userId}";
var res = await _cache.GetStringAsync(cacheKey);
return string.IsNullOrWhiteSpace(res) ? null : JSON.Deserialize>(res);
}
///
/// 删除角色数据范围权限缓存
///
///
///
public async Task RemoveRoleDataScope(int userId)
{
var cacheKey = $"{CommonConst.CACHE_KEY_ROLE_DATASCOPE}{userId}";
await RemoveAsync(cacheKey);
}
///
/// 缓存角色数据范围权限
///
///
///
///
public async Task SetRoleDataScope(int userId, List roles)
{
var cacheKey = $"{CommonConst.CACHE_KEY_ROLE_DATASCOPE}{userId}";
await _cache.SetStringAsync(cacheKey, JSON.Serialize(roles));
await AddCacheKey(cacheKey);
}
#endregion
///
/// 获取所有缓存关键字
///
///
//[HttpGet("sysCache/keyList")]
public async Task> GetAllCacheKeys()
{
var res = await _cache.GetStringAsync(CommonConst.CACHE_KEY_ALL);
return string.IsNullOrWhiteSpace(res) ? null : JSON.Deserialize>(res);
}
///
/// 删除指定关键字缓存
///
///
///
//[HttpGet("sysCache/remove")]
public async Task RemoveAsync(string key)
{
await _cache.RemoveAsync(key);
await DelCacheKey(key);
}
///
/// 删除某特征关键字缓存
///
///
///
public async Task DelByPatternAsync(string key)
{
var allkeys = await GetAllCacheKeys();
var delAllkeys = allkeys?.Where(u => u.Contains(key)).ToList();
// 删除相应的缓存
delAllkeys?.ForEach(u =>
{
_cache.Remove(u);
});
// 更新所有缓存键
allkeys = allkeys?.Where(u => !u.Contains(key)).ToList();
await _cache.SetStringAsync(CommonConst.CACHE_KEY_ALL, JSON.Serialize(allkeys));
}
///
/// 设置缓存
///
///
///
///
public async Task SetAsync(string cacheKey, object value)
{
await _cache.SetAsync(cacheKey, Encoding.UTF8.GetBytes(JSON.Serialize(value)));
await AddCacheKey(cacheKey);
}
///
/// 设置缓存
///
///
///
///
public async Task SetStringAsync(string cacheKey, string value)
{
await _cache.SetStringAsync(cacheKey, value);
await AddCacheKey(cacheKey);
}
///
/// 获取缓存
///
///
///
//[HttpGet("sysCache/detail")]
public async Task GetStringAsync(string cacheKey)
{
return await _cache.GetStringAsync(cacheKey);
}
///
/// 获取缓存
///
///
///
///
public async Task GetAsync(string cacheKey)
{
var res = await _cache.GetAsync(cacheKey);
return res == null ? default : JSON.Deserialize(Encoding.UTF8.GetString(res));
}
///
/// 检查给定 key 是否存在
///
/// 键
///
public bool Exists(string cacheKey)
{
var res = _cache.GetStringAsync(CommonConst.CACHE_KEY_ALL).GetAwaiter().GetResult();
var allkeys = string.IsNullOrWhiteSpace(res) ? new HashSet() : JSON.Deserialize>(res);
return allkeys.Any(_ => _ == cacheKey);
}
///
/// 增加缓存Key
///
///
///
public async Task AddCacheKey(string cacheKey)
{
var res = await _cache.GetStringAsync(CommonConst.CACHE_KEY_ALL);
var allkeys = string.IsNullOrWhiteSpace(res) ? new HashSet() : JSON.Deserialize>(res);
if (!allkeys.Any(m => m == cacheKey))
{
allkeys.Add(cacheKey);
await _cache.SetStringAsync(CommonConst.CACHE_KEY_ALL, JSON.Serialize(allkeys));
}
}
///
/// 按KEY删除缓存
///
///
///
public async Task DelCacheKey(string cacheKey)
{
var res = await _cache.GetStringAsync(CommonConst.CACHE_KEY_ALL);
var allkeys = string.IsNullOrWhiteSpace(res) ? new HashSet() : JSON.Deserialize>(res);
if (allkeys.Any(m => m == cacheKey))
{
allkeys.Remove(cacheKey);
await _cache.SetStringAsync(CommonConst.CACHE_KEY_ALL, JSON.Serialize(allkeys));
}
}
///
/// 获取菜单缓存
///
///
///
public async Task> GetMenu(int userId)
{
var cacheKey = $"{CommonConst.CACHE_KEY_MENU}{userId}";
var res = await _cache.GetStringAsync(cacheKey);
return string.IsNullOrWhiteSpace(res) ? null : JSON.Deserialize>(res);
}
///
/// 缓存菜单
///
///
///
///
public async Task SetMenu(int userId, List menus)
{
var cacheKey = $"{CommonConst.CACHE_KEY_MENU}{userId}";
await _cache.SetStringAsync(cacheKey, JSON.Serialize(menus));
await AddCacheKey(cacheKey);
}
}