Skip to content

Commit

Permalink
work around ToDistributedCacheEntryOptions memoize utility method
Browse files Browse the repository at this point in the history
  • Loading branch information
mgravell committed Jul 23, 2024
1 parent 661ffa7 commit 7e6aa49
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
12 changes: 11 additions & 1 deletion src/Caching/Hybrid/src/Internal/DefaultHybridCache.L2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,19 @@ private DistributedCacheEntryOptions GetOptions(HybridCacheEntryOptions? options
DistributedCacheEntryOptions? result = null;
if (options is not null && options.Expiration.HasValue && options.Expiration.GetValueOrDefault() != _defaultExpiration)
{
result = options.ToDistributedCacheEntryOptions();
result = ToDistributedCacheEntryOptions(options);
}
return result ?? _defaultDistributedCacheExpiration;

#if NET8_0_OR_GREATER
// internal method memoizes this allocation; since it is "init", it is immutable (outside reflection)
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = nameof(ToDistributedCacheEntryOptions))]
extern static DistributedCacheEntryOptions? ToDistributedCacheEntryOptions(HybridCacheEntryOptions options);
#else
// withoug that helper method, we'll just eat the alloc (down-level TFMs)
static DistributedCacheEntryOptions ToDistributedCacheEntryOptions(HybridCacheEntryOptions options)
=> new() { AbsoluteExpirationRelativeToNow = options.Expiration };
#endif
}

internal void SetL1<T>(string key, CacheItem<T> value, HybridCacheEntryOptions? options)
Expand Down
16 changes: 9 additions & 7 deletions src/Caching/Hybrid/test/L2Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ static string CreateString(bool work = false)
return Guid.NewGuid().ToString();
}

static readonly HybridCacheEntryOptions _noL1 = new() { Flags = HybridCacheEntryFlags.DisableLocalCache };
private static readonly HybridCacheEntryOptions Expiry = new() { Expiration = TimeSpan.FromMinutes(3.5) };

private static readonly HybridCacheEntryOptions ExpiryNoL1 = new() { Flags = HybridCacheEntryFlags.DisableLocalCache, Expiration = TimeSpan.FromMinutes(3.5) };

[Theory]
[InlineData(true)]
Expand All @@ -63,7 +65,7 @@ public async Task AssertL2Operations_Immutable(bool buffers)
Log.WriteLine("Reading without L1...");
for (var i = 0; i < 5; i++)
{
var x = await cache.GetOrCreateAsync(Me(), ct => new ValueTask<string>(CreateString()), _noL1);
var x = await cache.GetOrCreateAsync(Me(), ct => new ValueTask<string>(CreateString()), ExpiryNoL1);
Assert.Equal(s, x);
Assert.NotSame(s, x);
}
Expand Down Expand Up @@ -103,13 +105,13 @@ public async Task AssertL2Operations_Mutable(bool buffers)
using var provider = GetDefaultCache(buffers, out var cache);
var backend = Assert.IsAssignableFrom<LoggingCache>(cache.BackendCache);
Log.WriteLine("Inventing key...");
var s = await cache.GetOrCreateAsync(Me(), ct => new ValueTask<Foo>(new Foo { Value = CreateString(true) }));
var s = await cache.GetOrCreateAsync(Me(), ct => new ValueTask<Foo>(new Foo { Value = CreateString(true) }), Expiry);
Assert.Equal(2, backend.OpCount); // GET, SET

Log.WriteLine("Reading with L1...");
for (var i = 0; i < 5; i++)
{
var x = await cache.GetOrCreateAsync(Me(), ct => new ValueTask<Foo>(new Foo { Value = CreateString() }));
var x = await cache.GetOrCreateAsync(Me(), ct => new ValueTask<Foo>(new Foo { Value = CreateString() }), Expiry);
Assert.Equal(s.Value, x.Value);
Assert.NotSame(s, x);
}
Expand All @@ -118,7 +120,7 @@ public async Task AssertL2Operations_Mutable(bool buffers)
Log.WriteLine("Reading without L1...");
for (var i = 0; i < 5; i++)
{
var x = await cache.GetOrCreateAsync(Me(), ct => new ValueTask<Foo>(new Foo { Value = CreateString() }), _noL1);
var x = await cache.GetOrCreateAsync(Me(), ct => new ValueTask<Foo>(new Foo { Value = CreateString() }), ExpiryNoL1);
Assert.Equal(s.Value, x.Value);
Assert.NotSame(s, x);
}
Expand All @@ -129,7 +131,7 @@ public async Task AssertL2Operations_Mutable(bool buffers)
await cache.SetAsync(Me(), s);
for (var i = 0; i < 5; i++)
{
var x = await cache.GetOrCreateAsync(Me(), ct => new ValueTask<Foo>(new Foo { Value = CreateString() }));
var x = await cache.GetOrCreateAsync(Me(), ct => new ValueTask<Foo>(new Foo { Value = CreateString() }), Expiry);
Assert.Equal(s.Value, x.Value);
Assert.NotSame(s, x);
}
Expand All @@ -140,7 +142,7 @@ public async Task AssertL2Operations_Mutable(bool buffers)
Assert.Equal(9, backend.OpCount); // DEL

Log.WriteLine("Fetching new...");
var t = await cache.GetOrCreateAsync(Me(), ct => new ValueTask<Foo>(new Foo { Value = CreateString(true) }));
var t = await cache.GetOrCreateAsync(Me(), ct => new ValueTask<Foo>(new Foo { Value = CreateString(true) }), Expiry);
Assert.NotEqual(s.Value, t.Value);
Assert.Equal(11, backend.OpCount); // GET, SET
}
Expand Down

0 comments on commit 7e6aa49

Please sign in to comment.