Skip to content

Commit 1c5da38

Browse files
committed
MemoryCache should now refcount blobs right?
1 parent c4d96ab commit 1c5da38

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

src/Imazen.Routing/Caching/MemoryCache.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public Task<CacheFetchResult> CacheFetch(IBlobCacheRequest request, Cancellation
109109
if (__cache.TryGetValue(request.CacheKeyHashString, out var entry))
110110
{
111111
entry.UsageTracker.Used();
112-
return Task.FromResult(BlobCacheFetchFailure.OkResult(entry.BlobWrapper));
112+
return Task.FromResult(BlobCacheFetchFailure.OkResult(entry.BlobWrapper.ForkReference()));
113113
}
114114

115115
return Task.FromResult(BlobCacheFetchFailure.MissResult(this, this));
@@ -120,6 +120,7 @@ public Task<CacheFetchResult> CacheFetch(IBlobCacheRequest request, Cancellation
120120
private bool TryRemove(string cacheKey, [MaybeNullWhen(false)] out CacheEntry removed)
121121
{
122122
if (!__cache.TryRemove(cacheKey, out removed)) return false;
123+
removed.BlobWrapper.Dispose();
123124
Interlocked.Add(ref memoryUsedSync, -removed.BlobWrapper.EstimateAllocatedBytes ?? 0);
124125
Interlocked.Decrement(ref itemCountSync);
125126
return true;
@@ -154,12 +155,10 @@ private bool TryEnsureCapacity(long size)
154155
return true;
155156
}
156157

157-
private bool TryAdd(string cacheKey, IBlobWrapper blob)
158+
private async Task<bool> TryAdd(string cacheKey, IBlobWrapper blob)
158159
{
159-
if (!blob.IsReusable)
160-
{
161-
throw new InvalidOperationException("Cannot cache a blob that is not natively reusable");
162-
}
160+
161+
163162
if (blob.EstimateAllocatedBytes == null)
164163
{
165164
throw new InvalidOperationException("Cannot cache a blob that does not have an EstimateAllocatedBytes");
@@ -182,7 +181,11 @@ private bool TryAdd(string cacheKey, IBlobWrapper blob)
182181
{
183182
return false; // Can't make space? That's odd.
184183
}
185-
184+
if (!blob.IsReusable)
185+
{
186+
//await blob.EnsureReusable();
187+
throw new InvalidOperationException("Cannot cache a blob that is not natively reusable");
188+
}
186189
var entry = new CacheEntry(cacheKey, blob, UsageTracker.Create());
187190
if (entry == __cache.AddOrUpdate(cacheKey, entry, (_, existing) => existing with { BlobWrapper = blob }))
188191
{
@@ -204,14 +207,14 @@ private void IncrementUsage(string cacheKeyHashString)
204207
}
205208
}
206209
private IEnumerable<CacheEntry> AllEntries => __cache.Values;
207-
public Task<CodeResult> CachePut(ICacheEventDetails e, CancellationToken cancellationToken = default)
210+
public async Task<CodeResult> CachePut(ICacheEventDetails e, CancellationToken cancellationToken = default)
208211
{
209212
if (e.Result?.TryUnwrap(out var blob) == true)
210213
{
211-
TryAdd(e.OriginalRequest.CacheKeyHashString, blob);
214+
await TryAdd(e.OriginalRequest.CacheKeyHashString, blob.ForkReference());
212215
}
213216

214-
return Task.FromResult(CodeResult.Ok());
217+
return CodeResult.Ok();
215218
}
216219

217220
public Task<CodeResult<IAsyncEnumerable<IBlobStorageReference>>> CacheSearchByTag(SearchableBlobTag tag, CancellationToken cancellationToken = default)
@@ -268,4 +271,4 @@ public ValueTask<IBlobCacheHealthDetails> CacheHealthCheck(CancellationToken can
268271
// We could be low on memory, but we wouldn't turn off features, we'd just evict smarter.
269272
return new ValueTask<IBlobCacheHealthDetails>(BlobCacheHealthDetails.FullHealth(InitialCacheCapabilities));
270273
}
271-
}
274+
}

0 commit comments

Comments
 (0)