diff --git a/build/version.props b/build/version.props
index 18841930..1b54eb7f 100644
--- a/build/version.props
+++ b/build/version.props
@@ -1,29 +1,29 @@
- 1.9.2
- 1.9.2
- 1.9.2
- 1.9.2
- 1.9.2
- 1.9.2
- 1.9.2
- 1.9.2
- 1.9.2
- 1.9.2
- 1.9.2
- 1.9.2
- 1.9.2
- 1.9.2
- 1.9.2
- 1.9.2
- 1.9.2
- 1.9.2
- 1.9.2
- 1.9.2
- 1.9.2
- 1.9.2
- 1.9.2
- 1.9.2
- 1.9.2
+ 1.9.3
+ 1.9.3
+ 1.9.3
+ 1.9.3
+ 1.9.3
+ 1.9.3
+ 1.9.3
+ 1.9.3
+ 1.9.3
+ 1.9.3
+ 1.9.3
+ 1.9.3
+ 1.9.3
+ 1.9.3
+ 1.9.3
+ 1.9.3
+ 1.9.3
+ 1.9.3
+ 1.9.3
+ 1.9.3
+ 1.9.3
+ 1.9.3
+ 1.9.3
+ 1.9.3
+ 1.9.3
diff --git a/src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.Async.cs b/src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.Async.cs
index b3c2ad34..0f1e2389 100644
--- a/src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.Async.cs
+++ b/src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.Async.cs
@@ -5,6 +5,7 @@
using System.Threading;
using System.Threading.Tasks;
using EasyCaching.Core;
+ using EasyCaching.Core.Internal;
using global::CSRedis;
using Microsoft.Extensions.Logging;
@@ -346,7 +347,7 @@ public override async Task BaseSetAllAsync(IDictionary value, Time
if (MaxRdSecond > 0)
{
- var addSec = new Random().Next(1, MaxRdSecond);
+ var addSec = RandomHelper.GetNext(1, MaxRdSecond);
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
}
@@ -375,7 +376,7 @@ public override async Task BaseSetAsync(string cacheKey, T cacheValue, TimeSp
if (MaxRdSecond > 0)
{
- var addSec = new Random().Next(1, MaxRdSecond);
+ var addSec = RandomHelper.GetNext(1, MaxRdSecond);
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
}
@@ -405,7 +406,7 @@ public override async Task BaseTrySetAsync(string cacheKey, T cacheValu
if (MaxRdSecond > 0)
{
- var addSec = new Random().Next(1, MaxRdSecond);
+ var addSec = RandomHelper.GetNext(1, MaxRdSecond);
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
}
diff --git a/src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.cs b/src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.cs
index de162c3f..8f34d3d4 100644
--- a/src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.cs
+++ b/src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.cs
@@ -2,6 +2,7 @@
{
using EasyCaching.Core;
using EasyCaching.Core.DistributedLock;
+ using EasyCaching.Core.Internal;
using EasyCaching.Core.Serialization;
using EasyCaching.CSRedis.DistributedLock;
using global::CSRedis;
@@ -458,7 +459,7 @@ public override void BaseSet(string cacheKey, T cacheValue, TimeSpan expirati
if (MaxRdSecond > 0)
{
- var addSec = new Random().Next(1, MaxRdSecond);
+ var addSec = RandomHelper.GetNext(1, MaxRdSecond);
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
}
@@ -480,7 +481,7 @@ public override void BaseSetAll(IDictionary values, TimeSpan expir
//whether to use pipe based on redis mode
if (MaxRdSecond > 0)
{
- var addSec = new Random().Next(1, MaxRdSecond);
+ var addSec = RandomHelper.GetNext(1, MaxRdSecond);
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
}
@@ -506,7 +507,7 @@ public override bool BaseTrySet(string cacheKey, T cacheValue, TimeSpan expir
if (MaxRdSecond > 0)
{
- var addSec = new Random().Next(1, MaxRdSecond);
+ var addSec = RandomHelper.GetNext(1, MaxRdSecond);
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
}
diff --git a/src/EasyCaching.Core/Internal/RandomHelper.cs b/src/EasyCaching.Core/Internal/RandomHelper.cs
new file mode 100644
index 00000000..1976fd73
--- /dev/null
+++ b/src/EasyCaching.Core/Internal/RandomHelper.cs
@@ -0,0 +1,20 @@
+namespace EasyCaching.Core.Internal
+{
+ using System;
+
+ public static class RandomHelper
+ {
+#if NETSTANDARD2_0
+ private static readonly Random _random = new Random();
+#endif
+
+ public static int GetNext(int min, int max)
+ {
+#if NET6_0_OR_GREATER
+ return Random.Shared.Next(min, max);
+#else
+ return _random.Next(min, max);
+#endif
+ }
+ }
+}
diff --git a/src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.Async.cs b/src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.Async.cs
index 96b7050e..5a0b8c9a 100644
--- a/src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.Async.cs
+++ b/src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.Async.cs
@@ -5,6 +5,7 @@
using System.Threading;
using System.Threading.Tasks;
using EasyCaching.Core;
+ using EasyCaching.Core.Internal;
using Microsoft.Extensions.Logging;
///
@@ -184,7 +185,7 @@ public override Task BaseSetAsync(string cacheKey, T cacheValue, TimeSpan exp
if (MaxRdSecond > 0)
{
- var addSec = new Random().Next(1, MaxRdSecond);
+ var addSec = RandomHelper.GetNext(1, MaxRdSecond);
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
}
diff --git a/src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.cs b/src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.cs
index 5eb78bc6..4a18578b 100644
--- a/src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.cs
+++ b/src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.cs
@@ -2,10 +2,12 @@
{
using EasyCaching.Core;
using EasyCaching.Core.DistributedLock;
+ using EasyCaching.Core.Internal;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
+ using System.Security.Cryptography;
///
/// MemoryCaching provider.
@@ -224,7 +226,7 @@ public override void BaseSet(string cacheKey, T cacheValue, TimeSpan expirati
if (MaxRdSecond > 0)
{
- var addSec = new Random().Next(1, MaxRdSecond);
+ var addSec = RandomHelper.GetNext(1, MaxRdSecond);
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
}
diff --git a/src/EasyCaching.LiteDB/DefaultLiteDBCachingProvider.cs b/src/EasyCaching.LiteDB/DefaultLiteDBCachingProvider.cs
index bf2e5809..51c52e3f 100644
--- a/src/EasyCaching.LiteDB/DefaultLiteDBCachingProvider.cs
+++ b/src/EasyCaching.LiteDB/DefaultLiteDBCachingProvider.cs
@@ -2,6 +2,7 @@
namespace EasyCaching.LiteDB
{
using EasyCaching.Core;
+ using EasyCaching.Core.Internal;
using global::LiteDB;
using Microsoft.Extensions.Logging;
using System;
@@ -216,7 +217,7 @@ public override void BaseSet(string cacheKey, T cacheValue, TimeSpan expirati
if (MaxRdSecond > 0)
{
- var addSec = new Random().Next(1, MaxRdSecond);
+ var addSec = RandomHelper.GetNext(1, MaxRdSecond);
expiration.Add(new TimeSpan(0, 0, addSec));
}
_cache.Upsert(new CacheItem
@@ -409,7 +410,7 @@ public override bool BaseTrySet(string cacheKey, T cacheValue, TimeSpan expir
if (MaxRdSecond > 0)
{
- var addSec = new Random().Next(1, MaxRdSecond);
+ var addSec = RandomHelper.GetNext(1, MaxRdSecond);
expiration.Add(new TimeSpan(0, 0, addSec));
}
diff --git a/src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.Async.cs b/src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.Async.cs
index 1d96f1d5..927b6497 100644
--- a/src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.Async.cs
+++ b/src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.Async.cs
@@ -6,6 +6,7 @@
using System.Threading;
using System.Threading.Tasks;
using EasyCaching.Core;
+ using EasyCaching.Core.Internal;
using Microsoft.Extensions.Logging;
///
@@ -160,7 +161,7 @@ public override async Task BaseSetAsync(string cacheKey, T cacheValue, TimeSp
if (MaxRdSecond > 0)
{
- var addSec = new Random().Next(1, MaxRdSecond);
+ var addSec = RandomHelper.GetNext(1, MaxRdSecond);
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
}
@@ -327,7 +328,7 @@ public override Task BaseTrySetAsync(string cacheKey, T cacheValue, Tim
if (MaxRdSecond > 0)
{
- var addSec = new Random().Next(1, MaxRdSecond);
+ var addSec = RandomHelper.GetNext(1, MaxRdSecond);
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
}
diff --git a/src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.cs b/src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.cs
index f0e15a58..ac7429c1 100644
--- a/src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.cs
+++ b/src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.cs
@@ -2,6 +2,7 @@
{
using EasyCaching.Core;
using EasyCaching.Core.DistributedLock;
+ using EasyCaching.Core.Internal;
using EasyCaching.Memcached.DistributedLock;
using Microsoft.Extensions.Logging;
using System;
@@ -205,7 +206,7 @@ public override void BaseSet(string cacheKey, T cacheValue, TimeSpan expirati
if (MaxRdSecond > 0)
{
- var addSec = new Random().Next(1, MaxRdSecond);
+ var addSec = RandomHelper.GetNext(1, MaxRdSecond);
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
}
@@ -410,7 +411,7 @@ public override bool BaseTrySet(string cacheKey, T cacheValue, TimeSpan expir
if (MaxRdSecond > 0)
{
- var addSec = new Random().Next(1, MaxRdSecond);
+ var addSec = RandomHelper.GetNext(1, MaxRdSecond);
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
}
diff --git a/src/EasyCaching.Redis/DefaultRedisCachingProvider.Async.cs b/src/EasyCaching.Redis/DefaultRedisCachingProvider.Async.cs
index 3604b5df..a6d24645 100644
--- a/src/EasyCaching.Redis/DefaultRedisCachingProvider.Async.cs
+++ b/src/EasyCaching.Redis/DefaultRedisCachingProvider.Async.cs
@@ -6,6 +6,7 @@
using System.Threading;
using System.Threading.Tasks;
using EasyCaching.Core;
+ using EasyCaching.Core.Internal;
using Microsoft.Extensions.Logging;
using StackExchange.Redis;
@@ -187,7 +188,7 @@ public override async Task BaseSetAsync(string cacheKey, T cacheValue, TimeSp
if (MaxRdSecond > 0)
{
- var addSec = new Random().Next(1, MaxRdSecond);
+ var addSec = RandomHelper.GetNext(1, MaxRdSecond);
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
}
@@ -398,7 +399,7 @@ public override Task BaseTrySetAsync(string cacheKey, T cacheValue, Tim
if (MaxRdSecond > 0)
{
- var addSec = new Random().Next(1, MaxRdSecond);
+ var addSec = RandomHelper.GetNext(1, MaxRdSecond);
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
}
diff --git a/src/EasyCaching.Redis/DefaultRedisCachingProvider.cs b/src/EasyCaching.Redis/DefaultRedisCachingProvider.cs
index fc81d510..adb74e79 100644
--- a/src/EasyCaching.Redis/DefaultRedisCachingProvider.cs
+++ b/src/EasyCaching.Redis/DefaultRedisCachingProvider.cs
@@ -4,6 +4,7 @@ namespace EasyCaching.Redis
{
using EasyCaching.Core;
using EasyCaching.Core.DistributedLock;
+ using EasyCaching.Core.Internal;
using EasyCaching.Core.Serialization;
using Microsoft.Extensions.Logging;
using StackExchange.Redis;
@@ -247,7 +248,7 @@ public override void BaseSet(string cacheKey, T cacheValue, TimeSpan expirati
if (MaxRdSecond > 0)
{
- var addSec = new Random().Next(1, MaxRdSecond);
+ var addSec = RandomHelper.GetNext(1, MaxRdSecond);
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
}
@@ -574,7 +575,7 @@ public override bool BaseTrySet(string cacheKey, T cacheValue, TimeSpan expir
if (MaxRdSecond > 0)
{
- var addSec = new Random().Next(1, MaxRdSecond);
+ var addSec = RandomHelper.GetNext(1, MaxRdSecond);
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
}
diff --git a/src/EasyCaching.SQLite/DefaultSQLiteCachingProvider.Async.cs b/src/EasyCaching.SQLite/DefaultSQLiteCachingProvider.Async.cs
index 5463aec2..dce5ce03 100644
--- a/src/EasyCaching.SQLite/DefaultSQLiteCachingProvider.Async.cs
+++ b/src/EasyCaching.SQLite/DefaultSQLiteCachingProvider.Async.cs
@@ -7,6 +7,7 @@
using System.Threading.Tasks;
using Dapper;
using EasyCaching.Core;
+ using EasyCaching.Core.Internal;
using Microsoft.Extensions.Logging;
///
@@ -212,7 +213,7 @@ public override async Task BaseSetAsync(string cacheKey, T cacheValue, TimeSp
if (MaxRdSecond > 0)
{
- var addSec = new Random().Next(1, MaxRdSecond);
+ var addSec = RandomHelper.GetNext(1, MaxRdSecond);
expiration.Add(new TimeSpan(0, 0, addSec));
}
@@ -383,7 +384,7 @@ public override async Task BaseTrySetAsync(string cacheKey, T cacheValu
if (MaxRdSecond > 0)
{
- var addSec = new Random().Next(1, MaxRdSecond);
+ var addSec = RandomHelper.GetNext(1, MaxRdSecond);
expiration.Add(new TimeSpan(0, 0, addSec));
}
diff --git a/src/EasyCaching.SQLite/DefaultSQLiteCachingProvider.cs b/src/EasyCaching.SQLite/DefaultSQLiteCachingProvider.cs
index 95a76d68..d209f554 100644
--- a/src/EasyCaching.SQLite/DefaultSQLiteCachingProvider.cs
+++ b/src/EasyCaching.SQLite/DefaultSQLiteCachingProvider.cs
@@ -5,6 +5,7 @@
using System.Linq;
using Dapper;
using EasyCaching.Core;
+ using EasyCaching.Core.Internal;
using Microsoft.Data.Sqlite;
using Microsoft.Extensions.Logging;
@@ -209,7 +210,7 @@ public override void BaseSet(string cacheKey, T cacheValue, TimeSpan expirati
if (MaxRdSecond > 0)
{
- var addSec = new Random().Next(1, MaxRdSecond);
+ var addSec = RandomHelper.GetNext(1, MaxRdSecond);
expiration.Add(new TimeSpan(0, 0, addSec));
}
@@ -394,7 +395,7 @@ public override bool BaseTrySet(string cacheKey, T cacheValue, TimeSpan expir
if (MaxRdSecond > 0)
{
- var addSec = new Random().Next(1, MaxRdSecond);
+ var addSec = RandomHelper.GetNext(1, MaxRdSecond);
expiration.Add(new TimeSpan(0, 0, addSec));
}
diff --git a/test/EasyCaching.PerformanceTests/MBenchmark.cs b/test/EasyCaching.PerformanceTests/MBenchmark.cs
new file mode 100644
index 00000000..200d57e9
--- /dev/null
+++ b/test/EasyCaching.PerformanceTests/MBenchmark.cs
@@ -0,0 +1,62 @@
+namespace EasyCaching.PerformanceTests
+{
+ using BenchmarkDotNet.Attributes;
+ using Microsoft.Extensions.Caching.Memory;
+ using Microsoft.Extensions.DependencyInjection;
+ using System;
+ using System.Diagnostics;
+
+ [MemoryDiagnoser]
+ [AllStatisticsColumn]
+ public class MBenchmark
+ {
+ private Microsoft.Extensions.Caching.Memory.IMemoryCache mc;
+ private EasyCaching.Core.IEasyCachingProvider ec;
+
+ [GlobalSetup]
+ public void Setup()
+ {
+ var sc = new Microsoft.Extensions.DependencyInjection.ServiceCollection();
+ sc.AddMemoryCache();
+ sc.AddEasyCaching(x => x.UseInMemory("b1"));
+
+ var sp = sc.BuildServiceProvider();
+ mc = sp.GetRequiredService();
+ ec = sp.GetRequiredService();
+ }
+
+ [Benchmark]
+ public void MC_SET()
+ {
+ for (int i = 0; i < 1000000; i++)
+ {
+ mc.Set($"ms-key-out-{i}", "ms-value", System.TimeSpan.FromSeconds(5));
+ }
+ }
+
+ [Benchmark]
+ public void EC_SET()
+ {
+ for (int i = 0; i < 1000000; i++)
+ {
+ ec.Set($"ec-key-out-{i}", "ec-value", System.TimeSpan.FromSeconds(5));
+ }
+ }
+
+ public void Test()
+ {
+ Setup();
+
+ Stopwatch sw = new Stopwatch();
+ sw.Start();
+ MC_SET();
+ sw.Stop();
+ Console.WriteLine($"MC_SET: {sw.ElapsedMilliseconds}");
+
+ sw.Restart();
+ EC_SET();
+ sw.Stop();
+ Console.WriteLine($"EC_SET: {sw.ElapsedMilliseconds}");
+ }
+ }
+}
\ No newline at end of file
diff --git a/test/EasyCaching.PerformanceTests/MemoryCacheBenchmark.cs b/test/EasyCaching.PerformanceTests/MemoryCacheBenchmark.cs
index 325b5de1..aee31371 100644
--- a/test/EasyCaching.PerformanceTests/MemoryCacheBenchmark.cs
+++ b/test/EasyCaching.PerformanceTests/MemoryCacheBenchmark.cs
@@ -4,8 +4,8 @@ namespace EasyCaching.PerformanceTests
{
using BenchmarkDotNet.Attributes;
using EasyCaching.InMemory;
- using Microsoft.Extensions.Caching.Memory;
-
+ using Microsoft.Extensions.Caching.Memory;
+
[MemoryDiagnoser]
[AllStatisticsColumn]
public class SetBenchmark
diff --git a/test/EasyCaching.PerformanceTests/Program.cs b/test/EasyCaching.PerformanceTests/Program.cs
index 21ece500..7c0494a4 100644
--- a/test/EasyCaching.PerformanceTests/Program.cs
+++ b/test/EasyCaching.PerformanceTests/Program.cs
@@ -12,7 +12,10 @@ static void Main(string[] args)
//BenchmarkRunner.Run();
//BenchmarkRunner.Run();
//BenchmarkRunner.Run();
- BenchmarkRunner.Run();
+ //BenchmarkRunner.Run();
+ // BenchmarkRunner.Run();
+ //BenchmarkRunner.Run();
+ new MBenchmark().Test();
}
}
}
diff --git a/test/EasyCaching.PerformanceTests/RandomNextBenchmark.cs b/test/EasyCaching.PerformanceTests/RandomNextBenchmark.cs
new file mode 100644
index 00000000..6ed19f71
--- /dev/null
+++ b/test/EasyCaching.PerformanceTests/RandomNextBenchmark.cs
@@ -0,0 +1,23 @@
+namespace EasyCaching.PerformanceTests
+{
+ using BenchmarkDotNet.Attributes;
+
+ [MemoryDiagnoser]
+ [AllStatisticsColumn]
+ public class RandomNextBenchmark
+ {
+ private static System.Random _random = new System.Random();
+
+ [Benchmark]
+ public int RandomRecreate() => new System.Random().Next(1, 10000);
+
+ [Benchmark]
+ public int RandomReuse() => _random.Next(1, 10000);
+
+ [Benchmark]
+ public int RandomGenerator() => System.Security.Cryptography.RandomNumberGenerator.GetInt32(1, 10000);
+
+ [Benchmark]
+ public int RandomShared() => System.Random.Shared.Next(1, 10000);
+ }
+}
\ No newline at end of file