username@email.com
2024-09-09 626943b5ba84ce44bc19f4c3b8e8e94638bec733
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/***********************************************************************
 *            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
    {
 
        /// <summary>
        /// 判断key是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool Exists(string key)
        {
            return RedisHelper.Exists(key);
        }
 
 
        /// <summary>
        /// 添加缓存
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <param name="value">缓存Value</param>
        /// <param name="expiresIn">缓存时间</param>
        /// <returns></returns>
        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;
        }
 
        /// <summary>
        /// 删除缓存
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <returns></returns>
        public void Remove(string key)
        {
            RedisHelper.DelAsync(key);
        }
 
        /// <summary>
        /// 批量删除缓存
        /// </summary>
        /// <returns></returns>
        public void RemoveAll(IEnumerable<string> keys)
        {
            var enumerable = keys as string[] ?? keys.ToArray();
            string[] keyStrings = enumerable.ToArray();
            RedisHelper.DelAsync(keyStrings);
        }
 
        /// <summary>
        /// 获取缓存对象
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <returns></returns>
        public T Get<T>(string key)
        {
            return RedisHelper.Get<T>(key);
        }
 
        public object Get(string key)
        {
            return RedisHelper.Get<object>(key);
        }
 
        public IDictionary<string, object> GetAll(IEnumerable<string> keys)
        {
            if (keys == null)
                throw new ArgumentNullException(nameof(keys));
            var dict = new Dictionary<string, object>();
 
            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<string> SearchCacheRegex(string pattern)
        {
            return RedisHelper.Keys(pattern);
        }
    }
}