wanshenmean
2026-03-06 aefdecd0aa3226b7d00d1dc764241b82658b3be8
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
using Microsoft.Extensions.Logging;
using StackExchange.Redis;
using WIDESEAWCS_Core.Caches;
using WIDESEAWCS_RedisService.Connection;
using WIDESEAWCS_RedisService.Options;
using WIDESEAWCS_RedisService.Serialization;
using Microsoft.Extensions.Options;
 
namespace WIDESEAWCS_RedisService.Cache
{
    public class RedisCacheService : ICacheService
    {
        private readonly IRedisConnectionManager _connectionManager;
        private readonly IRedisSerializer _serializer;
        private readonly RedisOptions _options;
        private readonly ILogger<RedisCacheService> _logger;
        private bool _disposed;
 
        public RedisCacheService(
            IRedisConnectionManager connectionManager,
            IRedisSerializer serializer,
            IOptions<RedisOptions> options,
            ILogger<RedisCacheService> logger)
        {
            _connectionManager = connectionManager;
            _serializer = serializer;
            _options = options.Value;
            _logger = logger;
        }
 
        private string BuildKey(string key) => $"{_options.KeyPrefix}{key}";
 
        private IDatabase Db => _connectionManager.GetDatabase();
 
        public bool Exists(string key)
        {
            return Db.KeyExists(BuildKey(key));
        }
 
        public bool Add(string key, string value, int expireSeconds = -1, bool isSliding = false)
        {
            var fullKey = BuildKey(key);
            var expiry = expireSeconds > 0 ? TimeSpan.FromSeconds(expireSeconds) : (TimeSpan?)null;
            return Db.StringSet(fullKey, value, expiry);
        }
 
        public bool AddObject(string key, object value, int expireSeconds = -1, bool isSliding = false)
        {
            return Add(key, _serializer.Serialize(value), expireSeconds, isSliding);
        }
 
        public void AddOrUpdate(string key, string value, int expireSeconds = -1, bool isSliding = false)
        {
            Add(key, value, expireSeconds, isSliding);
        }
 
        public void AddOrUpdate(string key, object value, int expireSeconds = -1, bool isSliding = false)
        {
            AddObject(key, value, expireSeconds, isSliding);
        }
 
        public bool Remove(string key)
        {
            return Db.KeyDelete(BuildKey(key));
        }
 
        public void Remove(IEnumerable<string> keys)
        {
            var redisKeys = keys.Select(k => (RedisKey)BuildKey(k)).ToArray();
            Db.KeyDelete(redisKeys);
        }
 
        #region 删除扩展方法
 
        public string? RemoveAndGet(string key)
        {
            var fullKey = BuildKey(key);
            var value = Db.StringGet(fullKey);
            if (!value.IsNullOrEmpty)
            {
                Db.KeyDelete(fullKey);
                return value.ToString();
            }
            return null;
        }
 
        public T? RemoveAndGet<T>(string key) where T : class
        {
            var fullKey = BuildKey(key);
            var value = Db.StringGet(fullKey);
            if (!value.IsNullOrEmpty)
            {
                Db.KeyDelete(fullKey);
                return _serializer.Deserialize<T>(value!);
            }
            return default;
        }
 
        public int RemoveByPrefix(string prefix)
        {
            var fullPrefix = BuildKey(prefix);
            var server = Db.Multiplexer.GetServer(Db.Multiplexer.GetEndPoints().First());
            var keys = server.Keys(pattern: $"{fullPrefix}*").ToArray();
            if (keys.Length == 0) return 0;
            return (int)Db.KeyDelete(keys);
        }
 
        public int RemoveByPattern(string pattern)
        {
            var fullPattern = BuildKey(pattern).Replace("*", ""); // 保留用户传入的通配符
            var server = Db.Multiplexer.GetServer(Db.Multiplexer.GetEndPoints().First());
            var keys = server.Keys(pattern: $"{_options.KeyPrefix}{pattern}").ToArray();
            if (keys.Length == 0) return 0;
            return (int)Db.KeyDelete(keys);
        }
 
