wanshenmean
8 天以前 fd18eaba5e1c086a588509371f91310e7aafff9c
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace WIDESEAWCS_Core.Caches
{
    public interface ICacheService : IDisposable
    {
        /// <summary>
        /// 验证缓存项是否存在
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <returns></returns>
        bool Exists(string key);
 
        /// <summary>
        /// 添加缓存
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <param name="value">缓存Value</param>
        /// <param name="expiresIn">缓存时长</param>
        /// <param name="isSliding">是否滑动过期(如果在过期时间内有操作,则以当前时间点延长过期时间) //new TimeSpan(0, 60, 0);</param>
        /// <returns></returns>
        bool AddObject(string key, object value, int expireSeconds = -1, bool isSliding = false);
 
        bool Add(string key, string value, int expireSeconds = -1, bool isSliding = false);
 
        void AddOrUpdate(string key, string value, int expireSeconds = -1, bool isSliding = false);
 
        void AddOrUpdate(string key, object value, int expireSeconds = -1, bool isSliding = false);
 
        /// <summary>
        /// 删除缓存
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <returns></returns>
        bool Remove(string key);
 
        /// <summary>
        /// 批量删除缓存
        /// </summary>
        /// <param name="key">缓存Key集合</param>
        /// <returns></returns>
        void Remove(IEnumerable<string> keys);
 
        #region 删除扩展方法
 
        /// <summary>
        /// 删除并获取值:删除指定Key并返回其值
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <returns>被删除的值,不存在返回null</returns>
        string? RemoveAndGet(string key);
 
        /// <summary>
        /// 删除并获取对象:删除指定Key并返回其对象值
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <returns>被删除的对象,不存在返回null</returns>
        T? RemoveAndGet<T>(string key) where T : class;
 
        /// <summary>
        /// 按前缀删除:删除所有以指定前缀开头的Key
        /// </summary>
        /// <param name="prefix">Key前缀</param>
        /// <returns>删除的数量</returns>
        int RemoveByPrefix(string prefix);
 
        /// <summary>
        /// 按模式删除:删除匹配指定模式的所有Key(支持*通配符)
        /// </summary>
        /// <param name="pattern">匹配模式,如 "user:*", "session:123:*"</param>
        /// <returns>删除的数量</returns>
        int RemoveByPattern(string pattern);
 
        /// <summary>
        /// 批量删除并返回成功数量
        /// </summary>
        /// <param name="keys">缓存Key集合</param>
        /// <returns>成功删除的数量</returns>
        int RemoveAll(IEnumerable<string> keys);
 
        /// <summary>
        /// 条件删除:删除满足指定条件的所有Key
        /// </summary>
        /// <param name="predicate">条件谓词</param>
        /// <returns>删除的数量</returns>
        int RemoveWhere(Func<string, bool> predicate);
 
        #endregion
 
        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <returns></returns>
        T? Get<T>(string key) where T : class;
 
        object? Get(Type type, string key);
 
        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <returns></returns>
        string? Get(string key);
 
        #region 添加和修改扩展方法
 
        /// <summary>
        /// 批量添加缓存
        /// </summary>
        void AddAll(IDictionary<string, string> items, int expireSeconds = -1);
 
        /// <summary>
        /// 批量添加对象缓存
        /// </summary>
        void AddAllObjects(IDictionary<string, object> items, int expireSeconds = -1);
 
        /// <summary>
        /// 替换:仅当Key存在时替换其值
        /// </summary>
        bool Replace(string key, string newValue, int expireSeconds = -1);
 
        /// <summary>
        /// 替换对象:仅当Key存在时替换其值
        /// </summary>
        bool Replace<T>(string key, T newValue, int expireSeconds = -1) where T : class;
 
        /// <summary>
        /// 获取并刷新:获取值并刷新其过期时间
        /// </summary>
        string? GetAndRefresh(string key, int expireSeconds);
 
        /// <summary>
        /// 获取并刷新对象:获取对象并刷新其过期时间
        /// </summary>
        T? GetAndRefresh<T>(string key, int expireSeconds) where T : class;
 
        /// <summary>
        /// 刷新过期时间:更新指定Key的过期时间
        /// </summary>
        bool RefreshExpire(string key, int expireSeconds);
 
        /// <summary>
        /// 设置过期时间:在指定秒数后过期
        /// </summary>
        bool ExpireIn(string key, int seconds);
 
        /// <summary>
        /// 设置过期时间:在指定时间点过期
        /// </summary>
        bool ExpireAt(string key, DateTime expireTime);
 
        /// <summary>
        /// 获取剩余过期时间(秒)
        /// </summary>
        long? GetExpire(string key);
 
        /// <summary>
        /// 原子操作:仅当Key不存在时添加(原子操作)
        /// </summary>
        bool AddIfNotExists(string key, string value, int expireSeconds = -1);
 
        /// <summary>
        /// 原子操作:仅当Key不存在时添加对象(原子操作)
        /// </summary>
        bool AddIfNotExists<T>(string key, T value, int expireSeconds = -1) where T : class;
 
        /// <summary>
        /// 原子操作:获取旧值并设置新值
        /// </summary>
        string? GetAndSet(string key, string newValue, int expireSeconds = -1);
 
        /// <summary>
        /// 原子操作:获取旧对象并设置新对象
        /// </summary>
        T? GetAndSet<T>(string key, T newValue, int expireSeconds = -1) where T : class;
 
        /// <summary>
        /// 自增:将Key中的数值自增,返回自增后的值
        /// </summary>
        long Increment(string key, long value = 1);
 
        /// <summary>
        /// 自减:将Key中的数值自减,返回自减后的值
        /// </summary>
        long Decrement(string key, long value = 1);
 
        /// <summary>
        /// 追加:向现有值追加内容
        /// </summary>
        long Append(string key, string value);
 
        #endregion
 
        #region ConcurrentDictionary风格方法
 
        /// <summary>
        /// 尝试添加,仅当Key不存在时添加成功
        /// </summary>
        bool TryAdd(string key, string value, int expireSeconds = -1);
 
        /// <summary>
        /// 尝试添加对象,仅当Key不存在时添加成功
        /// </summary>
        bool TryAdd<T>(string key, T value, int expireSeconds = -1) where T : class;
 
        /// <summary>
        /// 尝试获取值,返回是否存在
        /// </summary>
        bool TryGetValue(string key, out string? value);
 
        /// <summary>
        /// 尝试获取对象,返回是否存在
        /// </summary>
        bool TryGetValue<T>(string key, out T? value) where T : class;
 
        /// <summary>
        /// 尝试移除并返回被移除的值
        /// </summary>
        bool TryRemove(string key, out string? value);
 
        /// <summary>
        /// 尝试更新,仅当Key存在时更新
        /// </summary>
        bool TryUpdate(string key, string newValue, int expireSeconds = -1);
 
        /// <summary>
        /// 值发生改变时更新:仅当Key存在且新值与旧值不同时才更新
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <param name="newValue">新值</param>
        /// <param name="expireSeconds">过期时间(秒)</param>
        /// <returns>值是否发生了改变并更新成功</returns>
        bool TryUpdateIfChanged(string key, string newValue, int expireSeconds = -1);
 
        /// <summary>
        /// 值发生改变时更新对象:仅当Key存在且新值与旧值不同时才更新
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <param name="newValue">新值</param>
        /// <param name="expireSeconds">过期时间(秒)</param>
        /// <returns>值是否发生了改变并更新成功</returns>
        bool TryUpdateIfChanged<T>(string key, T newValue, int expireSeconds = -1) where T : class;
 
        /// <summary>
        /// 安全更新:仅当内存缓存中的值与expectedVersion匹配时才更新
        /// 防止并发写入时旧值覆盖新值(适用于多线程/多进程场景)
        /// </summary>
        /// <typeparam name="T">值类型</typeparam>
        /// <param name="key">缓存键</param>
        /// <param name="newValue">新值</param>
        /// <param name="expectedVersion">期望的版本(通常是旧对象的某个属性值,如时间戳)</param>
        /// <param name="versionExtractor">从对象提取版本号的函数</param>
        /// <param name="expireSeconds">过期时间(秒)</param>
        /// <returns>是否更新成功</returns>
        bool TrySafeUpdate<T>(
            string key,
            T newValue,
            object? expectedVersion,
            Func<T, object?> versionExtractor,
            int expireSeconds = -1) where T : class;
 
        /// <summary>
        /// 获取或添加:Key存在则返回现有值,不存在则添加并返回新值
        /// </summary>
        string GetOrAdd(string key, string value, int expireSeconds = -1);
 
        /// <summary>
        /// 获取或添加(工厂方法):Key存在则返回现有值,不存在则通过工厂方法生成值并添加
        /// </summary>
        string GetOrAdd(string key, Func<string, string> valueFactory, int expireSeconds = -1);
 
        /// <summary>
        /// 获取或添加对象
        /// </summary>
        T GetOrAdd<T>(string key, Func<string, T> valueFactory, int expireSeconds = -1) where T : class;
 
        #endregion
    }
}