Skip to content

Commit 266bec0

Browse files
authored
try to optimize random usage (#509)
Signed-off-by: catcherwong <[email protected]>
1 parent 2c78256 commit 266bec0

17 files changed

+170
-50
lines changed

build/version.props

+25-25
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
<Project>
22
<PropertyGroup>
3-
<EasyCachingCorePackageVersion>1.9.2</EasyCachingCorePackageVersion>
4-
<EasyCachingMemcachedPackageVersion>1.9.2</EasyCachingMemcachedPackageVersion>
5-
<EasyCachingRedisPackageVersion>1.9.2</EasyCachingRedisPackageVersion>
6-
<EasyCachingSQLitePackageVersion>1.9.2</EasyCachingSQLitePackageVersion>
7-
<EasyCachingInMemoryPackageVersion>1.9.2</EasyCachingInMemoryPackageVersion>
8-
<EasyCachingHybridPackageVersion>1.9.2</EasyCachingHybridPackageVersion>
9-
<EasyCachingAspectCorePackageVersion>1.9.2</EasyCachingAspectCorePackageVersion>
10-
<EasyCachingCastlePackageVersion>1.9.2</EasyCachingCastlePackageVersion>
11-
<EasyCachingResponseCachingPackageVersion>1.9.2</EasyCachingResponseCachingPackageVersion>
12-
<EasyCachingJsonPackageVersion>1.9.2</EasyCachingJsonPackageVersion>
13-
<EasyCachingMessagePackPackageVersion>1.9.2</EasyCachingMessagePackPackageVersion>
14-
<EasyCachingProtobufPackageVersion>1.9.2</EasyCachingProtobufPackageVersion>
15-
<EasyCachingCSRedisPackageVersion>1.9.2</EasyCachingCSRedisPackageVersion>
16-
<EasyCachingRedisBusPackageVersion>1.9.2</EasyCachingRedisBusPackageVersion>
17-
<EasyCachingCSRedisBusPackageVersion>1.9.2</EasyCachingCSRedisBusPackageVersion>
18-
<EasyCachingRabbitBusPackageVersion>1.9.2</EasyCachingRabbitBusPackageVersion>
19-
<EasyCachingRabbitStreamBusPackageVersion>1.9.2</EasyCachingRabbitStreamBusPackageVersion>
20-
<EasyCachingKafkaBusPackageVersion>1.9.2</EasyCachingKafkaBusPackageVersion>
21-
<EasyCachingZookeeperBusPackageVersion>1.9.2</EasyCachingZookeeperBusPackageVersion>
22-
<EasyCachingDiskPackageVersion>1.9.2</EasyCachingDiskPackageVersion>
23-
<EasyCachingMsExtPackageVersion>1.9.2</EasyCachingMsExtPackageVersion>
24-
<EasyCachingLiteDBPackageVersion>1.9.2</EasyCachingLiteDBPackageVersion>
25-
<EasyCachingSTJsonPackageVersion>1.9.2</EasyCachingSTJsonPackageVersion>
26-
<EasyCachingMemoryPackageVersion>1.9.2</EasyCachingMemoryPackageVersion>
27-
<EasyCachingFaskKVPackageVersion>1.9.2</EasyCachingFaskKVPackageVersion>
3+
<EasyCachingCorePackageVersion>1.9.3</EasyCachingCorePackageVersion>
4+
<EasyCachingMemcachedPackageVersion>1.9.3</EasyCachingMemcachedPackageVersion>
5+
<EasyCachingRedisPackageVersion>1.9.3</EasyCachingRedisPackageVersion>
6+
<EasyCachingSQLitePackageVersion>1.9.3</EasyCachingSQLitePackageVersion>
7+
<EasyCachingInMemoryPackageVersion>1.9.3</EasyCachingInMemoryPackageVersion>
8+
<EasyCachingHybridPackageVersion>1.9.3</EasyCachingHybridPackageVersion>
9+
<EasyCachingAspectCorePackageVersion>1.9.3</EasyCachingAspectCorePackageVersion>
10+
<EasyCachingCastlePackageVersion>1.9.3</EasyCachingCastlePackageVersion>
11+
<EasyCachingResponseCachingPackageVersion>1.9.3</EasyCachingResponseCachingPackageVersion>
12+
<EasyCachingJsonPackageVersion>1.9.3</EasyCachingJsonPackageVersion>
13+
<EasyCachingMessagePackPackageVersion>1.9.3</EasyCachingMessagePackPackageVersion>
14+
<EasyCachingProtobufPackageVersion>1.9.3</EasyCachingProtobufPackageVersion>
15+
<EasyCachingCSRedisPackageVersion>1.9.3</EasyCachingCSRedisPackageVersion>
16+
<EasyCachingRedisBusPackageVersion>1.9.3</EasyCachingRedisBusPackageVersion>
17+
<EasyCachingCSRedisBusPackageVersion>1.9.3</EasyCachingCSRedisBusPackageVersion>
18+
<EasyCachingRabbitBusPackageVersion>1.9.3</EasyCachingRabbitBusPackageVersion>
19+
<EasyCachingRabbitStreamBusPackageVersion>1.9.3</EasyCachingRabbitStreamBusPackageVersion>
20+
<EasyCachingKafkaBusPackageVersion>1.9.3</EasyCachingKafkaBusPackageVersion>
21+
<EasyCachingZookeeperBusPackageVersion>1.9.3</EasyCachingZookeeperBusPackageVersion>
22+
<EasyCachingDiskPackageVersion>1.9.3</EasyCachingDiskPackageVersion>
23+
<EasyCachingMsExtPackageVersion>1.9.3</EasyCachingMsExtPackageVersion>
24+
<EasyCachingLiteDBPackageVersion>1.9.3</EasyCachingLiteDBPackageVersion>
25+
<EasyCachingSTJsonPackageVersion>1.9.3</EasyCachingSTJsonPackageVersion>
26+
<EasyCachingMemoryPackageVersion>1.9.3</EasyCachingMemoryPackageVersion>
27+
<EasyCachingFaskKVPackageVersion>1.9.3</EasyCachingFaskKVPackageVersion>
2828
</PropertyGroup>
2929
</Project>

src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.Async.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Threading;
66
using System.Threading.Tasks;
77
using EasyCaching.Core;
8+
using EasyCaching.Core.Internal;
89
using global::CSRedis;
910
using Microsoft.Extensions.Logging;
1011

@@ -346,7 +347,7 @@ public override async Task BaseSetAllAsync<T>(IDictionary<string, T> value, Time
346347

347348
if (MaxRdSecond > 0)
348349
{
349-
var addSec = new Random().Next(1, MaxRdSecond);
350+
var addSec = RandomHelper.GetNext(1, MaxRdSecond);
350351
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
351352
}
352353

@@ -375,7 +376,7 @@ public override async Task BaseSetAsync<T>(string cacheKey, T cacheValue, TimeSp
375376

376377
if (MaxRdSecond > 0)
377378
{
378-
var addSec = new Random().Next(1, MaxRdSecond);
379+
var addSec = RandomHelper.GetNext(1, MaxRdSecond);
379380
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
380381
}
381382

@@ -405,7 +406,7 @@ public override async Task<bool> BaseTrySetAsync<T>(string cacheKey, T cacheValu
405406

406407
if (MaxRdSecond > 0)
407408
{
408-
var addSec = new Random().Next(1, MaxRdSecond);
409+
var addSec = RandomHelper.GetNext(1, MaxRdSecond);
409410
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
410411
}
411412

src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{
33
using EasyCaching.Core;
44
using EasyCaching.Core.DistributedLock;
5+
using EasyCaching.Core.Internal;
56
using EasyCaching.Core.Serialization;
67
using EasyCaching.CSRedis.DistributedLock;
78
using global::CSRedis;
@@ -458,7 +459,7 @@ public override void BaseSet<T>(string cacheKey, T cacheValue, TimeSpan expirati
458459

459460
if (MaxRdSecond > 0)
460461
{
461-
var addSec = new Random().Next(1, MaxRdSecond);
462+
var addSec = RandomHelper.GetNext(1, MaxRdSecond);
462463
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
463464
}
464465

@@ -480,7 +481,7 @@ public override void BaseSetAll<T>(IDictionary<string, T> values, TimeSpan expir
480481
//whether to use pipe based on redis mode
481482
if (MaxRdSecond > 0)
482483
{
483-
var addSec = new Random().Next(1, MaxRdSecond);
484+
var addSec = RandomHelper.GetNext(1, MaxRdSecond);
484485
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
485486
}
486487

@@ -506,7 +507,7 @@ public override bool BaseTrySet<T>(string cacheKey, T cacheValue, TimeSpan expir
506507

507508
if (MaxRdSecond > 0)
508509
{
509-
var addSec = new Random().Next(1, MaxRdSecond);
510+
var addSec = RandomHelper.GetNext(1, MaxRdSecond);
510511
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
511512
}
512513

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace EasyCaching.Core.Internal
2+
{
3+
using System;
4+
5+
public static class RandomHelper
6+
{
7+
#if NETSTANDARD2_0
8+
private static readonly Random _random = new Random();
9+
#endif
10+
11+
public static int GetNext(int min, int max)
12+
{
13+
#if NET6_0_OR_GREATER
14+
return Random.Shared.Next(min, max);
15+
#else
16+
return _random.Next(min, max);
17+
#endif
18+
}
19+
}
20+
}

src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.Async.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Threading;
66
using System.Threading.Tasks;
77
using EasyCaching.Core;
8+
using EasyCaching.Core.Internal;
89
using Microsoft.Extensions.Logging;
910

1011
/// <summary>
@@ -184,7 +185,7 @@ public override Task BaseSetAsync<T>(string cacheKey, T cacheValue, TimeSpan exp
184185

185186
if (MaxRdSecond > 0)
186187
{
187-
var addSec = new Random().Next(1, MaxRdSecond);
188+
var addSec = RandomHelper.GetNext(1, MaxRdSecond);
188189
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
189190
}
190191

src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
{
33
using EasyCaching.Core;
44
using EasyCaching.Core.DistributedLock;
5+
using EasyCaching.Core.Internal;
56
using Microsoft.Extensions.Logging;
67
using System;
78
using System.Collections.Generic;
89
using System.Linq;
10+
using System.Security.Cryptography;
911

1012
/// <summary>
1113
/// MemoryCaching provider.
@@ -224,7 +226,7 @@ public override void BaseSet<T>(string cacheKey, T cacheValue, TimeSpan expirati
224226

225227
if (MaxRdSecond > 0)
226228
{
227-
var addSec = new Random().Next(1, MaxRdSecond);
229+
var addSec = RandomHelper.GetNext(1, MaxRdSecond);
228230
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
229231
}
230232

src/EasyCaching.LiteDB/DefaultLiteDBCachingProvider.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace EasyCaching.LiteDB
33
{
44
using EasyCaching.Core;
5+
using EasyCaching.Core.Internal;
56
using global::LiteDB;
67
using Microsoft.Extensions.Logging;
78
using System;
@@ -216,7 +217,7 @@ public override void BaseSet<T>(string cacheKey, T cacheValue, TimeSpan expirati
216217

217218
if (MaxRdSecond > 0)
218219
{
219-
var addSec = new Random().Next(1, MaxRdSecond);
220+
var addSec = RandomHelper.GetNext(1, MaxRdSecond);
220221
expiration.Add(new TimeSpan(0, 0, addSec));
221222
}
222223
_cache.Upsert(new CacheItem
@@ -409,7 +410,7 @@ public override bool BaseTrySet<T>(string cacheKey, T cacheValue, TimeSpan expir
409410

410411
if (MaxRdSecond > 0)
411412
{
412-
var addSec = new Random().Next(1, MaxRdSecond);
413+
var addSec = RandomHelper.GetNext(1, MaxRdSecond);
413414
expiration.Add(new TimeSpan(0, 0, addSec));
414415
}
415416

src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.Async.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Threading;
77
using System.Threading.Tasks;
88
using EasyCaching.Core;
9+
using EasyCaching.Core.Internal;
910
using Microsoft.Extensions.Logging;
1011

1112
/// <summary>
@@ -160,7 +161,7 @@ public override async Task BaseSetAsync<T>(string cacheKey, T cacheValue, TimeSp
160161

161162
if (MaxRdSecond > 0)
162163
{
163-
var addSec = new Random().Next(1, MaxRdSecond);
164+
var addSec = RandomHelper.GetNext(1, MaxRdSecond);
164165
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
165166
}
166167

@@ -327,7 +328,7 @@ public override Task<bool> BaseTrySetAsync<T>(string cacheKey, T cacheValue, Tim
327328

328329
if (MaxRdSecond > 0)
329330
{
330-
var addSec = new Random().Next(1, MaxRdSecond);
331+
var addSec = RandomHelper.GetNext(1, MaxRdSecond);
331332
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
332333
}
333334

src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{
33
using EasyCaching.Core;
44
using EasyCaching.Core.DistributedLock;
5+
using EasyCaching.Core.Internal;
56
using EasyCaching.Memcached.DistributedLock;
67
using Microsoft.Extensions.Logging;
78
using System;
@@ -205,7 +206,7 @@ public override void BaseSet<T>(string cacheKey, T cacheValue, TimeSpan expirati
205206

206207
if (MaxRdSecond > 0)
207208
{
208-
var addSec = new Random().Next(1, MaxRdSecond);
209+
var addSec = RandomHelper.GetNext(1, MaxRdSecond);
209210
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
210211
}
211212

@@ -410,7 +411,7 @@ public override bool BaseTrySet<T>(string cacheKey, T cacheValue, TimeSpan expir
410411

411412
if (MaxRdSecond > 0)
412413
{
413-
var addSec = new Random().Next(1, MaxRdSecond);
414+
var addSec = RandomHelper.GetNext(1, MaxRdSecond);
414415
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
415416
}
416417

src/EasyCaching.Redis/DefaultRedisCachingProvider.Async.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Threading;
77
using System.Threading.Tasks;
88
using EasyCaching.Core;
9+
using EasyCaching.Core.Internal;
910
using Microsoft.Extensions.Logging;
1011
using StackExchange.Redis;
1112

@@ -187,7 +188,7 @@ public override async Task BaseSetAsync<T>(string cacheKey, T cacheValue, TimeSp
187188

188189
if (MaxRdSecond > 0)
189190
{
190-
var addSec = new Random().Next(1, MaxRdSecond);
191+
var addSec = RandomHelper.GetNext(1, MaxRdSecond);
191192
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
192193
}
193194

@@ -398,7 +399,7 @@ public override Task<bool> BaseTrySetAsync<T>(string cacheKey, T cacheValue, Tim
398399

399400
if (MaxRdSecond > 0)
400401
{
401-
var addSec = new Random().Next(1, MaxRdSecond);
402+
var addSec = RandomHelper.GetNext(1, MaxRdSecond);
402403
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
403404
}
404405

src/EasyCaching.Redis/DefaultRedisCachingProvider.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace EasyCaching.Redis
44
{
55
using EasyCaching.Core;
66
using EasyCaching.Core.DistributedLock;
7+
using EasyCaching.Core.Internal;
78
using EasyCaching.Core.Serialization;
89
using Microsoft.Extensions.Logging;
910
using StackExchange.Redis;
@@ -247,7 +248,7 @@ public override void BaseSet<T>(string cacheKey, T cacheValue, TimeSpan expirati
247248

248249
if (MaxRdSecond > 0)
249250
{
250-
var addSec = new Random().Next(1, MaxRdSecond);
251+
var addSec = RandomHelper.GetNext(1, MaxRdSecond);
251252
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
252253
}
253254

@@ -574,7 +575,7 @@ public override bool BaseTrySet<T>(string cacheKey, T cacheValue, TimeSpan expir
574575

575576
if (MaxRdSecond > 0)
576577
{
577-
var addSec = new Random().Next(1, MaxRdSecond);
578+
var addSec = RandomHelper.GetNext(1, MaxRdSecond);
578579
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
579580
}
580581

src/EasyCaching.SQLite/DefaultSQLiteCachingProvider.Async.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Threading.Tasks;
88
using Dapper;
99
using EasyCaching.Core;
10+
using EasyCaching.Core.Internal;
1011
using Microsoft.Extensions.Logging;
1112

1213
/// <summary>
@@ -212,7 +213,7 @@ public override async Task BaseSetAsync<T>(string cacheKey, T cacheValue, TimeSp
212213

213214
if (MaxRdSecond > 0)
214215
{
215-
var addSec = new Random().Next(1, MaxRdSecond);
216+
var addSec = RandomHelper.GetNext(1, MaxRdSecond);
216217
expiration.Add(new TimeSpan(0, 0, addSec));
217218
}
218219

@@ -383,7 +384,7 @@ public override async Task<bool> BaseTrySetAsync<T>(string cacheKey, T cacheValu
383384

384385
if (MaxRdSecond > 0)
385386
{
386-
var addSec = new Random().Next(1, MaxRdSecond);
387+
var addSec = RandomHelper.GetNext(1, MaxRdSecond);
387388
expiration.Add(new TimeSpan(0, 0, addSec));
388389
}
389390

src/EasyCaching.SQLite/DefaultSQLiteCachingProvider.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Linq;
66
using Dapper;
77
using EasyCaching.Core;
8+
using EasyCaching.Core.Internal;
89
using Microsoft.Data.Sqlite;
910
using Microsoft.Extensions.Logging;
1011

@@ -209,7 +210,7 @@ public override void BaseSet<T>(string cacheKey, T cacheValue, TimeSpan expirati
209210

210211
if (MaxRdSecond > 0)
211212
{
212-
var addSec = new Random().Next(1, MaxRdSecond);
213+
var addSec = RandomHelper.GetNext(1, MaxRdSecond);
213214
expiration.Add(new TimeSpan(0, 0, addSec));
214215
}
215216

@@ -394,7 +395,7 @@ public override bool BaseTrySet<T>(string cacheKey, T cacheValue, TimeSpan expir
394395

395396
if (MaxRdSecond > 0)
396397
{
397-
var addSec = new Random().Next(1, MaxRdSecond);
398+
var addSec = RandomHelper.GetNext(1, MaxRdSecond);
398399
expiration.Add(new TimeSpan(0, 0, addSec));
399400
}
400401

0 commit comments

Comments
 (0)