        public int RemoveAll(IEnumerable<string> keys)
        {
            if (keys == null) return 0;
            var redisKeys = keys.Select(k => (RedisKey)BuildKey(k)).ToArray();
            return (int)Db.KeyDelete(redisKeys);
        }
 
        public int RemoveWhere(Func<string, bool> predicate)
        {
            if (predicate == null) return 0;
            var server = Db.Multiplexer.GetServer(Db.Multiplexer.GetEndPoints().First());
            var keys = server.Keys(pattern: $"{_options.KeyPrefix}*")
                .Where(k => predicate(k.ToString().Replace(_options.KeyPrefix, "")))
                .ToArray();
            if (keys.Length == 0) return 0;
            return (int)Db.KeyDelete(keys);
        }
 
        #endregion
 
        #region 添加和修改扩展方法
 
        public void AddAll(IDictionary<string, string> items, int expireSeconds = -1)
        {
            if (items == null) return;
            var batch = Db.CreateBatch();
            var expiry = expireSeconds > 0 ? TimeSpan.FromSeconds(expireSeconds) : (TimeSpan?)null;
            foreach (var item in items)
            {
                batch.StringSetAsync(BuildKey(item.Key), item.Value, expiry);
            }
            batch.Execute();
        }
 
        public void AddAllObjects(IDictionary<string, object> items, int expireSeconds = -1)
        {
            if (items == null) return;
            var batch = Db.CreateBatch();
            var expiry = expireSeconds > 0 ? TimeSpan.FromSeconds(expireSeconds) : (TimeSpan?)null;
            foreach (var item in items)
            {
                batch.StringSetAsync(BuildKey(item.Key), _serializer.Serialize(item.Value), expiry);
            }
            batch.Execute();
        }
 
        public bool Replace(string key, string newValue, int expireSeconds = -1)
        {
            return TryUpdate(key, newValue, expireSeconds);
        }
 
        public bool Replace<T>(string key, T newValue, int expireSeconds = -1) where T : class
        {
            var fullKey = BuildKey(key);
            if (!Db.KeyExists(fullKey)) return false;
            var expiry = expireSeconds > 0 ? TimeSpan.FromSeconds(expireSeconds) : (TimeSpan?)null;
            return Db.StringSet(fullKey, _serializer.Serialize(newValue), expiry, When.Exists);
        }
 
        public string? GetAndRefresh(string key, int expireSeconds)
        {
            var fullKey = BuildKey(key);
            var value = Db.StringGet(fullKey);
            if (!value.IsNullOrEmpty)
            {
                Db.KeyExpire(fullKey, TimeSpan.FromSeconds(expireSeconds));
                return value.ToString();
            }
            return null;
        }
 
        public T? GetAndRefresh<T>(string key, int expireSeconds) where T : class
        {
            var fullKey = BuildKey(key);
            var value = Db.StringGet(fullKey);
            if (!value.IsNullOrEmpty)
            {
                Db.KeyExpire(fullKey, TimeSpan.FromSeconds(expireSeconds));
                return _serializer.Deserialize<T>(value!);
            }
            return default;
        }
 
        public bool RefreshExpire(string key, int expireSeconds)
        {
            return Db.KeyExpire(BuildKey(key), TimeSpan.FromSeconds(expireSeconds));
        }
 
        public bool ExpireIn(string key, int seconds)
        {
            return Db.KeyExpire(BuildKey(key), TimeSpan.FromSeconds(seconds));
        }
 
        public bool ExpireAt(string key, DateTime expireTime)
        {
            return Db.KeyExpire(BuildKey(key), expireTime);
        }
 
        public long? GetExpire(string key)
        {
            var ttl = Db.KeyTimeToLive(BuildKey(key));
            return ttl.HasValue ? (long)ttl.Value.TotalSeconds : null;
        }
 
        public bool AddIfNotExists(string key, string value, int expireSeconds = -1)
        {
            var fullKey = BuildKey(key);
            var expiry = expireSeconds > 0 ? TimeSpan.FromSeconds(expireSeconds) : (TimeSpan?)null;
            return Db.StringSet(fullKey, value, expiry, When.NotExists);
        }
 
