Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/HotChocolate/Caching/src/Caching.Memory/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ private CacheEntry InsertNew(string key, TValue value)
var prev = Interlocked.CompareExchange(ref _ring[idx], newEntry, entry);
if (ReferenceEquals(prev, entry))
{
_map.TryRemove(prev.Key, out _);
_map.TryRemove(new KeyValuePair<string, CacheEntry>(prev.Key, prev));
_diagnostics.Evict();
return newEntry;
}
Expand Down Expand Up @@ -395,7 +395,7 @@ private CacheEntry InsertNew(string key, TValue value)
// accept this small window in which the map might have a dangling reference.
if (ReferenceEquals(prev, entry))
{
_map.TryRemove(prev.Key, out _);
_map.TryRemove(new KeyValuePair<string, CacheEntry>(prev.Key, prev));
_diagnostics.Evict();
return newEntry;
}
Expand Down
4 changes: 2 additions & 2 deletions src/HotChocolate/Caching/src/Caching.Memory/Cache`2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ private CacheEntry InsertNew(TKey key, TValue value)
var prev = Interlocked.CompareExchange(ref _ring[idx], newEntry, entry);
if (ReferenceEquals(prev, entry))
{
_map.TryRemove(prev.Key, out _);
_map.TryRemove(new KeyValuePair<TKey, CacheEntry>(prev.Key, prev));
_diagnostics.Evict();
return newEntry;
}
Expand Down Expand Up @@ -254,7 +254,7 @@ private CacheEntry InsertNew(TKey key, TValue value)
// accept this small window in which the map might have a dangling reference.
if (ReferenceEquals(prev, entry))
{
_map.TryRemove(prev.Key, out _);
_map.TryRemove(new KeyValuePair<TKey, CacheEntry>(prev.Key, prev));
_diagnostics.Evict();
return newEntry;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,30 @@ public void TryGet_Should_Keep_Distinct_Keys_Separate_When_Hashes_Collide()
Assert.Equal("second", second);
}

[Fact]
public void GetOrCreate_Should_KeepEntry_When_DuplicateOfItsKeyIsEvicted()
{
// arrange
// the factory of k creates k again, which leaves a second entry for k like two threads that miss together
var cache = new Cache<CacheKey, string>(4);
var k = new CacheKey(1);
cache.GetOrCreate(new CacheKey(0), _ => "x");
cache.GetOrCreate(k, _ => cache.GetOrCreate(k, _ => "k"));
cache.GetOrCreate(new CacheKey(2), _ => "a");
cache.GetOrCreate(new CacheKey(3), _ => "b");
cache.TryGet(k, out _);

// act
cache.GetOrCreate(new CacheKey(4), _ => "c");

// assert
Assert.True(cache.TryGet(k, out var value));
Assert.Equal("k", value);
Assert.Equal(
new[] { new CacheKey(3), k, new CacheKey(4), new CacheKey(2) },
cache.GetKeys().ToArray());
}

private readonly record struct CacheKey(int Value)
{
public override int GetHashCode() => 1;
Expand Down
21 changes: 21 additions & 0 deletions src/HotChocolate/Caching/test/Caching.Memory.Tests/CacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,27 @@ public void Evict_Items_That_Were_Not_Recently_Access_When_Cache_Is_Full()
key => Assert.Equal("j", key));
}

[Fact]
public void GetOrCreate_Should_KeepEntry_When_DuplicateOfItsKeyIsEvicted()
{
// arrange
// the factory of k creates k again, which leaves a second entry for k like two threads that miss together
var cache = new Cache<string>(4);
cache.GetOrCreate("x", _ => "x");
cache.GetOrCreate("k", _ => cache.GetOrCreate("k", _ => "k"));
cache.GetOrCreate("a", _ => "a");
cache.GetOrCreate("b", _ => "b");
cache.TryGet("k", out _);

// act
cache.GetOrCreate("c", _ => "c");

// assert
Assert.True(cache.TryGet("k", out var value));
Assert.Equal("k", value);
Assert.Equal(new[] { "b", "k", "c", "a" }, cache.GetKeys().ToArray());
}

[Fact]
public void TryGet_Hit_IncrementsHitCounter()
{
Expand Down