From 1af6ecdc6fd2da52427fb8cae0831fac73d9f315 Mon Sep 17 00:00:00 2001 From: Shuai Zhang Date: Sat, 24 Sep 2022 20:46:56 +0800 Subject: [PATCH] style: fix or suppress all known warnings. --- .../PlcClientDeadline.cs | 1 + .../Memoization.Tests/TestMemoization.cs | 16 +- .../Memoization/GlobalSuppressions.cs | 14 + Memoization.Net/Memoization/Memoization.cs | 1276 ++++++++--------- .../WebHdfsFileInfo.cs | 74 +- .../LongIntervalHistory_UnitTest.cs | 3 +- RateLimiter/RateLimiter/GlobalSuppressions.cs | 16 + RateLimiter/RateLimiter/RateLimiterBase.cs | 15 +- RateLimiter/RateLimiter/SmoothRateLimiter.cs | 13 +- third_party/Directory.Build.props | 7 +- 10 files changed, 739 insertions(+), 696 deletions(-) create mode 100644 Memoization.Net/Memoization/GlobalSuppressions.cs create mode 100644 RateLimiter/RateLimiter/GlobalSuppressions.cs diff --git a/GeothermalResearchInstitute/GeothermalResearchInstitute.PlcV2/PlcClientDeadline.cs b/GeothermalResearchInstitute/GeothermalResearchInstitute.PlcV2/PlcClientDeadline.cs index 74cbfab4..e828cfa9 100644 --- a/GeothermalResearchInstitute/GeothermalResearchInstitute.PlcV2/PlcClientDeadline.cs +++ b/GeothermalResearchInstitute/GeothermalResearchInstitute.PlcV2/PlcClientDeadline.cs @@ -31,6 +31,7 @@ private void DeadlineBackgroundTaskEntryPoint() "Request {0} to {1} exceed deadline, deadline={2:u}, now1={3:u}, now2={4:u}", entry.Key, this.RemoteEndPoint, + entry.Value.Deadline, now, DateTime.UtcNow); requestContext.TaskCompletionSource.TrySetException(new RpcException(new Status( diff --git a/Memoization.Net/Memoization.Tests/TestMemoization.cs b/Memoization.Net/Memoization.Tests/TestMemoization.cs index c838f155..b54da071 100644 --- a/Memoization.Net/Memoization.Tests/TestMemoization.cs +++ b/Memoization.Net/Memoization.Tests/TestMemoization.cs @@ -54,14 +54,16 @@ public void Test2() m_fib(7); Assert.Equal(0, counter); } - } - internal static class YCombinator - { - public static Func, Func>, Func> Fix = - f => ((Recursive)(g => - f(x => g(g)(x))))((Recursive)(g => f(x => g(g)(x)))); + internal static class YCombinator + { + private static Func, Func>, Func> fix = + f => ((Recursive)(g => + f(x => g(g)(x))))((Recursive)(g => f(x => g(g)(x)))); - private delegate Func Recursive(Recursive recursive); + private delegate Func Recursive(Recursive recursive); + + public static Func, Func>, Func> Fix { get => fix; set => fix = value; } + } } } diff --git a/Memoization.Net/Memoization/GlobalSuppressions.cs b/Memoization.Net/Memoization/GlobalSuppressions.cs new file mode 100644 index 00000000..1175c64a --- /dev/null +++ b/Memoization.Net/Memoization/GlobalSuppressions.cs @@ -0,0 +1,14 @@ +// +// Copyright Shuai Zhang. All rights reserved. +// Licensed under the GPLv3 license. See LICENSE file in the project root for full license information. +// +// +// This file is used by Code Analysis to maintain SuppressMessage +// attributes that are applied to this project. +// Project-level suppressions either have no target or are given +// a specific target and scoped to a namespace, type, member, etc. + +using System.Diagnostics.CodeAnalysis; + +[assembly: SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1119:Statement should not use unnecessary parenthesis", Justification = "<挂起>", Scope = "member", Target = "~M:Memoization.Memoization.Create``2(System.Func{``0,``1},Microsoft.Extensions.Caching.Memory.IMemoryCache,Microsoft.Extensions.Caching.Memory.MemoryCacheEntryOptions)~System.Func{``0,``1}")] +[assembly: SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1119:Statement should not use unnecessary parenthesis", Justification = "<挂起>", Scope = "member", Target = "~M:Memoization.Memoization.Create``2(System.Func{``0,``1},Microsoft.Extensions.Caching.Memory.IMemoryCache)~System.Func{``0,``1}")] diff --git a/Memoization.Net/Memoization/Memoization.cs b/Memoization.Net/Memoization/Memoization.cs index 726a052a..fd00c1b1 100644 --- a/Memoization.Net/Memoization/Memoization.cs +++ b/Memoization.Net/Memoization/Memoization.cs @@ -1,638 +1,638 @@ -// -// Copyright Shuai Zhang. All rights reserved. -// Licensed under the GPLv3 license. See LICENSE file in the project root for full license information. -// - -using System; -using System.Diagnostics; -using Microsoft.Extensions.Caching.Memory; - -namespace Memoization -{ - public static partial class Memoization - { - [DebuggerStepThrough] - public static Func Create(Func func) - { - return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null")); - } - - [DebuggerStepThrough] - public static Func Create(Func func, MemoryCacheEntryOptions options) - { - return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null"), options); - } - - public static Func Create(Func func, IMemoryCache cache) - { - return (T1 t1) => cache.GetOrCreate((t1), ignored => func(t1)); - } - - public static Func Create(Func func, IMemoryCache cache, MemoryCacheEntryOptions options) - { - return (T1 t1) => - { - var key = (t1); - if (!cache.TryGetValue(key, out var result)) - { - var entry = cache.CreateEntry(key); - result = func(t1); - entry.SetOptions(options); - entry.SetValue(result); - - // need to manually call dispose instead of having a using - // in case the factory passed in throws, in which case we - // do not want to add the entry to the cache - entry.Dispose(); - } - - return result; - }; - } - - [DebuggerStepThrough] - public static Func Create(Func func) - { - return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null")); - } - - [DebuggerStepThrough] - public static Func Create(Func func, MemoryCacheEntryOptions options) - { - return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null"), options); - } - - public static Func Create(Func func, IMemoryCache cache) - { - return (T1 t1, T2 t2) => cache.GetOrCreate((t1, t2), ignored => func(t1, t2)); - } - - public static Func Create(Func func, IMemoryCache cache, MemoryCacheEntryOptions options) - { - return (T1 t1, T2 t2) => - { - var key = (t1, t2); - if (!cache.TryGetValue(key, out var result)) - { - var entry = cache.CreateEntry(key); - result = func(t1, t2); - entry.SetOptions(options); - entry.SetValue(result); - - // need to manually call dispose instead of having a using - // in case the factory passed in throws, in which case we - // do not want to add the entry to the cache - entry.Dispose(); - } - - return result; - }; - } - - [DebuggerStepThrough] - public static Func Create(Func func) - { - return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null")); - } - - [DebuggerStepThrough] - public static Func Create(Func func, MemoryCacheEntryOptions options) - { - return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null"), options); - } - - public static Func Create(Func func, IMemoryCache cache) - { - return (T1 t1, T2 t2, T3 t3) => cache.GetOrCreate((t1, t2, t3), ignored => func(t1, t2, t3)); - } - - public static Func Create(Func func, IMemoryCache cache, MemoryCacheEntryOptions options) - { - return (T1 t1, T2 t2, T3 t3) => - { - var key = (t1, t2, t3); - if (!cache.TryGetValue(key, out var result)) - { - var entry = cache.CreateEntry(key); - result = func(t1, t2, t3); - entry.SetOptions(options); - entry.SetValue(result); - - // need to manually call dispose instead of having a using - // in case the factory passed in throws, in which case we - // do not want to add the entry to the cache - entry.Dispose(); - } - - return result; - }; - } - - [DebuggerStepThrough] - public static Func Create(Func func) - { - return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null")); - } - - [DebuggerStepThrough] - public static Func Create(Func func, MemoryCacheEntryOptions options) - { - return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null"), options); - } - - public static Func Create(Func func, IMemoryCache cache) - { - return (T1 t1, T2 t2, T3 t3, T4 t4) => cache.GetOrCreate((t1, t2, t3, t4), ignored => func(t1, t2, t3, t4)); - } - - public static Func Create(Func func, IMemoryCache cache, MemoryCacheEntryOptions options) - { - return (T1 t1, T2 t2, T3 t3, T4 t4) => - { - var key = (t1, t2, t3, t4); - if (!cache.TryGetValue(key, out var result)) - { - var entry = cache.CreateEntry(key); - result = func(t1, t2, t3, t4); - entry.SetOptions(options); - entry.SetValue(result); - - // need to manually call dispose instead of having a using - // in case the factory passed in throws, in which case we - // do not want to add the entry to the cache - entry.Dispose(); - } - - return result; - }; - } - - [DebuggerStepThrough] - public static Func Create(Func func) - { - return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null")); - } - - [DebuggerStepThrough] - public static Func Create(Func func, MemoryCacheEntryOptions options) - { - return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null"), options); - } - - public static Func Create(Func func, IMemoryCache cache) - { - return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5) => cache.GetOrCreate((t1, t2, t3, t4, t5), ignored => func(t1, t2, t3, t4, t5)); - } - - public static Func Create(Func func, IMemoryCache cache, MemoryCacheEntryOptions options) - { - return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5) => - { - var key = (t1, t2, t3, t4, t5); - if (!cache.TryGetValue(key, out var result)) - { - var entry = cache.CreateEntry(key); - result = func(t1, t2, t3, t4, t5); - entry.SetOptions(options); - entry.SetValue(result); - - // need to manually call dispose instead of having a using - // in case the factory passed in throws, in which case we - // do not want to add the entry to the cache - entry.Dispose(); - } - - return result; - }; - } - - [DebuggerStepThrough] - public static Func Create(Func func) - { - return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null")); - } - - [DebuggerStepThrough] - public static Func Create(Func func, MemoryCacheEntryOptions options) - { - return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null"), options); - } - - public static Func Create(Func func, IMemoryCache cache) - { - return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6) => cache.GetOrCreate((t1, t2, t3, t4, t5, t6), ignored => func(t1, t2, t3, t4, t5, t6)); - } - - public static Func Create(Func func, IMemoryCache cache, MemoryCacheEntryOptions options) - { - return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6) => - { - var key = (t1, t2, t3, t4, t5, t6); - if (!cache.TryGetValue(key, out var result)) - { - var entry = cache.CreateEntry(key); - result = func(t1, t2, t3, t4, t5, t6); - entry.SetOptions(options); - entry.SetValue(result); - - // need to manually call dispose instead of having a using - // in case the factory passed in throws, in which case we - // do not want to add the entry to the cache - entry.Dispose(); - } - - return result; - }; - } - - [DebuggerStepThrough] - public static Func Create(Func func) - { - return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null")); - } - - [DebuggerStepThrough] - public static Func Create(Func func, MemoryCacheEntryOptions options) - { - return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null"), options); - } - - public static Func Create(Func func, IMemoryCache cache) - { - return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7) => cache.GetOrCreate((t1, t2, t3, t4, t5, t6, t7), ignored => func(t1, t2, t3, t4, t5, t6, t7)); - } - - public static Func Create(Func func, IMemoryCache cache, MemoryCacheEntryOptions options) - { - return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7) => - { - var key = (t1, t2, t3, t4, t5, t6, t7); - if (!cache.TryGetValue(key, out var result)) - { - var entry = cache.CreateEntry(key); - result = func(t1, t2, t3, t4, t5, t6, t7); - entry.SetOptions(options); - entry.SetValue(result); - - // need to manually call dispose instead of having a using - // in case the factory passed in throws, in which case we - // do not want to add the entry to the cache - entry.Dispose(); - } - - return result; - }; - } - - [DebuggerStepThrough] - public static Func Create(Func func) - { - return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null")); - } - - [DebuggerStepThrough] - public static Func Create(Func func, MemoryCacheEntryOptions options) - { - return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null"), options); - } - - public static Func Create(Func func, IMemoryCache cache) - { - return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8) => cache.GetOrCreate((t1, t2, t3, t4, t5, t6, t7, t8), ignored => func(t1, t2, t3, t4, t5, t6, t7, t8)); - } - - public static Func Create(Func func, IMemoryCache cache, MemoryCacheEntryOptions options) - { - return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8) => - { - var key = (t1, t2, t3, t4, t5, t6, t7, t8); - if (!cache.TryGetValue(key, out var result)) - { - var entry = cache.CreateEntry(key); - result = func(t1, t2, t3, t4, t5, t6, t7, t8); - entry.SetOptions(options); - entry.SetValue(result); - - // need to manually call dispose instead of having a using - // in case the factory passed in throws, in which case we - // do not want to add the entry to the cache - entry.Dispose(); - } - - return result; - }; - } - - [DebuggerStepThrough] - public static Func Create(Func func) - { - return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null")); - } - - [DebuggerStepThrough] - public static Func Create(Func func, MemoryCacheEntryOptions options) - { - return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null"), options); - } - - public static Func Create(Func func, IMemoryCache cache) - { - return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9) => cache.GetOrCreate((t1, t2, t3, t4, t5, t6, t7, t8, t9), ignored => func(t1, t2, t3, t4, t5, t6, t7, t8, t9)); - } - - public static Func Create(Func func, IMemoryCache cache, MemoryCacheEntryOptions options) - { - return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9) => - { - var key = (t1, t2, t3, t4, t5, t6, t7, t8, t9); - if (!cache.TryGetValue(key, out var result)) - { - var entry = cache.CreateEntry(key); - result = func(t1, t2, t3, t4, t5, t6, t7, t8, t9); - entry.SetOptions(options); - entry.SetValue(result); - - // need to manually call dispose instead of having a using - // in case the factory passed in throws, in which case we - // do not want to add the entry to the cache - entry.Dispose(); - } - - return result; - }; - } - - [DebuggerStepThrough] - public static Func Create(Func func) - { - return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null")); - } - - [DebuggerStepThrough] - public static Func Create(Func func, MemoryCacheEntryOptions options) - { - return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null"), options); - } - - public static Func Create(Func func, IMemoryCache cache) - { - return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10) => cache.GetOrCreate((t1, t2, t3, t4, t5, t6, t7, t8, t9, t10), ignored => func(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10)); - } - - public static Func Create(Func func, IMemoryCache cache, MemoryCacheEntryOptions options) - { - return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10) => - { - var key = (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10); - if (!cache.TryGetValue(key, out var result)) - { - var entry = cache.CreateEntry(key); - result = func(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10); - entry.SetOptions(options); - entry.SetValue(result); - - // need to manually call dispose instead of having a using - // in case the factory passed in throws, in which case we - // do not want to add the entry to the cache - entry.Dispose(); - } - - return result; - }; - } - - [DebuggerStepThrough] - public static Func Create(Func func) - { - return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null")); - } - - [DebuggerStepThrough] - public static Func Create(Func func, MemoryCacheEntryOptions options) - { - return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null"), options); - } - - public static Func Create(Func func, IMemoryCache cache) - { - return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10, T11 t11) => cache.GetOrCreate((t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11), ignored => func(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11)); - } - - public static Func Create(Func func, IMemoryCache cache, MemoryCacheEntryOptions options) - { - return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10, T11 t11) => - { - var key = (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11); - if (!cache.TryGetValue(key, out var result)) - { - var entry = cache.CreateEntry(key); - result = func(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11); - entry.SetOptions(options); - entry.SetValue(result); - - // need to manually call dispose instead of having a using - // in case the factory passed in throws, in which case we - // do not want to add the entry to the cache - entry.Dispose(); - } - - return result; - }; - } - - [DebuggerStepThrough] - public static Func Create(Func func) - { - return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null")); - } - - [DebuggerStepThrough] - public static Func Create(Func func, MemoryCacheEntryOptions options) - { - return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null"), options); - } - - public static Func Create(Func func, IMemoryCache cache) - { - return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10, T11 t11, T12 t12) => cache.GetOrCreate((t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12), ignored => func(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12)); - } - - public static Func Create(Func func, IMemoryCache cache, MemoryCacheEntryOptions options) - { - return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10, T11 t11, T12 t12) => - { - var key = (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12); - if (!cache.TryGetValue(key, out var result)) - { - var entry = cache.CreateEntry(key); - result = func(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12); - entry.SetOptions(options); - entry.SetValue(result); - - // need to manually call dispose instead of having a using - // in case the factory passed in throws, in which case we - // do not want to add the entry to the cache - entry.Dispose(); - } - - return result; - }; - } - - [DebuggerStepThrough] - public static Func Create(Func func) - { - return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null")); - } - - [DebuggerStepThrough] - public static Func Create(Func func, MemoryCacheEntryOptions options) - { - return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null"), options); - } - - public static Func Create(Func func, IMemoryCache cache) - { - return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10, T11 t11, T12 t12, T13 t13) => cache.GetOrCreate((t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13), ignored => func(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13)); - } - - public static Func Create(Func func, IMemoryCache cache, MemoryCacheEntryOptions options) - { - return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10, T11 t11, T12 t12, T13 t13) => - { - var key = (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13); - if (!cache.TryGetValue(key, out var result)) - { - var entry = cache.CreateEntry(key); - result = func(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13); - entry.SetOptions(options); - entry.SetValue(result); - - // need to manually call dispose instead of having a using - // in case the factory passed in throws, in which case we - // do not want to add the entry to the cache - entry.Dispose(); - } - - return result; - }; - } - - [DebuggerStepThrough] - public static Func Create(Func func) - { - return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null")); - } - - [DebuggerStepThrough] - public static Func Create(Func func, MemoryCacheEntryOptions options) - { - return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null"), options); - } - - public static Func Create(Func func, IMemoryCache cache) - { - return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10, T11 t11, T12 t12, T13 t13, T14 t14) => cache.GetOrCreate((t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14), ignored => func(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14)); - } - - public static Func Create(Func func, IMemoryCache cache, MemoryCacheEntryOptions options) - { - return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10, T11 t11, T12 t12, T13 t13, T14 t14) => - { - var key = (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14); - if (!cache.TryGetValue(key, out var result)) - { - var entry = cache.CreateEntry(key); - result = func(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14); - entry.SetOptions(options); - entry.SetValue(result); - - // need to manually call dispose instead of having a using - // in case the factory passed in throws, in which case we - // do not want to add the entry to the cache - entry.Dispose(); - } - - return result; - }; - } - - [DebuggerStepThrough] - public static Func Create(Func func) - { - return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null")); - } - - [DebuggerStepThrough] - public static Func Create(Func func, MemoryCacheEntryOptions options) - { - return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null"), options); - } - - public static Func Create(Func func, IMemoryCache cache) - { - return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10, T11 t11, T12 t12, T13 t13, T14 t14, T15 t15) => cache.GetOrCreate((t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15), ignored => func(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15)); - } - - public static Func Create(Func func, IMemoryCache cache, MemoryCacheEntryOptions options) - { - return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10, T11 t11, T12 t12, T13 t13, T14 t14, T15 t15) => - { - var key = (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15); - if (!cache.TryGetValue(key, out var result)) - { - var entry = cache.CreateEntry(key); - result = func(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15); - entry.SetOptions(options); - entry.SetValue(result); - - // need to manually call dispose instead of having a using - // in case the factory passed in throws, in which case we - // do not want to add the entry to the cache - entry.Dispose(); - } - - return result; - }; - } - - [DebuggerStepThrough] - public static Func Create(Func func) - { - return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null")); - } - - [DebuggerStepThrough] - public static Func Create(Func func, MemoryCacheEntryOptions options) - { - return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null"), options); - } - - public static Func Create(Func func, IMemoryCache cache) - { - return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10, T11 t11, T12 t12, T13 t13, T14 t14, T15 t15, T16 t16) => cache.GetOrCreate((t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16), ignored => func(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16)); - } - - public static Func Create(Func func, IMemoryCache cache, MemoryCacheEntryOptions options) - { - return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10, T11 t11, T12 t12, T13 t13, T14 t14, T15 t15, T16 t16) => - { - var key = (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16); - if (!cache.TryGetValue(key, out var result)) - { - var entry = cache.CreateEntry(key); - result = func(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16); - entry.SetOptions(options); - entry.SetValue(result); - - // need to manually call dispose instead of having a using - // in case the factory passed in throws, in which case we - // do not want to add the entry to the cache - entry.Dispose(); - } - - return result; - }; - } - } -} +// +// Copyright Shuai Zhang. All rights reserved. +// Licensed under the GPLv3 license. See LICENSE file in the project root for full license information. +// + +using System; +using System.Diagnostics; +using Microsoft.Extensions.Caching.Memory; + +namespace Memoization +{ + public static partial class Memoization + { + [DebuggerStepThrough] + public static Func Create(Func func) + { + return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null")); + } + + [DebuggerStepThrough] + public static Func Create(Func func, MemoryCacheEntryOptions options) + { + return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null"), options); + } + + public static Func Create(Func func, IMemoryCache cache) + { + return (T1 t1) => cache.GetOrCreate((t1), ignored => func(t1)); + } + + public static Func Create(Func func, IMemoryCache cache, MemoryCacheEntryOptions options) + { + return (T1 t1) => + { + var key = (t1); + if (!cache.TryGetValue(key, out var result)) + { + var entry = cache.CreateEntry(key); + result = func(t1); + entry.SetOptions(options); + entry.SetValue(result); + + // need to manually call dispose instead of having a using + // in case the factory passed in throws, in which case we + // do not want to add the entry to the cache + entry.Dispose(); + } + + return result; + }; + } + + [DebuggerStepThrough] + public static Func Create(Func func) + { + return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null")); + } + + [DebuggerStepThrough] + public static Func Create(Func func, MemoryCacheEntryOptions options) + { + return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null"), options); + } + + public static Func Create(Func func, IMemoryCache cache) + { + return (T1 t1, T2 t2) => cache.GetOrCreate((t1, t2), ignored => func(t1, t2)); + } + + public static Func Create(Func func, IMemoryCache cache, MemoryCacheEntryOptions options) + { + return (T1 t1, T2 t2) => + { + var key = (t1, t2); + if (!cache.TryGetValue(key, out var result)) + { + var entry = cache.CreateEntry(key); + result = func(t1, t2); + entry.SetOptions(options); + entry.SetValue(result); + + // need to manually call dispose instead of having a using + // in case the factory passed in throws, in which case we + // do not want to add the entry to the cache + entry.Dispose(); + } + + return result; + }; + } + + [DebuggerStepThrough] + public static Func Create(Func func) + { + return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null")); + } + + [DebuggerStepThrough] + public static Func Create(Func func, MemoryCacheEntryOptions options) + { + return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null"), options); + } + + public static Func Create(Func func, IMemoryCache cache) + { + return (T1 t1, T2 t2, T3 t3) => cache.GetOrCreate((t1, t2, t3), ignored => func(t1, t2, t3)); + } + + public static Func Create(Func func, IMemoryCache cache, MemoryCacheEntryOptions options) + { + return (T1 t1, T2 t2, T3 t3) => + { + var key = (t1, t2, t3); + if (!cache.TryGetValue(key, out var result)) + { + var entry = cache.CreateEntry(key); + result = func(t1, t2, t3); + entry.SetOptions(options); + entry.SetValue(result); + + // need to manually call dispose instead of having a using + // in case the factory passed in throws, in which case we + // do not want to add the entry to the cache + entry.Dispose(); + } + + return result; + }; + } + + [DebuggerStepThrough] + public static Func Create(Func func) + { + return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null")); + } + + [DebuggerStepThrough] + public static Func Create(Func func, MemoryCacheEntryOptions options) + { + return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null"), options); + } + + public static Func Create(Func func, IMemoryCache cache) + { + return (T1 t1, T2 t2, T3 t3, T4 t4) => cache.GetOrCreate((t1, t2, t3, t4), ignored => func(t1, t2, t3, t4)); + } + + public static Func Create(Func func, IMemoryCache cache, MemoryCacheEntryOptions options) + { + return (T1 t1, T2 t2, T3 t3, T4 t4) => + { + var key = (t1, t2, t3, t4); + if (!cache.TryGetValue(key, out var result)) + { + var entry = cache.CreateEntry(key); + result = func(t1, t2, t3, t4); + entry.SetOptions(options); + entry.SetValue(result); + + // need to manually call dispose instead of having a using + // in case the factory passed in throws, in which case we + // do not want to add the entry to the cache + entry.Dispose(); + } + + return result; + }; + } + + [DebuggerStepThrough] + public static Func Create(Func func) + { + return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null")); + } + + [DebuggerStepThrough] + public static Func Create(Func func, MemoryCacheEntryOptions options) + { + return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null"), options); + } + + public static Func Create(Func func, IMemoryCache cache) + { + return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5) => cache.GetOrCreate((t1, t2, t3, t4, t5), ignored => func(t1, t2, t3, t4, t5)); + } + + public static Func Create(Func func, IMemoryCache cache, MemoryCacheEntryOptions options) + { + return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5) => + { + var key = (t1, t2, t3, t4, t5); + if (!cache.TryGetValue(key, out var result)) + { + var entry = cache.CreateEntry(key); + result = func(t1, t2, t3, t4, t5); + entry.SetOptions(options); + entry.SetValue(result); + + // need to manually call dispose instead of having a using + // in case the factory passed in throws, in which case we + // do not want to add the entry to the cache + entry.Dispose(); + } + + return result; + }; + } + + [DebuggerStepThrough] + public static Func Create(Func func) + { + return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null")); + } + + [DebuggerStepThrough] + public static Func Create(Func func, MemoryCacheEntryOptions options) + { + return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null"), options); + } + + public static Func Create(Func func, IMemoryCache cache) + { + return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6) => cache.GetOrCreate((t1, t2, t3, t4, t5, t6), ignored => func(t1, t2, t3, t4, t5, t6)); + } + + public static Func Create(Func func, IMemoryCache cache, MemoryCacheEntryOptions options) + { + return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6) => + { + var key = (t1, t2, t3, t4, t5, t6); + if (!cache.TryGetValue(key, out var result)) + { + var entry = cache.CreateEntry(key); + result = func(t1, t2, t3, t4, t5, t6); + entry.SetOptions(options); + entry.SetValue(result); + + // need to manually call dispose instead of having a using + // in case the factory passed in throws, in which case we + // do not want to add the entry to the cache + entry.Dispose(); + } + + return result; + }; + } + + [DebuggerStepThrough] + public static Func Create(Func func) + { + return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null")); + } + + [DebuggerStepThrough] + public static Func Create(Func func, MemoryCacheEntryOptions options) + { + return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null"), options); + } + + public static Func Create(Func func, IMemoryCache cache) + { + return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7) => cache.GetOrCreate((t1, t2, t3, t4, t5, t6, t7), ignored => func(t1, t2, t3, t4, t5, t6, t7)); + } + + public static Func Create(Func func, IMemoryCache cache, MemoryCacheEntryOptions options) + { + return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7) => + { + var key = (t1, t2, t3, t4, t5, t6, t7); + if (!cache.TryGetValue(key, out var result)) + { + var entry = cache.CreateEntry(key); + result = func(t1, t2, t3, t4, t5, t6, t7); + entry.SetOptions(options); + entry.SetValue(result); + + // need to manually call dispose instead of having a using + // in case the factory passed in throws, in which case we + // do not want to add the entry to the cache + entry.Dispose(); + } + + return result; + }; + } + + [DebuggerStepThrough] + public static Func Create(Func func) + { + return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null")); + } + + [DebuggerStepThrough] + public static Func Create(Func func, MemoryCacheEntryOptions options) + { + return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null"), options); + } + + public static Func Create(Func func, IMemoryCache cache) + { + return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8) => cache.GetOrCreate((t1, t2, t3, t4, t5, t6, t7, t8), ignored => func(t1, t2, t3, t4, t5, t6, t7, t8)); + } + + public static Func Create(Func func, IMemoryCache cache, MemoryCacheEntryOptions options) + { + return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8) => + { + var key = (t1, t2, t3, t4, t5, t6, t7, t8); + if (!cache.TryGetValue(key, out var result)) + { + var entry = cache.CreateEntry(key); + result = func(t1, t2, t3, t4, t5, t6, t7, t8); + entry.SetOptions(options); + entry.SetValue(result); + + // need to manually call dispose instead of having a using + // in case the factory passed in throws, in which case we + // do not want to add the entry to the cache + entry.Dispose(); + } + + return result; + }; + } + + [DebuggerStepThrough] + public static Func Create(Func func) + { + return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null")); + } + + [DebuggerStepThrough] + public static Func Create(Func func, MemoryCacheEntryOptions options) + { + return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null"), options); + } + + public static Func Create(Func func, IMemoryCache cache) + { + return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9) => cache.GetOrCreate((t1, t2, t3, t4, t5, t6, t7, t8, t9), ignored => func(t1, t2, t3, t4, t5, t6, t7, t8, t9)); + } + + public static Func Create(Func func, IMemoryCache cache, MemoryCacheEntryOptions options) + { + return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9) => + { + var key = (t1, t2, t3, t4, t5, t6, t7, t8, t9); + if (!cache.TryGetValue(key, out var result)) + { + var entry = cache.CreateEntry(key); + result = func(t1, t2, t3, t4, t5, t6, t7, t8, t9); + entry.SetOptions(options); + entry.SetValue(result); + + // need to manually call dispose instead of having a using + // in case the factory passed in throws, in which case we + // do not want to add the entry to the cache + entry.Dispose(); + } + + return result; + }; + } + + [DebuggerStepThrough] + public static Func Create(Func func) + { + return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null")); + } + + [DebuggerStepThrough] + public static Func Create(Func func, MemoryCacheEntryOptions options) + { + return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null"), options); + } + + public static Func Create(Func func, IMemoryCache cache) + { + return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10) => cache.GetOrCreate((t1, t2, t3, t4, t5, t6, t7, t8, t9, t10), ignored => func(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10)); + } + + public static Func Create(Func func, IMemoryCache cache, MemoryCacheEntryOptions options) + { + return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10) => + { + var key = (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10); + if (!cache.TryGetValue(key, out var result)) + { + var entry = cache.CreateEntry(key); + result = func(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10); + entry.SetOptions(options); + entry.SetValue(result); + + // need to manually call dispose instead of having a using + // in case the factory passed in throws, in which case we + // do not want to add the entry to the cache + entry.Dispose(); + } + + return result; + }; + } + + [DebuggerStepThrough] + public static Func Create(Func func) + { + return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null")); + } + + [DebuggerStepThrough] + public static Func Create(Func func, MemoryCacheEntryOptions options) + { + return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null"), options); + } + + public static Func Create(Func func, IMemoryCache cache) + { + return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10, T11 t11) => cache.GetOrCreate((t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11), ignored => func(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11)); + } + + public static Func Create(Func func, IMemoryCache cache, MemoryCacheEntryOptions options) + { + return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10, T11 t11) => + { + var key = (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11); + if (!cache.TryGetValue(key, out var result)) + { + var entry = cache.CreateEntry(key); + result = func(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11); + entry.SetOptions(options); + entry.SetValue(result); + + // need to manually call dispose instead of having a using + // in case the factory passed in throws, in which case we + // do not want to add the entry to the cache + entry.Dispose(); + } + + return result; + }; + } + + [DebuggerStepThrough] + public static Func Create(Func func) + { + return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null")); + } + + [DebuggerStepThrough] + public static Func Create(Func func, MemoryCacheEntryOptions options) + { + return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null"), options); + } + + public static Func Create(Func func, IMemoryCache cache) + { + return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10, T11 t11, T12 t12) => cache.GetOrCreate((t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12), ignored => func(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12)); + } + + public static Func Create(Func func, IMemoryCache cache, MemoryCacheEntryOptions options) + { + return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10, T11 t11, T12 t12) => + { + var key = (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12); + if (!cache.TryGetValue(key, out var result)) + { + var entry = cache.CreateEntry(key); + result = func(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12); + entry.SetOptions(options); + entry.SetValue(result); + + // need to manually call dispose instead of having a using + // in case the factory passed in throws, in which case we + // do not want to add the entry to the cache + entry.Dispose(); + } + + return result; + }; + } + + [DebuggerStepThrough] + public static Func Create(Func func) + { + return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null")); + } + + [DebuggerStepThrough] + public static Func Create(Func func, MemoryCacheEntryOptions options) + { + return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null"), options); + } + + public static Func Create(Func func, IMemoryCache cache) + { + return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10, T11 t11, T12 t12, T13 t13) => cache.GetOrCreate((t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13), ignored => func(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13)); + } + + public static Func Create(Func func, IMemoryCache cache, MemoryCacheEntryOptions options) + { + return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10, T11 t11, T12 t12, T13 t13) => + { + var key = (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13); + if (!cache.TryGetValue(key, out var result)) + { + var entry = cache.CreateEntry(key); + result = func(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13); + entry.SetOptions(options); + entry.SetValue(result); + + // need to manually call dispose instead of having a using + // in case the factory passed in throws, in which case we + // do not want to add the entry to the cache + entry.Dispose(); + } + + return result; + }; + } + + [DebuggerStepThrough] + public static Func Create(Func func) + { + return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null")); + } + + [DebuggerStepThrough] + public static Func Create(Func func, MemoryCacheEntryOptions options) + { + return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null"), options); + } + + public static Func Create(Func func, IMemoryCache cache) + { + return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10, T11 t11, T12 t12, T13 t13, T14 t14) => cache.GetOrCreate((t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14), ignored => func(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14)); + } + + public static Func Create(Func func, IMemoryCache cache, MemoryCacheEntryOptions options) + { + return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10, T11 t11, T12 t12, T13 t13, T14 t14) => + { + var key = (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14); + if (!cache.TryGetValue(key, out var result)) + { + var entry = cache.CreateEntry(key); + result = func(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14); + entry.SetOptions(options); + entry.SetValue(result); + + // need to manually call dispose instead of having a using + // in case the factory passed in throws, in which case we + // do not want to add the entry to the cache + entry.Dispose(); + } + + return result; + }; + } + + [DebuggerStepThrough] + public static Func Create(Func func) + { + return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null")); + } + + [DebuggerStepThrough] + public static Func Create(Func func, MemoryCacheEntryOptions options) + { + return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null"), options); + } + + public static Func Create(Func func, IMemoryCache cache) + { + return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10, T11 t11, T12 t12, T13 t13, T14 t14, T15 t15) => cache.GetOrCreate((t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15), ignored => func(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15)); + } + + public static Func Create(Func func, IMemoryCache cache, MemoryCacheEntryOptions options) + { + return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10, T11 t11, T12 t12, T13 t13, T14 t14, T15 t15) => + { + var key = (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15); + if (!cache.TryGetValue(key, out var result)) + { + var entry = cache.CreateEntry(key); + result = func(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15); + entry.SetOptions(options); + entry.SetValue(result); + + // need to manually call dispose instead of having a using + // in case the factory passed in throws, in which case we + // do not want to add the entry to the cache + entry.Dispose(); + } + + return result; + }; + } + + [DebuggerStepThrough] + public static Func Create(Func func) + { + return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null")); + } + + [DebuggerStepThrough] + public static Func Create(Func func, MemoryCacheEntryOptions options) + { + return Create(func, DefaultCache ?? throw new InvalidOperationException("Memoization.DefaultCache is null"), options); + } + + public static Func Create(Func func, IMemoryCache cache) + { + return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10, T11 t11, T12 t12, T13 t13, T14 t14, T15 t15, T16 t16) => cache.GetOrCreate((t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16), ignored => func(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16)); + } + + public static Func Create(Func func, IMemoryCache cache, MemoryCacheEntryOptions options) + { + return (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10, T11 t11, T12 t12, T13 t13, T14 t14, T15 t15, T16 t16) => + { + var key = (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16); + if (!cache.TryGetValue(key, out var result)) + { + var entry = cache.CreateEntry(key); + result = func(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16); + entry.SetOptions(options); + entry.SetValue(result); + + // need to manually call dispose instead of having a using + // in case the factory passed in throws, in which case we + // do not want to add the entry to the cache + entry.Dispose(); + } + + return result; + }; + } + } +} diff --git a/MicrosoftExtensions/FileProviders/WebHdfs/WebHdfs.Extensions.FileProviders/WebHdfsFileInfo.cs b/MicrosoftExtensions/FileProviders/WebHdfs/WebHdfs.Extensions.FileProviders/WebHdfsFileInfo.cs index c4f33565..e1d56c16 100644 --- a/MicrosoftExtensions/FileProviders/WebHdfs/WebHdfs.Extensions.FileProviders/WebHdfsFileInfo.cs +++ b/MicrosoftExtensions/FileProviders/WebHdfs/WebHdfs.Extensions.FileProviders/WebHdfsFileInfo.cs @@ -110,6 +110,43 @@ internal async Task GetFileStatuses() } } + private static DateTimeOffset FromUnixTimeMilliseconds(long milliseconds) + { +#if _NET462 || _NETSTANDARD2_0 + return DateTimeOffset.FromUnixTimeMilliseconds(milliseconds); +#else + // Number of days in a non-leap year + const int DaysPerYear = 365; + + // Number of days in 4 years + const int DaysPer4Years = (DaysPerYear * 4) + 1; // 1461 + + // Number of days in 100 years + const int DaysPer100Years = (DaysPer4Years * 25) - 1; // 36524 + + // Number of days in 400 years + const int DaysPer400Years = (DaysPer100Years * 4) + 1; // 146097 + + const int DaysTo1970 = (DaysPer400Years * 4) + (DaysPer100Years * 3) + (DaysPer4Years * 17) + DaysPerYear; // 719,162 + + const long UnixEpochTicks = TimeSpan.TicksPerDay * DaysTo1970; // 621,355,968,000,000,000 + const long UnixEpochMilliseconds = UnixEpochTicks / TimeSpan.TicksPerMillisecond; // 62,135,596,800,000 + + long minMilliseconds = (DateTime.MinValue.Ticks / TimeSpan.TicksPerMillisecond) - UnixEpochMilliseconds; + long maxMilliseconds = (DateTime.MaxValue.Ticks / TimeSpan.TicksPerMillisecond) - UnixEpochMilliseconds; + + if (milliseconds < minMilliseconds || milliseconds > maxMilliseconds) + { + throw new ArgumentOutOfRangeException( + nameof(milliseconds), + string.Format("Valid value between {0} and {1} (included).", minMilliseconds, maxMilliseconds)); + } + + long ticks = (milliseconds * TimeSpan.TicksPerMillisecond) + UnixEpochTicks; + return new DateTimeOffset(ticks, TimeSpan.Zero); +#endif + } + private void SetFileStatus(WebHdfsFileStatus fileStatus) { if (fileStatus == null || fileStatus == WebHdfsFileStatus.Empty) @@ -149,42 +186,5 @@ private async Task GetFileStatus() } } } - - private static DateTimeOffset FromUnixTimeMilliseconds(long milliseconds) - { -#if _NET462 || _NETSTANDARD2_0 - return DateTimeOffset.FromUnixTimeMilliseconds(milliseconds); -#else - // Number of days in a non-leap year - const int DaysPerYear = 365; - - // Number of days in 4 years - const int DaysPer4Years = (DaysPerYear * 4) + 1; // 1461 - - // Number of days in 100 years - const int DaysPer100Years = (DaysPer4Years * 25) - 1; // 36524 - - // Number of days in 400 years - const int DaysPer400Years = (DaysPer100Years * 4) + 1; // 146097 - - const int DaysTo1970 = (DaysPer400Years * 4) + (DaysPer100Years * 3) + (DaysPer4Years * 17) + DaysPerYear; // 719,162 - - const long UnixEpochTicks = TimeSpan.TicksPerDay * DaysTo1970; // 621,355,968,000,000,000 - const long UnixEpochMilliseconds = UnixEpochTicks / TimeSpan.TicksPerMillisecond; // 62,135,596,800,000 - - long minMilliseconds = (DateTime.MinValue.Ticks / TimeSpan.TicksPerMillisecond) - UnixEpochMilliseconds; - long maxMilliseconds = (DateTime.MaxValue.Ticks / TimeSpan.TicksPerMillisecond) - UnixEpochMilliseconds; - - if (milliseconds < minMilliseconds || milliseconds > maxMilliseconds) - { - throw new ArgumentOutOfRangeException( - nameof(milliseconds), - string.Format("Valid value between {0} and {1} (included).", minMilliseconds, maxMilliseconds)); - } - - long ticks = (milliseconds * TimeSpan.TicksPerMillisecond) + UnixEpochTicks; - return new DateTimeOffset(ticks, TimeSpan.Zero); -#endif - } } } diff --git a/PhiFailureDetector/PhiFailureDetector.UnitTest/LongIntervalHistory_UnitTest.cs b/PhiFailureDetector/PhiFailureDetector.UnitTest/LongIntervalHistory_UnitTest.cs index c9f410a9..229ea50a 100644 --- a/PhiFailureDetector/PhiFailureDetector.UnitTest/LongIntervalHistory_UnitTest.cs +++ b/PhiFailureDetector/PhiFailureDetector.UnitTest/LongIntervalHistory_UnitTest.cs @@ -29,8 +29,7 @@ public void Test_Baseline() CollectionAssert.AreEqual( new long[] { 3, 4, 5, 6, 7 }, - queueArray - ); + queueArray); Assert.AreEqual(25, arrivalWindow.Sum); Assert.AreEqual(5, arrivalWindow.Avg); diff --git a/RateLimiter/RateLimiter/GlobalSuppressions.cs b/RateLimiter/RateLimiter/GlobalSuppressions.cs new file mode 100644 index 00000000..9e5e0469 --- /dev/null +++ b/RateLimiter/RateLimiter/GlobalSuppressions.cs @@ -0,0 +1,16 @@ +// +// Copyright Shuai Zhang. All rights reserved. +// Licensed under the GPLv3 license. See LICENSE file in the project root for full license information. +// +// +// This file is used by Code Analysis to maintain SuppressMessage +// attributes that are applied to this project. +// Project-level suppressions either have no target or are given +// a specific target and scoped to a namespace, type, member, etc. + +using System.Diagnostics.CodeAnalysis; + +[assembly: SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1401:Fields should be private", Justification = "<挂起>", Scope = "member", Target = "~F:RateLimiter.RateLimiterBase.StopwatchProvider")] +[assembly: SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1401:Fields should be private", Justification = "<挂起>", Scope = "member", Target = "~F:RateLimiter.SmoothRateLimiter.storedPermits")] +[assembly: SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1401:Fields should be private", Justification = "<挂起>", Scope = "member", Target = "~F:RateLimiter.SmoothRateLimiter.maxPermits")] +[assembly: SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1401:Fields should be private", Justification = "<挂起>", Scope = "member", Target = "~F:RateLimiter.SmoothRateLimiter.stableInterval")] diff --git a/RateLimiter/RateLimiter/RateLimiterBase.cs b/RateLimiter/RateLimiter/RateLimiterBase.cs index 0365ef0c..c9cbb298 100644 --- a/RateLimiter/RateLimiter/RateLimiterBase.cs +++ b/RateLimiter/RateLimiter/RateLimiterBase.cs @@ -23,22 +23,25 @@ public abstract class RateLimiterBase : IRateLimiter private readonly IAsyncBlocker asyncBlocker; #endif - protected RateLimiterBase(IStopwatchProvider stopwatchProvider) #if !NET20 - : this(stopwatchProvider, null) - { - } - internal RateLimiterBase( IStopwatchProvider stopwatchProvider, IAsyncBlocker asyncBlocker) { this.asyncBlocker = asyncBlocker ?? TaskDelayAsyncBlocker.Instance; + this.StopwatchProvider = stopwatchProvider ?? throw new ArgumentNullException(nameof(stopwatchProvider)); + } + + protected RateLimiterBase(IStopwatchProvider stopwatchProvider) + : this(stopwatchProvider, null) + { + } #else + protected RateLimiterBase(IStopwatchProvider stopwatchProvider) { -#endif this.StopwatchProvider = stopwatchProvider ?? throw new ArgumentNullException(nameof(stopwatchProvider)); } +#endif public double PermitsPerSecond { diff --git a/RateLimiter/RateLimiter/SmoothRateLimiter.cs b/RateLimiter/RateLimiter/SmoothRateLimiter.cs index cf1c4892..bd31eeb5 100644 --- a/RateLimiter/RateLimiter/SmoothRateLimiter.cs +++ b/RateLimiter/RateLimiter/SmoothRateLimiter.cs @@ -18,19 +18,22 @@ public abstract class SmoothRateLimiter : RateLimiterBase private long nextFreeTicketTimestamp = 0; - protected SmoothRateLimiter(IStopwatchProvider stopwatchProvider) #if !NET20 - : this(stopwatchProvider, null) + internal SmoothRateLimiter(IStopwatchProvider stopwatchProvider, IAsyncBlocker asyncBlocker) + : base(stopwatchProvider, asyncBlocker) { } - internal SmoothRateLimiter(IStopwatchProvider stopwatchProvider, IAsyncBlocker asyncBlocker) - : base(stopwatchProvider, asyncBlocker) + protected SmoothRateLimiter(IStopwatchProvider stopwatchProvider) + : this(stopwatchProvider, null) + { + } #else + protected SmoothRateLimiter(IStopwatchProvider stopwatchProvider) : base(stopwatchProvider) -#endif { } +#endif protected abstract TimeSpan CoolDownInterval { get; } diff --git a/third_party/Directory.Build.props b/third_party/Directory.Build.props index 0afcd3db..c00ac28f 100644 --- a/third_party/Directory.Build.props +++ b/third_party/Directory.Build.props @@ -3,9 +3,14 @@ + + + false + + - +