From 2ae018af277b808786cf398c689910bc016e7ef0 Mon Sep 17 00:00:00 2001 From: vnugent Date: Tue, 10 Sep 2024 18:59:06 -0400 Subject: fix: zero/unsafezero with data types > sizeof(byte) --- lib/Utils/src/Extensions/CacheExtensions.cs | 77 ++++++++--------------------- 1 file changed, 20 insertions(+), 57 deletions(-) (limited to 'lib/Utils/src/Extensions') diff --git a/lib/Utils/src/Extensions/CacheExtensions.cs b/lib/Utils/src/Extensions/CacheExtensions.cs index 665e282..7efc36d 100644 --- a/lib/Utils/src/Extensions/CacheExtensions.cs +++ b/lib/Utils/src/Extensions/CacheExtensions.cs @@ -52,10 +52,7 @@ namespace VNLib.Utils.Extensions /// public static void StoreRecord(this IDictionary store, TKey key, T record) where T : ICacheable { - if (store is null) - { - throw new ArgumentNullException(nameof(store)); - } + ArgumentNullException.ThrowIfNull(store); T ?oldRecord = default; lock (store) @@ -128,10 +125,7 @@ namespace VNLib.Utils.Extensions /// public static ERRNO TryGetOrEvictRecord(this IDictionary store, TKey key, out T? value) where T : ICacheable { - if (store is null) - { - throw new ArgumentNullException(nameof(store)); - } + ArgumentNullException.ThrowIfNull(store); value = default; //Cache current date time before entering the lock @@ -194,10 +188,7 @@ namespace VNLib.Utils.Extensions /// True if the record was found and evicted public static bool EvictRecord(this IDictionary store, TKey key) where T : ICacheable { - if (store is null) - { - throw new ArgumentNullException(nameof(store)); - } + ArgumentNullException.ThrowIfNull(store); T? record = default; lock (store) @@ -218,10 +209,8 @@ namespace VNLib.Utils.Extensions /// /// /// - public static void CollectRecords(this IDictionary store) where T : ICacheable - { - CollectRecords(store, DateTime.UtcNow); - } + public static void CollectRecords(this IDictionary store) where T : ICacheable + => CollectRecords(store, DateTime.UtcNow); /// /// Evicts all expired records from the store @@ -232,10 +221,7 @@ namespace VNLib.Utils.Extensions /// A time that specifies the time which expired records should be evicted public static void CollectRecords(this IDictionary store, DateTime validAfter) where T : ICacheable { - if (store is null) - { - throw new ArgumentNullException(nameof(store)); - } + ArgumentNullException.ThrowIfNull(store); //Build a query to get the keys that belong to the expired records IEnumerable> expired = store.Where(s => s.Value.Expires < validAfter); //temp list for expired records @@ -273,15 +259,8 @@ namespace VNLib.Utils.Extensions /// A callback method that will be passed the record to use within an exclusive context public static void UseRecord(this IDictionary store, TKey key, TState state, Action useCtx) where T: ICacheable { - if (store is null) - { - throw new ArgumentNullException(nameof(store)); - } - - if (useCtx is null) - { - throw new ArgumentNullException(nameof(useCtx)); - } + ArgumentNullException.ThrowIfNull(store); + ArgumentNullException.ThrowIfNull(useCtx); lock (store) { @@ -303,15 +282,8 @@ namespace VNLib.Utils.Extensions /// A callback method that will be passed the record to use within an exclusive context public static void UseRecord(this IDictionary store, TKey key, Action useCtx) where T : ICacheable { - if (store is null) - { - throw new ArgumentNullException(nameof(store)); - } - - if (useCtx is null) - { - throw new ArgumentNullException(nameof(useCtx)); - } + ArgumentNullException.ThrowIfNull(store); + ArgumentNullException.ThrowIfNull(useCtx); lock (store) { @@ -335,17 +307,15 @@ namespace VNLib.Utils.Extensions /// A user-token type state parameter to pass to the use callback method /// A callback method that will be passed the record to use within an exclusive context /// If the record is found, but is expired, the record is evicted from the store. The callback is never invoked - public static void UseIfValid(this IDictionary store, TKey key, TState state, Action useCtx) where T : ICacheable + public static void UseIfValid( + this IDictionary store, + TKey key, + TState state, + Action useCtx + ) where T : ICacheable { - if (store is null) - { - throw new ArgumentNullException(nameof(store)); - } - - if (useCtx is null) - { - throw new ArgumentNullException(nameof(useCtx)); - } + ArgumentNullException.ThrowIfNull(store); + ArgumentNullException.ThrowIfNull(useCtx); DateTime now = DateTime.UtcNow; T? record; @@ -376,15 +346,8 @@ namespace VNLib.Utils.Extensions /// If the record is found, but is expired, the record is evicted from the store. The callback is never invoked public static void UseIfValid(this IDictionary store, TKey key, Action useCtx) where T : ICacheable { - if (store is null) - { - throw new ArgumentNullException(nameof(store)); - } - - if (useCtx is null) - { - throw new ArgumentNullException(nameof(useCtx)); - } + ArgumentNullException.ThrowIfNull(store); + ArgumentNullException.ThrowIfNull(useCtx); DateTime now = DateTime.UtcNow; T? record; -- cgit