        public bool AddIfNotExists<T>(string key, T value, int expireSeconds = -1) where T : class
        {
            var fullKey = BuildKey(key);
            var expiry = expireSeconds > 0 ? TimeSpan.FromSeconds(expireSeconds) : (TimeSpan?)null;
            return Db.StringSet(fullKey, _serializer.Serialize(value), expiry, When.NotExists);
        }
 
        public string? GetAndSet(string key, string newValue, int expireSeconds = -1)
        {
            var fullKey = BuildKey(key);
            var expiry = expireSeconds > 0 ? TimeSpan.FromSeconds(expireSeconds) : (TimeSpan?)null;
            var oldValue = Db.StringGetSet(fullKey, newValue);
            if (expireSeconds > 0)
            {
                Db.KeyExpire(fullKey, expiry);
            }
            return oldValue.IsNullOrEmpty ? null : oldValue.ToString();
        }
 
        public T? GetAndSet<T>(string key, T newValue, int expireSeconds = -1) where T : class
        {
            var fullKey = BuildKey(key);
            var serialized = _serializer.Serialize(newValue);
            var oldValue = Db.StringGetSet(fullKey, serialized);
            if (expireSeconds > 0)
            {
                Db.KeyExpire(fullKey, TimeSpan.FromSeconds(expireSeconds));
            }
            return oldValue.IsNullOrEmpty ? default : _serializer.Deserialize<T>(oldValue!);
        }
 
        public long Increment(string key, long value = 1)
        {
            return Db.StringIncrement(BuildKey(key), value);
        }
 
        public long Decrement(string key, long value = 1)
        {
            return Db.StringDecrement(BuildKey(key), value);
        }
 
        public long Append(string key, string value)
        {
            return Db.StringAppend(BuildKey(key), value);
        }
 
        #endregion
 
        public T? Get<T>(string key) where T : class
        {
            var value = Db.StringGet(BuildKey(key));
            if (value.IsNullOrEmpty) return default;
            return _serializer.Deserialize<T>(value!);
        }
 
        public object? Get(Type type, string key)
        {
            var value = Db.StringGet(BuildKey(key));
            if (value.IsNullOrEmpty) return null;
            return _serializer.Deserialize(value!, type);
        }
 
        public string? Get(string key)
        {
            var value = Db.StringGet(BuildKey(key));
            return value.IsNullOrEmpty ? null : value.ToString();
        }
 
        public void Dispose()
        {
            if (_disposed) return;
            _disposed = true;
        }
 
        public bool TryAdd(string key, string value, int expireSeconds = -1)
        {
            var fullKey = BuildKey(key);
            var expiry = expireSeconds > 0 ? TimeSpan.FromSeconds(expireSeconds) : (TimeSpan?)null;
            return Db.StringSet(fullKey, value, expiry, When.NotExists);
        }
 
        public bool TryAdd<T>(string key, T value, int expireSeconds = -1) where T : class
        {
            return TryAdd(key, _serializer.Serialize(value), expireSeconds);
        }
 
        public bool TryGetValue(string key, out string? value)
        {
            var val = Db.StringGet(BuildKey(key));
            value = val.IsNullOrEmpty ? null : val.ToString();
            return !val.IsNullOrEmpty;
        }
 
        public bool TryGetValue<T>(string key, out T? value) where T : class
        {
            var val = Db.StringGet(BuildKey(key));
            if (val.IsNullOrEmpty) { value = default; return false; }
            value = _serializer.Deserialize<T>(val!);
            return value != null;
        }
 
        public bool TryRemove(string key, out string? value)
        {
            var fullKey = BuildKey(key);
            value = Db.StringGet(fullKey).ToString();
            if (value == null) return false;
            return Db.KeyDelete(fullKey);
        }
 
