using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace GDZZ.Core.Service;
///
/// 系统缓存服务
///
[ApiDescriptionSettings(Name = "Cache", Order = 100)]
public class SysCacheService : ISysCacheService, IDynamicApiController, ISingleton
{
private readonly ICache _cache;
private readonly CacheOptions _cacheOptions;
public SysCacheService(IOptions cacheOptions, Func resolveNamed)
{
_cacheOptions = cacheOptions.Value;
_cache = resolveNamed(_cacheOptions.CacheType.ToString(), default) as ICache;
}
///
/// 获取数据范围缓存(机构Id集合)
///
///
///
public async Task> GetDataScope(long userId)
{
var cacheKey = CommonConst.CACHE_KEY_DATASCOPE + $"{userId}";
return await _cache.GetAsync>(cacheKey);
}
///
/// 获取数据范围缓存(用户Id集合)
///
///
///
public async Task> GetUsersDataScope(long userId)
{
var cacheKey = CommonConst.CACHE_KEY_USERSDATASCOPE + $"{userId}";
return await _cache.GetAsync>(cacheKey);
}
///
/// 缓存数据范围(机构Id集合)
///
///
///
///
[NonAction]
public async Task SetDataScope(long userId, List dataScopes)
{
var cacheKey = CommonConst.CACHE_KEY_DATASCOPE + $"{userId}";
await _cache.SetAsync(cacheKey, dataScopes);
}
///
/// 缓存数据范围(用户Id集合)
///
///
///
///
[NonAction]
public async Task SetUsersDataScope(long userId, List dataScopes)
{
var cacheKey = CommonConst.CACHE_KEY_USERSDATASCOPE + $"{userId}";
await _cache.SetAsync(cacheKey, dataScopes);
}
///
/// 获取菜单缓存
///
///
///
///
public async Task> GetMenu(long userId, string appCode)
{
var cacheKey = CommonConst.CACHE_KEY_MENU + $"{userId}-{appCode}";
return await _cache.GetAsync>(cacheKey);
}
///
/// 缓存菜单
///
///
///
///
///
[NonAction]
public async Task SetMenu(long userId, string appCode, List menus)
{
var cacheKey = CommonConst.CACHE_KEY_MENU + $"{userId}-{appCode}";
await _cache.SetAsync(cacheKey, menus);
}
///
/// 获取权限缓存(按钮)
///
///
///
public async Task> GetPermission(long userId)
{
var cacheKey = CommonConst.CACHE_KEY_PERMISSION + $"{userId}";
return await _cache.GetAsync>(cacheKey);
}
///
/// 缓存权限
///
///
///
///
[NonAction]
public async Task SetPermission(long userId, List permissions)
{
var cacheKey = CommonConst.CACHE_KEY_PERMISSION + $"{userId}";
await _cache.SetAsync(cacheKey, permissions);
}
///
/// 获取所有缓存关键字
///
///
public List GetAllCacheKeys()
{
var cacheItems = _cache.GetAllKeys();
if (cacheItems == null) return new List();
return cacheItems.Where(u => !u.ToString().StartsWith("mini-profiler")).Select(u => u).ToList();
}
///
/// 获取所有按钮权限
///
///
public async Task> GetAllPermission()
{
var cacheKey = CommonConst.CACHE_KEY_ALLPERMISSION;
return await _cache.GetAsync>(cacheKey);
}
///
/// 设置所有按钮权限
///
///
///
public async Task SetAllPermission(List permissions)
{
var cacheKey = CommonConst.CACHE_KEY_ALLPERMISSION;
await _cache.SetAsync(cacheKey, permissions);
}
///
/// 删除指定关键字缓存
///
///
///
[NonAction]
public bool Del(string key)
{
_cache.Del(key);
return true;
}
///
/// 删除指定关键字缓存
///
///
///
public Task DelAsync(string key)
{
_cache.DelAsync(key);
return Task.FromResult(true);
}
///
/// 删除某特征关键字缓存
///
///
///
public Task DelByPatternAsync(string key)
{
_cache.DelByPatternAsync(key);
return Task.FromResult(true);
}
///
/// 设置缓存
///
///
///
///
[NonAction]
public bool Set(string key, object value)
{
return _cache.Set(key, value);
}
///
/// 设置缓存
///
///
///
///
public async Task SetAsync(string key, object value)
{
return await _cache.SetAsync(key, value);
}
///
/// 获取缓存
///
///
///
[NonAction]
public string Get(string key)
{
return _cache.Get(key);
}
///
/// 获取缓存
///
///
///
public async Task GetAsync(string key)
{
return await _cache.GetAsync(key);
}
///
/// 获取缓存
///
///
///
///
[NonAction]
public T Get(string key)
{
return _cache.Get(key);
}
///
/// 获取缓存
///
///
///
///
public Task GetAsync(string key)
{
return _cache.GetAsync(key);
}
///
/// 检查给定 key 是否存在
///
/// 键
///
[NonAction]
public bool Exists(string key)
{
return _cache.Exists(key);
}
///
/// 检查给定 key 是否存在
///
/// 键
///
public Task ExistsAsync(string key)
{
return _cache.ExistsAsync(key);
}
}