diff --git a/src/ZiggyCreatures.FusionCache.Chaos/FusionCacheChaosUtils.cs b/src/ZiggyCreatures.FusionCache.Chaos/FusionCacheChaosUtils.cs index 34795160..4ce5bed1 100644 --- a/src/ZiggyCreatures.FusionCache.Chaos/FusionCacheChaosUtils.cs +++ b/src/ZiggyCreatures.FusionCache.Chaos/FusionCacheChaosUtils.cs @@ -1,4 +1,4 @@ -using ZiggyCreatures.Caching.Fusion.Internals; +using ThreadSafeRandomizer; namespace ZiggyCreatures.Caching.Fusion.Chaos; @@ -20,7 +20,7 @@ public static bool ShouldThrow(float throwProbability) if (throwProbability >= 1f) return true; - return ConcurrentRandom.NextDouble() < throwProbability; + return ThreadSafeRandom.Instance.NextDouble() < throwProbability; } /// @@ -47,7 +47,7 @@ public static TimeSpan GetRandomDelay(TimeSpan minDelay, TimeSpan maxDelay) if (minDelay >= maxDelay) return minDelay; - return minDelay + TimeSpan.FromMilliseconds(ConcurrentRandom.NextDouble() * (maxDelay - minDelay).TotalMilliseconds); + return minDelay + TimeSpan.FromMilliseconds(ThreadSafeRandom.Instance.NextDouble() * (maxDelay - minDelay).TotalMilliseconds); } /// diff --git a/src/ZiggyCreatures.FusionCache.Chaos/ZiggyCreatures.FusionCache.Chaos.csproj b/src/ZiggyCreatures.FusionCache.Chaos/ZiggyCreatures.FusionCache.Chaos.csproj index 884c9b54..ae6ce40c 100644 --- a/src/ZiggyCreatures.FusionCache.Chaos/ZiggyCreatures.FusionCache.Chaos.csproj +++ b/src/ZiggyCreatures.FusionCache.Chaos/ZiggyCreatures.FusionCache.Chaos.csproj @@ -1,7 +1,7 @@  - netstandard2.0 + netstandard2.0;net6.0 2.5.0 ZiggyCreatures.FusionCache.Chaos Chaos-related utilities and implementations of various componenets (like a distributed cache or a backplane), useful for things like testing dependent components' behavior in a controlled failing environment. diff --git a/src/ZiggyCreatures.FusionCache/FusionCacheEntryOptions.cs b/src/ZiggyCreatures.FusionCache/FusionCacheEntryOptions.cs index 01ae80e2..ed348e81 100644 --- a/src/ZiggyCreatures.FusionCache/FusionCacheEntryOptions.cs +++ b/src/ZiggyCreatures.FusionCache/FusionCacheEntryOptions.cs @@ -3,6 +3,7 @@ using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Logging; +using ThreadSafeRandomizer; using ZiggyCreatures.Caching.Fusion.Events; using ZiggyCreatures.Caching.Fusion.Internals; using ZiggyCreatures.Caching.Fusion.Internals.Memory; @@ -527,7 +528,7 @@ public double GetJitterDurationMs() if (JitterMaxDuration <= TimeSpan.Zero) return 0d; - return ConcurrentRandom.NextDouble() * JitterMaxDuration.TotalMilliseconds; + return ThreadSafeRandom.Instance.NextDouble() * JitterMaxDuration.TotalMilliseconds; } /// diff --git a/src/ZiggyCreatures.FusionCache/Internals/ConcurrentRandom.cs b/src/ZiggyCreatures.FusionCache/Internals/ConcurrentRandom.cs deleted file mode 100644 index e7257ab6..00000000 --- a/src/ZiggyCreatures.FusionCache/Internals/ConcurrentRandom.cs +++ /dev/null @@ -1,91 +0,0 @@ -using System.Diagnostics; - -namespace ZiggyCreatures.Caching.Fusion.Internals; - -/// -/// A thread-safe version of the Random class. -///
-/// Inspired by Ben Adam's ConcurrentRandom (see here). -///
-public static class ConcurrentRandom -{ - [ThreadStatic] - private static Random? _random; - - private static Random CreateRandom() - { - return new Random((int)Stopwatch.GetTimestamp()); - } - - private static Random Random - { - get { return _random ??= CreateRandom(); } - } - - /// - /// Returns a non-negative random integer that is greater than or equal to 0 and less than . - /// - public static int Next() - { - return Random.Next(); - } - - /// - /// Returns a non-negative random integer that is less than the specified maximum. - /// - /// The exclusive upper bound of the random number to be generated: must be greater than or equal to 0. - public static int Next(int maxValue) - { - return Random.Next(maxValue); - } - - /// - /// Returns a random integer that is within a specified range. - /// - /// The inclusive lower bound of the random number returned. - /// The exclusive upper bound of the random number returned: must be greater than or equal to . - public static int Next(int minValue, int maxValue) - { - return Random.Next(minValue, maxValue); - } - - /// - /// Returns a random long that is within a specified range. - /// - /// The exclusive upper bound of the random number returned: must be greater than or equal to 0. - public static long NextInt64(long maxValue) - { - var buf = new byte[8]; - Random.NextBytes(buf); - return Math.Abs(BitConverter.ToInt64(buf, 0) % maxValue); - } - - /// - /// Returns a random long that is within a specified range. - /// - /// The inclusive lower bound of the random number returned. - /// The exclusive upper bound of the random number returned: must be greater than or equal to . - public static long NextInt64(long minValue, long maxValue) - { - var buf = new byte[8]; - Random.NextBytes(buf); - return Math.Abs(BitConverter.ToInt64(buf, 0) % (maxValue - minValue)) + minValue; - } - - /// - /// Fills the elements of a specified array of bytes with random numbers. - /// - /// The array to be filled with random numbers. - public static void NextBytes(byte[] buffer) - { - Random.NextBytes(buffer); - } - - /// - /// Returns a random floating-point number that is greater than or equal to 0.0, and less than 1.0. - /// - public static double NextDouble() - { - return Random.NextDouble(); - } -} diff --git a/src/ZiggyCreatures.FusionCache/ZiggyCreatures.FusionCache.csproj b/src/ZiggyCreatures.FusionCache/ZiggyCreatures.FusionCache.csproj index 5c2fe316..f62b00fd 100644 --- a/src/ZiggyCreatures.FusionCache/ZiggyCreatures.FusionCache.csproj +++ b/src/ZiggyCreatures.FusionCache/ZiggyCreatures.FusionCache.csproj @@ -1,7 +1,7 @@  - netstandard2.0;net8.0;net9.0 + netstandard2.0;net6.0;net8.0;net9.0 2.5.0 ZiggyCreatures.FusionCache FusionCache is an easy to use, fast and robust hybrid cache with advanced resiliency features. @@ -31,6 +31,10 @@ + + all + analyzers + @@ -41,6 +45,7 @@ +