Skip to content

Commit 5b27e7c

Browse files
committed
Improves setting expirations on multiple cache keys
Ensures that setting expirations for multiple cache keys correctly publishes invalidation messages and handles empty expiration dictionaries efficiently. This prevents unnecessary message bus calls when no expirations are set, and guarantees invalidation messages are sent with the correct keys after setting the expirations on the distributed cache.
1 parent cdff01f commit 5b27e7c

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

src/Foundatio/Caching/HybridAwareCacheClient.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,16 @@ public async Task SetExpirationAsync(string key, TimeSpan expiresIn)
199199
await _messagePublisher.PublishAsync(new HybridCacheClient.InvalidateCache { CacheId = _cacheId, Keys = [key] }).AnyContext();
200200
}
201201

202-
public Task SetAllExpirationAsync(IDictionary<string, TimeSpan?> expirations)
202+
public async Task SetAllExpirationAsync(IDictionary<string, TimeSpan?> expirations)
203203
{
204204
if (expirations is null)
205205
throw new ArgumentNullException(nameof(expirations));
206206

207-
return _distributedCache.SetAllExpirationAsync(expirations);
208-
// TODO: await _messagePublisher.PublishAsync(new HybridCacheClient.InvalidateCache { CacheId = _cacheId, Keys = [key] }).AnyContext();
207+
if (expirations.Count is 0)
208+
return;
209+
210+
await _distributedCache.SetAllExpirationAsync(expirations).AnyContext();
211+
await _messagePublisher.PublishAsync(new HybridCacheClient.InvalidateCache { CacheId = _cacheId, Keys = expirations.Keys.ToArray() }).AnyContext();
209212
}
210213

211214
public async Task<double> SetIfHigherAsync(string key, double value, TimeSpan? expiresIn = null)

src/Foundatio/Caching/HybridCacheClient.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -420,13 +420,12 @@ public async Task SetAllExpirationAsync(IDictionary<string, TimeSpan?> expiratio
420420
if (expirations is null)
421421
throw new ArgumentNullException(nameof(expirations));
422422

423+
if (expirations.Count is 0)
424+
return;
425+
423426
await _localCache.SetAllExpirationAsync(expirations).AnyContext();
424427
await _distributedCache.SetAllExpirationAsync(expirations).AnyContext();
425-
426-
if (expirations.Count > 0)
427-
{
428-
await _messageBus.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = expirations.Keys.ToArray() }).AnyContext();
429-
}
428+
await _messageBus.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = expirations.Keys.ToArray() }).AnyContext();
430429
}
431430

432431
public async Task<double> SetIfHigherAsync(string key, double value, TimeSpan? expiresIn = null)

src/Foundatio/Caching/InMemoryCacheClient.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
4+
using System.Collections.ObjectModel;
45
using System.Linq;
56
using System.Reflection;
67
using System.Text.RegularExpressions;
@@ -953,8 +954,12 @@ public Task<bool> ExistsAsync(string key)
953954
if (keys is null)
954955
throw new ArgumentNullException(nameof(keys));
955956

956-
var result = new Dictionary<string, TimeSpan?>();
957-
foreach (string key in keys)
957+
string[] keysArray = keys.ToArray();
958+
if (keysArray.Length is 0)
959+
return Task.FromResult<IDictionary<string, TimeSpan?>>(ReadOnlyDictionary<string, TimeSpan?>.Empty);
960+
961+
var result = new Dictionary<string, TimeSpan?>(keysArray.Length);
962+
foreach (string key in keysArray)
958963
{
959964
if (String.IsNullOrEmpty(key))
960965
throw new ArgumentNullException(nameof(key), "Key cannot be null or empty");

0 commit comments

Comments
 (0)