From 6b87785026ca57d6f41cff87ddbd066362f3cacc Mon Sep 17 00:00:00 2001 From: vnugent Date: Wed, 14 Feb 2024 14:23:53 -0500 Subject: Squashed commit of the following: commit 456ead9bc8b0f61357bae93152ad0403c4940101 Author: vnugent Date: Tue Feb 13 14:46:35 2024 -0500 fix: #1 shared cluster index on linux & latested core updates commit a481d63f964a5d5204cac2e95141f37f9a28d573 Author: vnugent Date: Tue Jan 23 15:43:50 2024 -0500 cache extension api tweaks --- .../src/BlobCache.cs | 76 +++++++++++----------- 1 file changed, 38 insertions(+), 38 deletions(-) (limited to 'lib/VNLib.Data.Caching.ObjectCache/src/BlobCache.cs') diff --git a/lib/VNLib.Data.Caching.ObjectCache/src/BlobCache.cs b/lib/VNLib.Data.Caching.ObjectCache/src/BlobCache.cs index 5a425ec..7b2b3b1 100644 --- a/lib/VNLib.Data.Caching.ObjectCache/src/BlobCache.cs +++ b/lib/VNLib.Data.Caching.ObjectCache/src/BlobCache.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: VNLib.Data.Caching.ObjectCache @@ -34,10 +34,10 @@ namespace VNLib.Data.Caching.ObjectCache /// /// A general purpose binary data storage /// - public sealed class BlobCache : LRUCache, IBlobCache, IMemoryCacheEntryFactory + public sealed class BlobCache : LRUCache, IBlobCache { private bool disposedValue; - private IPersistantCacheStore? _persistance; + private readonly IPersistantCacheStore? _persistance; /// public override bool IsReadOnly { get; } @@ -62,17 +62,12 @@ namespace VNLib.Data.Caching.ObjectCache public BlobCache(uint bucketId, int maxCapacity, ICacheEntryMemoryManager manager, IPersistantCacheStore? store) :base(maxCapacity, StringComparer.Ordinal) { - if(maxCapacity < 1) - { - throw new ArgumentException("The maxium capacity of the store must be a positive integer larger than 0", nameof(maxCapacity)); - } + ArgumentOutOfRangeException.ThrowIfLessThan(maxCapacity, 1); + ArgumentNullException.ThrowIfNull(manager); BucketId = bucketId; - _persistance = store; - - MemoryManager = manager ?? throw new ArgumentNullException(nameof(manager)); - + MemoryManager = manager; MaxCapacity = maxCapacity; //Update the lookup table size @@ -88,11 +83,11 @@ namespace VNLib.Data.Caching.ObjectCache return false; } //Use the persistant cache - return _persistance.OnCacheMiss(BucketId, key, this, out value); + return _persistance.OnCacheMiss(BucketId, key, MemoryManager, out value); } /// - protected override void Evicted(ref KeyValuePair evicted) + protected override void Evicted(ref readonly KeyValuePair evicted) { try { @@ -109,6 +104,8 @@ namespace VNLib.Data.Caching.ObjectCache /// public bool TryChangeKey(string objectId, string newId, out CacheEntry entry) { + ObjectDisposedException.ThrowIf(disposedValue, this); + //Try to get the node at the current key if (LookupTable.Remove(objectId, out LinkedListNode> ? node)) { @@ -137,6 +134,8 @@ namespace VNLib.Data.Caching.ObjectCache /// public override bool Remove(string key) { + ObjectDisposedException.ThrowIf(disposedValue, this); + //Remove from persistant store also _persistance?.OnEntryDeleted(BucketId, key); @@ -161,33 +160,23 @@ namespace VNLib.Data.Caching.ObjectCache /// public override void Clear() { - //Start from first node - LinkedListNode>? node = List.First; + ObjectDisposedException.ThrowIf(disposedValue, this); - //Classic ll node itteration - while(node != null) - { - //Dispose the cache entry - node.ValueRef.Value.Dispose(); - - //Move to next node - node = node.Next; - } - - //empty all cache entires in the store - base.Clear(); + ClearInternal(); } /// public bool Remove(string objectId, out CacheEntry entry) { + ObjectDisposedException.ThrowIf(disposedValue, this); + //Try to get the stored object - if(TryGetValue(objectId, out entry)) + if (TryGetValue(objectId, out entry)) { //remove the entry and bypass the disposal bool result = base.Remove(objectId); - Debug.Assert(result == true, "The cache entry was found in the table, but failed to remove"); + Debug.Assert(result, "The cache entry was found in the table, but failed to remove"); return true; } @@ -196,6 +185,25 @@ namespace VNLib.Data.Caching.ObjectCache return false; } + private void ClearInternal() + { + //Start from first node + LinkedListNode>? node = List.First; + + //Classic ll node itteration + while (node != null) + { + //Dispose the cache entry + node.ValueRef.Value.Dispose(); + + //Move to next node + node = node.Next; + } + + //empty all cache entires in the store + base.Clear(); + } + /// void Dispose(bool disposing) { @@ -203,7 +211,7 @@ namespace VNLib.Data.Caching.ObjectCache { if (disposing) { - Clear(); + ClearInternal(); } disposedValue = true; } @@ -216,13 +224,5 @@ namespace VNLib.Data.Caching.ObjectCache Dispose(disposing: true); GC.SuppressFinalize(this); } - - - /// - CacheEntry IMemoryCacheEntryFactory.CreateEntry(ReadOnlySpan entryData) - { - //Create entry from the internal heap - return CacheEntry.Create(entryData, MemoryManager); - } } } -- cgit