// 大名科技(天津)有限公司版权所有 电话:18020030720 QQ:515096995 // // 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证 using NewLife.Caching.Models; namespace Admin.NET.Core.Service; /// /// 系统缓存服务 /// [ApiDescriptionSettings(Order = 400)] public class SysCacheService : IDynamicApiController, ISingleton { private readonly ICache _cache; private readonly CacheOptions _cacheOptions; public SysCacheService(ICache cache, IOptions cacheOptions) { _cache = cache; _cacheOptions = cacheOptions.Value; } /// /// 获取缓存键名集合 /// /// [DisplayName("获取缓存键名集合")] public List GetKeyList() { return _cache == Cache.Default ? _cache.Keys.Where(u => u.StartsWith(_cacheOptions.Prefix)).Select(u => u[_cacheOptions.Prefix.Length..]).OrderBy(u => u).ToList() : ((FullRedis)_cache).Search($"{_cacheOptions.Prefix}*", int.MaxValue).Select(u => u[_cacheOptions.Prefix.Length..]).OrderBy(u => u).ToList(); } /// /// 增加缓存 /// /// /// /// [NonAction] public bool Set(string key, object value) { if (string.IsNullOrWhiteSpace(key)) return false; return _cache.Set($"{_cacheOptions.Prefix}{key}", value); } /// /// 增加缓存并设置过期时间 /// /// /// /// /// [NonAction] public bool Set(string key, object value, TimeSpan expire) { if (string.IsNullOrWhiteSpace(key)) return false; return _cache.Set($"{_cacheOptions.Prefix}{key}", value, expire); } /// /// 获取缓存 /// /// /// /// [NonAction] public T Get(string key) { return _cache.Get($"{_cacheOptions.Prefix}{key}"); } /// /// 删除缓存 /// /// /// [ApiDescriptionSettings(Name = "Delete"), HttpPost] [DisplayName("删除缓存")] public int Remove(string key) { return _cache.Remove($"{_cacheOptions.Prefix}{key}"); } /// /// 检查缓存是否存在 /// /// 键 /// [NonAction] public bool ExistKey(string key) { return _cache.ContainsKey($"{_cacheOptions.Prefix}{key}"); } /// /// 根据键名前缀删除缓存 /// /// 键名前缀 /// [ApiDescriptionSettings(Name = "DeleteByPreKey"), HttpPost] [DisplayName("根据键名前缀删除缓存")] public int RemoveByPrefixKey(string prefixKey) { var delKeys = _cache == Cache.Default ? _cache.Keys.Where(u => u.StartsWith($"{_cacheOptions.Prefix}{prefixKey}")).ToArray() : ((FullRedis)_cache).Search($"{_cacheOptions.Prefix}{prefixKey}*", int.MaxValue).ToArray(); return _cache.Remove(delKeys); } /// /// 根据键名前缀获取键名集合 /// /// 键名前缀 /// [DisplayName("根据键名前缀获取键名集合")] public List GetKeysByPrefixKey(string prefixKey) { return _cache == Cache.Default ? _cache.Keys.Where(u => u.StartsWith($"{_cacheOptions.Prefix}{prefixKey}")).Select(u => u[_cacheOptions.Prefix.Length..]).ToList() : ((FullRedis)_cache).Search($"{_cacheOptions.Prefix}{prefixKey}*", int.MaxValue).Select(u => u[_cacheOptions.Prefix.Length..]).ToList(); } /// /// 获取缓存值 /// /// /// [DisplayName("获取缓存值")] public object GetValue(string key) { return _cache == Cache.Default ? _cache.Get($"{_cacheOptions.Prefix}{key}") : _cache.Get($"{_cacheOptions.Prefix}{key}"); } /// /// 获取或添加缓存,在数据不存在时执行委托请求数据 /// /// /// /// /// 过期时间,单位秒 /// [NonAction] public T GetOrAdd(string key, Func callback, int expire = -1) { if (string.IsNullOrWhiteSpace(key)) return default; return _cache.GetOrAdd($"{_cacheOptions.Prefix}{key}", callback, expire); } [NonAction] public RedisHash GetHashMap(string key) { return _cache.GetDictionary(key) as RedisHash; } /// /// 批量添加HASH /// /// /// /// /// [NonAction] public bool HashSet(string key, Dictionary dic) { var hash = GetHashMap(key); return hash.HMSet(dic); } /// /// 添加一条HASH /// /// /// /// /// [NonAction] public void HashAdd(string key, string hashKey, T value) { var hash = GetHashMap(key); hash.Add(hashKey, value); } /// /// 获取多条HASH /// /// /// /// /// [NonAction] public List HashGet(string key, params string[] fields) { var hash = GetHashMap(key); var result = hash.HMGet(fields); return result.ToList(); } /// /// 获取一条HASH /// /// /// /// /// [NonAction] public T HashGetOne(string key, string field) { var hash = GetHashMap(key); var result = hash.HMGet(new string[] { field }); return result[0]; } /// /// 根据KEY获取所有HASH /// /// /// /// [NonAction] public IDictionary HashGetAll(string key) { var hash = GetHashMap(key); return hash.GetAll(); } /// /// 删除HASH /// /// /// /// /// [NonAction] public int HashDel(string key, params string[] fields) { var hash = GetHashMap(key); return hash.HDel(fields); } /// /// 搜索HASH /// /// /// /// /// [NonAction] public List> HashSearch(string key, SearchModel searchModel) { var hash = GetHashMap(key); return hash.Search(searchModel).ToList(); } /// /// 搜索HASH /// /// /// /// /// /// [NonAction] public List> HashSearch(string key, string pattern, int count) { var hash = GetHashMap(key); return hash.Search(pattern, count).ToList(); } }