-
Notifications
You must be signed in to change notification settings - Fork 0
/
CsRedisManager.cs
585 lines (519 loc) · 19.9 KB
/
CsRedisManager.cs
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
using CSRedis;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using StackExchange.Redis;
using StackExchange.Redis.KeyspaceIsolation;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace NetPro.RedisManager
{
/// <summary>
///
/// </summary>
//[Obsolete]
internal class CsRedisManager : IRedisManager
{
private readonly RedisCacheOption _option;
private readonly ConnectionMultiplexer _connection;
private IMemoryCache _memorycache;
private ILogger _logger;
public CsRedisManager(RedisCacheOption option,
ConnectionMultiplexer connection,
IMemoryCache memorycache, ILogger<CsRedisManager> logger)
{
_connection = connection;
_option = option;
_memorycache = memorycache;
_logger = logger;
}
public T Get<T>(string key)
{
var result = RedisHelper.Get<T>(key);
return result;
}
public async Task<T> GetAsync<T>(string key)
{
var result = await RedisHelper.GetAsync<T>(key);
return result;
}
/// <summary>
///获取或者创建缓存
/// localExpiredTime参数大于0并且小于expiredTime数据将缓存到本地内存
/// 设置了本地缓存过期时间大于0并委托也为空,本地将保持5秒空值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="func"></param>
/// <param name="expiredTime"></param>
/// <param name="localExpiredTime">本地过期时间</param>
/// <returns></returns>
public T GetOrSet<T>(string key, Func<T> func = null, TimeSpan? expiredTime = null, int localExpiredTime = 0)
{
if (localExpiredTime > 0 && TimeSpan.FromSeconds(localExpiredTime) <= expiredTime && _memorycache != null)
{
var memoryResult = _memorycache.GetOrCreate<T>(key, s =>
{
if (func == null)
{
s.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(1);
return default(T);
}
var resultTemp = _(key, func, expiredTime);
if (resultTemp == null)
s.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(5);
else
s.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(localExpiredTime);
return resultTemp;
});
return memoryResult;
}
return _(key, func, expiredTime);
}
private T _<T>(string key, Func<T> func = null, TimeSpan? expiredTime = null)
{
var result = RedisHelper.Get<T>(key);
//引用类型
if (typeof(T).IsClass && result == null)
{
if (func == null) return default(T);
var executeResult = func.Invoke();
if (executeResult == null) return default(T);
if (expiredTime.HasValue)
RedisHelper.Set(key, executeResult, expiredTime.Value);
else
RedisHelper.Set(key, executeResult);
return executeResult;
}
//值类型
else if (typeof(T).IsValueType)
{
if (typeof(T).Name.StartsWith("Nullable`1"))//可空类型
{
if (result == null)
{
if (func == null) return default(T);
var executeResult = func.Invoke();
if (executeResult == null) return default(T);
if (expiredTime.HasValue)
RedisHelper.Set(key, executeResult, expiredTime.Value);
else
RedisHelper.Set(key, executeResult);
return executeResult;
}
else
{
return result;
}
}
else//不可空
{
if (result.ToString() == typeof(T).GetDefault().ToString())
{
if (func == null) return default(T);
var executeResult = func.Invoke();
if (executeResult == null) return default(T);
if (expiredTime.HasValue)
RedisHelper.Set(key, executeResult, expiredTime.Value);
else
RedisHelper.Set(key, executeResult);
return executeResult;
}
else
{
return result;
}
}
}
return result;
}
public async Task<T> GetOrSetAsync<T>(string key, Func<Task<T>> func = null, TimeSpan? expiredTime = null, int localExpiredTime = 0)
{
if (localExpiredTime > 0 && TimeSpan.FromSeconds(localExpiredTime) <= expiredTime && _memorycache != null)
{
var memoryResult = await _memorycache.GetOrCreateAsync<T>(key, async s =>
{
if (func == null)
{
s.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(1);
return default(T);
}
var executeResult = await _Async(key, func, expiredTime);
if (executeResult == null)
s.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(5);
else
s.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(localExpiredTime);
return executeResult;
});
return memoryResult;
}
return await _Async(key, func, expiredTime);
}
private async Task<T> _Async<T>(string key, Func<Task<T>> func = null, TimeSpan? expiredTime = null)
{
var result = await RedisHelper.GetAsync<T>(key);
//引用类型
if (typeof(T).IsClass && result == null)
{
if (func == null) return default(T);
var executeResult = await func.Invoke();
if (executeResult == null) return default(T);
if (expiredTime.HasValue)
await RedisHelper.SetAsync(key, executeResult, expiredTime.Value);
else
await RedisHelper.SetAsync(key, executeResult);
return executeResult;
}
//值类型
else if (typeof(T).IsValueType)
{
if (typeof(T).Name.StartsWith("Nullable`1"))//可空类型
{
if (result == null)
{
if (func == null) return default(T);
var executeResult = await func.Invoke();
if (executeResult == null) return default(T);
if (expiredTime.HasValue)
await RedisHelper.SetAsync(key, executeResult, expiredTime.Value);
else
await RedisHelper.SetAsync(key, executeResult);
return executeResult;
}
else
{
return result;
}
}
else//不可空
{
if (result.ToString() == typeof(T).GetDefault().ToString())
{
if (func == null) return default(T);
var executeResult = await func.Invoke();
if (executeResult == null) return default(T);
if (expiredTime.HasValue)
await RedisHelper.SetAsync(key, executeResult, expiredTime.Value);
else
await RedisHelper.SetAsync(key, executeResult);
return executeResult;
}
else
{
return result;
}
}
}
return result;
}
public object GetByLuaScript(string script, object obj)
{
var result = RedisHelper.Eval(script, key: "lock_name", args: obj);
return result;
}
public T GetDistributedLock<T>(string resource, int timeoutSeconds, Func<T> func, bool isAwait)
{
if (timeoutSeconds <= 0 || string.IsNullOrWhiteSpace(resource))
{
throw new ArgumentException($"The timeout is not valid with a distributed lock object--key:{resource}--expiryTime--{timeoutSeconds}");
}
resource = AddDefaultPrefixKey(resource);
if (isAwait)
goto gotoNext;
using (var _lockObject = RedisHelper.Lock(resource, 1))
{
if (_lockObject == null)
{
return default;
}
goto gotoNext;
}
gotoNext:
{
using (var lockObject = RedisHelper.Lock(resource, timeoutSeconds))
{
if (lockObject == null)
{
_logger.LogWarning($"当前线程:{Thread.CurrentThread.ManagedThreadId}--未拿到锁!!");
return default;
}
var result = func();
if (lockObject.Unlock())
return result;
return default;
}
}
}
public long HashDelete(string key, IEnumerable<string> field)
{
return RedisHelper.HDel(key, field.ToArray());
}
public async Task<long> HashDeleteAsync(string key, IEnumerable<string> field)
{
return await RedisHelper.HDelAsync(key, field.ToArray());
}
public long HashDelete(string key, string[] field)
{
return RedisHelper.HDel(key, field);
}
public async Task<long> HashDeleteAsync(string key, string[] field)
{
return await RedisHelper.HDelAsync(key, field);
}
public async Task<bool> HashExistsAsync(string key, string hashField)
{
return await RedisHelper.HExistsAsync(key, hashField);
}
public bool HashExists(string key, string hashField)
{
return RedisHelper.HExists(key, hashField);
}
public T HashGet<T>(string key, string field)
{
return RedisHelper.HGet<T>(key, field);
}
public async Task<T> HashGetAsync<T>(string key, string field)
{
return await RedisHelper.HGetAsync<T>(key, field);
}
public bool HashSet<T>(string key, string field, T value, TimeSpan? expiredTime = null)
{
bool isSet = RedisHelper.HSet(key, field, value);
if (isSet && expiredTime.HasValue)
RedisHelper.Expire(key, expiredTime.Value);
return isSet;
}
public async Task<bool> HashSetAsync<T>(string key, string field, T value, TimeSpan? expiredTime = null)
{
bool isSet = await RedisHelper.HSetAsync(key, field, value);
if (isSet && expiredTime.HasValue)
await RedisHelper.ExpireAsync(key, expiredTime.Value);
return isSet;
}
public bool KeyExpire(string key, TimeSpan expiration)
{
return RedisHelper.Expire(key, expiration);
}
public async Task<bool> KeyExpireAsync(string key, TimeSpan expiration)
{
return await RedisHelper.ExpireAsync(key, expiration);
}
public bool Exists(string key)
{
return RedisHelper.Exists(key);
}
public async Task<bool> ExistsAsync(string key)
{
return await RedisHelper.ExistsAsync(key);
}
public async Task<bool> RemoveAsync(string key)
{
var result = await RedisHelper.DelAsync(key);
return result > 0 ? true : false;
}
public bool Remove(string key)
{
return RedisHelper.Del(key) > 0 ? true : false;
}
public long Remove(string[] keys)
{
return RedisHelper.Del(keys);
}
public async Task<long> RemoveAsync(string[] keys)
{
return await RedisHelper.DelAsync(keys);
}
public bool Set(string key, object data, TimeSpan? expiredTime)
{
if (expiredTime.HasValue)
return RedisHelper.Set(key, data, expiredTime.Value);
else
return RedisHelper.Set(key, data);
}
public async Task<bool> SetAsync(string key, object data, TimeSpan? expiredTime)
{
if (expiredTime.HasValue)
return await RedisHelper.SetAsync(key, data, expiredTime.Value);
else
return await RedisHelper.SetAsync(key, data);
}
public bool SortedSetAdd<T>(string key, T obj, decimal score)
{
return RedisHelper.ZAdd(key, (score, obj)) > 0 ? true : false;
}
public async Task<bool> SortedSetAddAsync<T>(string key, T obj, decimal score)
{
var result = await RedisHelper.ZAddAsync(key, (score, obj));
return result > 0 ? true : false;
}
public List<T> SortedSetRangeByRank<T>(string key)
{
return RedisHelper.ZRange<T>(key, 0, -1)?.ToList();
}
public async Task<List<T>> SortedSetRangeByRankAsync<T>(string key)
{
var result = await RedisHelper.ZRangeAsync<T>(key, 0, -1);
return result.ToList();
}
/// <summary>
/// value递减
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expiry"></param>
/// <returns></returns>
public long StringDecrement(string key, long value = 1, TimeSpan? expiry = null)
{
var result = RedisHelper.IncrBy(key, -value);
if (expiry.HasValue)
RedisHelper.Expire(key, expiry.Value);
return result;
}
/// <summary>
/// value递减
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expiry">过期时间</param>
/// <returns></returns>
/// <remarks>TODO 待优化为脚本批量操作</remarks>
public async Task<long> StringDecrementAsync(string key, long value = 1, TimeSpan? expiry = null)
{
var result = await RedisHelper.IncrByAsync(key, -value);
if (expiry.HasValue)
await RedisHelper.ExpireAsync(key, expiry.Value);
return result;
}
/// <summary>
/// value递增
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expiry">过期时间</param>
/// <returns></returns>
/// <remarks>TODO 待优化为脚本批量操作</remarks>
public long StringIncrement(string key, long value = 1, TimeSpan? expiry = null)
{
var result = RedisHelper.IncrBy(key, value);
if (expiry.HasValue)
RedisHelper.Expire(key, expiry.Value);
return result;
}
/// <summary>
/// value递增
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expiry">过期时间</param>
/// <returns></returns>
/// <remarks>TODO 待优化为脚本批量操作</remarks>
public async Task<long> StringIncrementAsync(string key, long value = 1, TimeSpan? expiry = null)
{
var result = await RedisHelper.IncrByAsync(key, value);
if (expiry.HasValue)
await RedisHelper.ExpireAsync(key, expiry.Value);
return result;
}
public long KeyTimeToLive(string key)
{
var time = RedisHelper.Ttl(key);
return time;
}
public async Task<long> KeyTimeToLiveAsync(string key)
{
var time = await RedisHelper.TtlAsync(key);
return time;
}
/// <summary>
/// 获得使用原生stackexchange.redis的能力,用于pipeline (stackExchange.redis专用)
/// </summary>
/// <returns></returns>
public IDatabase GetIDatabase()
{
return _connection.GetDatabase();
}
/// <summary>
/// 订阅
/// </summary>
/// <typeparam name="T"></typeparam>
public void Subscribe<T>(params (string, Action<CSRedisClient.SubscribeMessageEventArgs>)[] channels)
{
RedisHelper.Subscribe(channels);
}
/// <summary>
/// 发布
/// </summary>
/// <param name="key"></param>
/// <param name="message"></param>
public long Publish(string key, string message)
{
return RedisHelper.PublishNoneMessageId(key, message);
}
public async Task<long> PublishAsync(string channel, string input)
{
var result = await RedisHelper.PublishNoneMessageIdAsync(channel, input);
return result;
}
/// <summary>
/// 订阅消息
/// </summary>
/// <param name="channel">管道</param>
/// <returns>收到的消息</returns>
public string Subscriber(string channel)
{
string result = null;
ISubscriber sub = _connection.GetSubscriber();
//订阅名为 messages 的通道
sub.Subscribe(channel, (channel, message) =>
{
result = message;
});
return result;
}
/// <summary>
/// 订阅消息
/// </summary>
/// <param name="channel">管道</param>
/// <returns>收到的消息</returns>
public async Task<string> SubscriberAsync(string channel)
{
string result = null;
ISubscriber sub = _connection.GetSubscriber();
//订阅名为 messages 的通道
await sub.SubscribeAsync(channel, (channel, message) =>
{
result = message;
});
return result;
}
private string AddDefaultPrefixKey(string key)
{
var build = new StringBuilder(_option?.DefaultCustomKey ?? string.Empty);
return build.Append(key).ToString();
}
public List<T> SortedSetRangeByRank<T>(string key, long start = 0, long stop = -1)
{
return RedisHelper.ZRange<T>(key, start, stop)?.ToList();
}
public async Task<List<T>> SortedSetRangeByRankAsync<T>(string key, long start = 0, long stop = -1)
{
var result = await RedisHelper.ZRangeAsync<T>(key, start, stop);
return result.ToList();
}
public IDatabase Database
{
get
{
var db = _connection.GetDatabase(_option.Database);
if (!string.IsNullOrWhiteSpace(_option.DefaultCustomKey))
return db.WithKeyPrefix(_option.DefaultCustomKey);
return db;
}
}
}
}