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