namespace Admin.NET.Core.Service; /// /// 系统缓存服务 /// [ApiDescriptionSettings(Order = 400)] public class SysCacheService : IDynamicApiController, ISingleton { private readonly ICache _cache; public SysCacheService(ICache cache) { _cache = cache; } /// /// 获取缓存键名集合 /// /// [DisplayName("获取缓存键名集合")] public List GetKeyList() { return _cache.Keys.ToList(); } /// /// 增加缓存 /// /// /// /// [ApiDescriptionSettings(false)] public void Set(string key, object value) { _cache.Set(key, value); } /// /// 增加缓存并设置过期时间 /// /// /// /// /// [ApiDescriptionSettings(false)] public void Set(string key, object value, TimeSpan expire) { _cache.Set(key, value, expire); } /// /// 获取缓存 /// /// /// /// [ApiDescriptionSettings(false)] public T Get(string key) { return _cache.Get(key); } /// /// 删除缓存 /// /// /// [ApiDescriptionSettings(Name = "Delete"), HttpPost] [DisplayName("删除缓存")] public void Remove(string key) { _cache.Remove(key); } /// /// 检查缓存是否存在 /// /// 键 /// [ApiDescriptionSettings(false)] public bool ExistKey(string key) { return _cache.ContainsKey(key); } /// /// 根据键名前缀删除缓存 /// /// 键名前缀 /// [ApiDescriptionSettings(Name = "DeleteByPreKey"), HttpPost] [DisplayName("根据键名前缀删除缓存")] public int RemoveByPrefixKey(string prefixKey) { var delKeys = _cache.Keys.Where(u => u.StartsWith(prefixKey)).ToArray(); if (!delKeys.Any()) return 0; return _cache.Remove(delKeys); } /// /// 获取缓存值 /// /// /// [DisplayName("获取缓存值")] public dynamic GetValue(string key) { return _cache.Get(key); } }