Skip to content

Commit

Permalink
try to optimize random usage
Browse files Browse the repository at this point in the history
Signed-off-by: catcherwong <catcher_hwq@outlook.com>
  • Loading branch information
catcherwong committed Nov 18, 2023
1 parent 699742a commit 25e681d
Show file tree
Hide file tree
Showing 17 changed files with 170 additions and 50 deletions.
50 changes: 25 additions & 25 deletions build/version.props
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
<Project>
<PropertyGroup>
<EasyCachingCorePackageVersion>1.9.2</EasyCachingCorePackageVersion>
<EasyCachingMemcachedPackageVersion>1.9.2</EasyCachingMemcachedPackageVersion>
<EasyCachingRedisPackageVersion>1.9.2</EasyCachingRedisPackageVersion>
<EasyCachingSQLitePackageVersion>1.9.2</EasyCachingSQLitePackageVersion>
<EasyCachingInMemoryPackageVersion>1.9.2</EasyCachingInMemoryPackageVersion>
<EasyCachingHybridPackageVersion>1.9.2</EasyCachingHybridPackageVersion>
<EasyCachingAspectCorePackageVersion>1.9.2</EasyCachingAspectCorePackageVersion>
<EasyCachingCastlePackageVersion>1.9.2</EasyCachingCastlePackageVersion>
<EasyCachingResponseCachingPackageVersion>1.9.2</EasyCachingResponseCachingPackageVersion>
<EasyCachingJsonPackageVersion>1.9.2</EasyCachingJsonPackageVersion>
<EasyCachingMessagePackPackageVersion>1.9.2</EasyCachingMessagePackPackageVersion>
<EasyCachingProtobufPackageVersion>1.9.2</EasyCachingProtobufPackageVersion>
<EasyCachingCSRedisPackageVersion>1.9.2</EasyCachingCSRedisPackageVersion>
<EasyCachingRedisBusPackageVersion>1.9.2</EasyCachingRedisBusPackageVersion>
<EasyCachingCSRedisBusPackageVersion>1.9.2</EasyCachingCSRedisBusPackageVersion>
<EasyCachingRabbitBusPackageVersion>1.9.2</EasyCachingRabbitBusPackageVersion>
<EasyCachingRabbitStreamBusPackageVersion>1.9.2</EasyCachingRabbitStreamBusPackageVersion>
<EasyCachingKafkaBusPackageVersion>1.9.2</EasyCachingKafkaBusPackageVersion>
<EasyCachingZookeeperBusPackageVersion>1.9.2</EasyCachingZookeeperBusPackageVersion>
<EasyCachingDiskPackageVersion>1.9.2</EasyCachingDiskPackageVersion>
<EasyCachingMsExtPackageVersion>1.9.2</EasyCachingMsExtPackageVersion>
<EasyCachingLiteDBPackageVersion>1.9.2</EasyCachingLiteDBPackageVersion>
<EasyCachingSTJsonPackageVersion>1.9.2</EasyCachingSTJsonPackageVersion>
<EasyCachingMemoryPackageVersion>1.9.2</EasyCachingMemoryPackageVersion>
<EasyCachingFaskKVPackageVersion>1.9.2</EasyCachingFaskKVPackageVersion>
<EasyCachingCorePackageVersion>1.9.3</EasyCachingCorePackageVersion>
<EasyCachingMemcachedPackageVersion>1.9.3</EasyCachingMemcachedPackageVersion>
<EasyCachingRedisPackageVersion>1.9.3</EasyCachingRedisPackageVersion>
<EasyCachingSQLitePackageVersion>1.9.3</EasyCachingSQLitePackageVersion>
<EasyCachingInMemoryPackageVersion>1.9.3</EasyCachingInMemoryPackageVersion>
<EasyCachingHybridPackageVersion>1.9.3</EasyCachingHybridPackageVersion>
<EasyCachingAspectCorePackageVersion>1.9.3</EasyCachingAspectCorePackageVersion>
<EasyCachingCastlePackageVersion>1.9.3</EasyCachingCastlePackageVersion>
<EasyCachingResponseCachingPackageVersion>1.9.3</EasyCachingResponseCachingPackageVersion>
<EasyCachingJsonPackageVersion>1.9.3</EasyCachingJsonPackageVersion>
<EasyCachingMessagePackPackageVersion>1.9.3</EasyCachingMessagePackPackageVersion>
<EasyCachingProtobufPackageVersion>1.9.3</EasyCachingProtobufPackageVersion>
<EasyCachingCSRedisPackageVersion>1.9.3</EasyCachingCSRedisPackageVersion>
<EasyCachingRedisBusPackageVersion>1.9.3</EasyCachingRedisBusPackageVersion>
<EasyCachingCSRedisBusPackageVersion>1.9.3</EasyCachingCSRedisBusPackageVersion>
<EasyCachingRabbitBusPackageVersion>1.9.3</EasyCachingRabbitBusPackageVersion>
<EasyCachingRabbitStreamBusPackageVersion>1.9.3</EasyCachingRabbitStreamBusPackageVersion>
<EasyCachingKafkaBusPackageVersion>1.9.3</EasyCachingKafkaBusPackageVersion>
<EasyCachingZookeeperBusPackageVersion>1.9.3</EasyCachingZookeeperBusPackageVersion>
<EasyCachingDiskPackageVersion>1.9.3</EasyCachingDiskPackageVersion>
<EasyCachingMsExtPackageVersion>1.9.3</EasyCachingMsExtPackageVersion>
<EasyCachingLiteDBPackageVersion>1.9.3</EasyCachingLiteDBPackageVersion>
<EasyCachingSTJsonPackageVersion>1.9.3</EasyCachingSTJsonPackageVersion>
<EasyCachingMemoryPackageVersion>1.9.3</EasyCachingMemoryPackageVersion>
<EasyCachingFaskKVPackageVersion>1.9.3</EasyCachingFaskKVPackageVersion>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

