/*********************************************************************** * Project: baifenBinfa.Net * * Web: https://baifenBinfa.com * * ProjectName: 百分兵法管理系统 * * Author: * * Email: * * Versions: 1.0 * * CreateTime: 2020-02-02 14:09:33 * ClassDescription: ***********************************************************************/ using System; using System.Collections.Generic; using System.Linq; using CoreCms.Net.Caching.Manual; namespace CoreCms.Net.Caching.Redis { public class RedisCacheManager : IManualCacheManager { /// /// 判断key是否存在 /// /// /// public bool Exists(string key) { return RedisHelper.Exists(key); } /// /// 添加缓存 /// /// 缓存Key /// 缓存Value /// 缓存时间 /// public bool Set(string key, object value, int expiresIn = 0) { if (value != null) { return expiresIn > 0 ? RedisHelper.Set(key, value, TimeSpan.FromMinutes(expiresIn)) : RedisHelper.Set(key, value); } return false; } /// /// 删除缓存 /// /// 缓存Key /// public void Remove(string key) { RedisHelper.DelAsync(key); } /// /// 批量删除缓存 /// /// public void RemoveAll(IEnumerable keys) { var enumerable = keys as string[] ?? keys.ToArray(); string[] keyStrings = enumerable.ToArray(); RedisHelper.DelAsync(keyStrings); } /// /// 获取缓存对象 /// /// 缓存Key /// public T Get(string key) { return RedisHelper.Get(key); } public object Get(string key) { return RedisHelper.Get(key); } public IDictionary GetAll(IEnumerable keys) { if (keys == null) throw new ArgumentNullException(nameof(keys)); var dict = new Dictionary(); keys.ToList().ForEach(item => dict.Add(item, RedisHelper.Get(item))); return dict; } public void RemoveCacheAll() { //查找所有分区节点中符合给定模式(pattern)的 key var cacheKeys = RedisHelper.Keys("*"); RedisHelper.Del(cacheKeys); } public void RemoveCacheRegex(string pattern) { var cacheKeys = RedisHelper.Keys(pattern); RedisHelper.Del(cacheKeys); } public IList SearchCacheRegex(string pattern) { return RedisHelper.Keys(pattern); } } }