        public bool TryUpdate(string key, string newValue, int expireSeconds = -1)
        {
            var fullKey = BuildKey(key);
            if (!Db.KeyExists(fullKey)) return false;
            var expiry = expireSeconds > 0 ? TimeSpan.FromSeconds(expireSeconds) : (TimeSpan?)null;
            return Db.StringSet(fullKey, newValue, expiry, When.Exists);
        }
 
        public bool TryUpdateIfChanged(string key, string newValue, int expireSeconds = -1)
        {
            var fullKey = BuildKey(key);
            var existing = Db.StringGet(fullKey);
            if (existing.IsNullOrEmpty) return false;
            if (existing.ToString() == newValue) return false; // 值相同,不更新
            var expiry = expireSeconds > 0 ? TimeSpan.FromSeconds(expireSeconds) : (TimeSpan?)null;
            return Db.StringSet(fullKey, newValue, expiry, When.Exists);
        }
 
        public bool TryUpdateIfChanged<T>(string key, T newValue, int expireSeconds = -1) where T : class
        {
            var fullKey = BuildKey(key);
            var existing = Db.StringGet(fullKey);
            if (existing.IsNullOrEmpty) return false;
 
            var newJson = _serializer.Serialize(newValue);
            if (existing.ToString() == newJson) return false; // JSON字符串相同,不更新
 
            var expiry = expireSeconds > 0 ? TimeSpan.FromSeconds(expireSeconds) : (TimeSpan?)null;
            return Db.StringSet(fullKey, newJson, expiry, When.Exists);
        }
 
        public bool TrySafeUpdate<T>(
            string key,
            T newValue,
            object? expectedVersion,
            Func<T, object?> versionExtractor,
            int expireSeconds = -1) where T : class
        {
            var fullKey = BuildKey(key);
            var existing = Db.StringGet(fullKey);
            if (existing.IsNullOrEmpty) return false;
 
            // 反序列化现有值
            var existingValue = _serializer.Deserialize<T>(existing.ToString()!);
            if (existingValue == null) return false;
 
            // 检查版本是否匹配
            var currentVersion = versionExtractor(existingValue);
            if (!Equals(currentVersion, expectedVersion))
            {
                return false; // 版本不匹配,拒绝更新
            }
 
            // 版本匹配,执行更新
            var newJson = _serializer.Serialize(newValue);
            var expiry = expireSeconds > 0 ? TimeSpan.FromSeconds(expireSeconds) : (TimeSpan?)null;
            return Db.StringSet(fullKey, newJson, expiry, When.Exists);
        }
 
        public string GetOrAdd(string key, string value, int expireSeconds = -1)
        {
            var fullKey = BuildKey(key);
            var existing = Db.StringGet(fullKey);
            if (!existing.IsNullOrEmpty) return existing.ToString();
            var expiry = expireSeconds > 0 ? TimeSpan.FromSeconds(expireSeconds) : (TimeSpan?)null;
            Db.StringSet(fullKey, value, expiry, When.NotExists);
            return Db.StringGet(fullKey).ToString();
        }
 
        public string GetOrAdd(string key, Func<string, string> valueFactory, int expireSeconds = -1)
        {
            var fullKey = BuildKey(key);
            var existing = Db.StringGet(fullKey);
            if (!existing.IsNullOrEmpty) return existing.ToString();
            var value = valueFactory(key);
            var expiry = expireSeconds > 0 ? TimeSpan.FromSeconds(expireSeconds) : (TimeSpan?)null;
            Db.StringSet(fullKey, value, expiry, When.NotExists);
            return Db.StringGet(fullKey).ToString();
        }
 
        public T GetOrAdd<T>(string key, Func<string, T> valueFactory, int expireSeconds = -1) where T : class
        {
            var fullKey = BuildKey(key);
            var existing = Db.StringGet(fullKey);
            if (!existing.IsNullOrEmpty) return _serializer.Deserialize<T>(existing!)!;
            var value = valueFactory(key);
            var expiry = expireSeconds > 0 ? TimeSpan.FromSeconds(expireSeconds) : (TimeSpan?)null;
            Db.StringSet(fullKey, _serializer.Serialize(value), expiry, When.NotExists);
            return value;
        }
    }
}