Skip to content
Prev Previous commit
Next Next commit
Refactoring tuple naming to PascalCase. Other code style changes.
  • Loading branch information
profet23 authored and github-actions committed Jun 4, 2025
commit d264e4db89cb7c518551f3ca591e65c9857e2f48
20 changes: 10 additions & 10 deletions src/Middleware/OutputCaching/src/Memory/MemoryOutputCacheStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Microsoft.AspNetCore.OutputCaching.Memory;
internal sealed class MemoryOutputCacheStore : IOutputCacheStore
{
private readonly MemoryCache _cache;
private readonly Dictionary<string, HashSet<(string key, Guid entryId)>> _taggedEntries = [];
private readonly Dictionary<string, HashSet<(string Key, Guid EntryId)>> _taggedEntries = [];
private readonly object _tagsLock = new();

internal MemoryOutputCacheStore(MemoryCache cache)
Expand All @@ -21,7 +21,7 @@ internal MemoryOutputCacheStore(MemoryCache cache)
}

// For testing
internal Dictionary<string, HashSet<string>> TaggedEntries => _taggedEntries.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.Select(t => t.key).ToHashSet());
internal Dictionary<string, HashSet<string>> TaggedEntries => _taggedEntries.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.Select(t => t.Key).ToHashSet());

public ValueTask EvictByTagAsync(string tag, CancellationToken cancellationToken)
{
Expand All @@ -31,7 +31,7 @@ public ValueTask EvictByTagAsync(string tag, CancellationToken cancellationToken
{
if (_taggedEntries.TryGetValue(tag, out var keys))
{
if (keys != null && keys.Count > 0)
if (keys is { Count: > 0 })
{
// If MemoryCache changed to run eviction callbacks inline in Remove, iterating over keys could throw
// To prevent allocating a copy of the keys we check if the eviction callback ran,
Expand All @@ -41,9 +41,9 @@ public ValueTask EvictByTagAsync(string tag, CancellationToken cancellationToken
while (i > 0)
{
var oldCount = keys.Count;
foreach (var tuple in keys)
foreach (var (key, _) in keys)
{
_cache.Remove(tuple.key);
_cache.Remove(key);
i--;
if (oldCount != keys.Count)
{
Expand Down Expand Up @@ -99,7 +99,7 @@ public ValueTask SetAsync(string key, byte[] value, string[]? tags, TimeSpan val

Debug.Assert(keys != null);

keys.Add(ValueTuple.Create(key, entryId));
keys.Add((key, entryId));
}

SetEntry(key, value, tags, validFor, entryId);
Expand All @@ -126,17 +126,17 @@ private void SetEntry(string key, byte[] value, string[]? tags, TimeSpan validFo
if (tags is { Length: > 0 })
{
// Remove cache keys from tag lists when the entry is evicted
options.RegisterPostEvictionCallback(RemoveFromTags, ValueTuple.Create(tags, entryId));
options.RegisterPostEvictionCallback(RemoveFromTags, (tags, entryId));
}

_cache.Set(key, value, options);
}

void RemoveFromTags(object key, object? value, EvictionReason reason, object? state)
private void RemoveFromTags(object key, object? value, EvictionReason reason, object? state)
{
Debug.Assert(state != null);

var (tags, entryId) = ((string[] tags, Guid entryId))state;
var (tags, entryId) = ((string[] Tags, Guid EntryId))state;

Debug.Assert(tags != null);
Debug.Assert(tags.Length > 0);
Expand All @@ -149,7 +149,7 @@ void RemoveFromTags(object key, object? value, EvictionReason reason, object? st
{
if (_taggedEntries.TryGetValue(tag, out var tagged))
{
tagged.Remove((key: (string)key, entryId));
tagged.Remove((Key: (string)key, entryId));

// Remove the collection if there is no more keys in it
if (tagged.Count == 0)
Expand Down