if (MaxRdSecond > 0)
{
var addSec = new Random().Next(1, MaxRdSecond);
var addSec = RandomHelper.GetNext(1, MaxRdSecond);
expiration = expiration.Add(TimeSpan.FromSeconds(addSec));
}

Expand Down Expand Up @@ -375,7 +376,7 @@ public override async Task BaseSetAsync<T>(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));
}

Expand Down Expand Up @@ -405,7 +406,7 @@ public override async Task<bool> BaseTrySetAsync<T>(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));
}

Expand Down
7 changes: 4 additions & 3 deletions src/EasyCaching.CSRedis/DefaultCSRedisCachingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -458,7 +459,7 @@ public override void BaseSet<T>(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));
}

Expand All @@ -480,7 +481,7 @@ public override void BaseSetAll<T>(IDictionary<string, T> 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));
}

Expand All @@ -506,7 +507,7 @@ public override bool BaseTrySet<T>(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));
}

Expand Down
20 changes: 20 additions & 0 deletions src/EasyCaching.Core/Internal/RandomHelper.cs
Original file line number Diff line number Diff line change
@@ -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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading;
using System.Threading.Tasks;
using EasyCaching.Core;
using EasyCaching.Core.Internal;
using Microsoft.Extensions.Logging;

/// <summary>
Expand Down Expand Up @@ -184,7 +185,7 @@ public override Task BaseSetAsync<T>(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));
}

Expand Down
4 changes: 3 additions & 1 deletion src/EasyCaching.InMemory/DefaultInMemoryCachingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
/// MemoryCaching provider.
Expand Down Expand Up @@ -224,7 +226,7 @@ public override void BaseSet<T>(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));
}

Expand Down
5 changes: 3 additions & 2 deletions src/EasyCaching.LiteDB/DefaultLiteDBCachingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace EasyCaching.LiteDB
{
using EasyCaching.Core;
using EasyCaching.Core.Internal;
using global::LiteDB;
using Microsoft.Extensions.Logging;
using System;
Expand Down Expand Up @@ -216,7 +217,7 @@ public override void BaseSet<T>(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
Expand Down Expand Up @@ -409,7 +410,7 @@ public override bool BaseTrySet<T>(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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading;
using System.Threading.Tasks;
using EasyCaching.Core;
using EasyCaching.Core.Internal;
using Microsoft.Extensions.Logging;

/// <summary>
Expand Down Expand Up @@ -160,7 +161,7 @@ public override async Task BaseSetAsync<T>(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));
}

Expand Down Expand Up @@ -327,7 +328,7 @@ public override Task<bool> BaseTrySetAsync<T>(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));
}

Expand Down
5 changes: 3 additions & 2 deletions src/EasyCaching.Memcached/DefaultMemcachedCachingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -205,7 +206,7 @@ public override void BaseSet<T>(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));
}

Expand Down Expand Up @@ -410,7 +411,7 @@ public override bool BaseTrySet<T>(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));
}

Expand Down
5 changes: 3 additions & 2 deletions src/EasyCaching.Redis/DefaultRedisCachingProvider.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -187,7 +188,7 @@ public override async Task BaseSetAsync<T>(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));
}

Expand Down Expand Up @@ -398,7 +399,7 @@ public override Task<bool> BaseTrySetAsync<T>(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));
}

Expand Down
5 changes: 3 additions & 2 deletions src/EasyCaching.Redis/DefaultRedisCachingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -247,7 +248,7 @@ public override void BaseSet<T>(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));
}

Expand Down Expand Up @@ -574,7 +575,7 @@ public override bool BaseTrySet<T>(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));
}

Expand Down
5 changes: 3 additions & 2 deletions src/EasyCaching.SQLite/DefaultSQLiteCachingProvider.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading.Tasks;
using Dapper;
using EasyCaching.Core;
using EasyCaching.Core.Internal;
using Microsoft.Extensions.Logging;

/// <summary>
Expand Down Expand Up @@ -212,7 +213,7 @@ public override async Task BaseSetAsync<T>(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));
}

Expand Down Expand Up @@ -383,7 +384,7 @@ public override async Task<bool> BaseTrySetAsync<T>(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));
}

Expand Down
5 changes: 3 additions & 2 deletions src/EasyCaching.SQLite/DefaultSQLiteCachingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using Dapper;
using EasyCaching.Core;
using EasyCaching.Core.Internal;
using Microsoft.Data.Sqlite;
using Microsoft.Extensions.Logging;

Expand Down Expand Up @@ -209,7 +210,7 @@ public override void BaseSet<T>(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));
}

Expand Down Expand Up @@ -394,7 +395,7 @@ public override bool BaseTrySet<T>(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));
}

Expand Down
Loading

0 comments on commit 25e681d

Please sign in to comment.