Skip to content

Commit a9320bc

Browse files
committed
Enable 'await' on CacheEntity/CacheEntityReference to make the usage less verbose/tedious +semver: minor
1 parent 9c98129 commit a9320bc

File tree

6 files changed

+51
-5
lines changed

6 files changed

+51
-5
lines changed

AsyncMemoryCache.Tests/AsyncMemoryCacheTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public async Task GetOrCreateObjectIsReturnedInCacheEntity()
4646

4747
var cacheEntityReference = target.GetOrCreate("test", factory);
4848

49-
var cachedObject = await cacheEntityReference.CacheEntity.ObjectFactory;
49+
var cachedObject = await cacheEntityReference.CacheEntity;
5050
Assert.Same(objectToCache, cachedObject);
5151
}
5252

@@ -62,7 +62,7 @@ public async Task GetOrCreateObjectIsReturnedFromIndexer()
6262
const string cacheKey = "test";
6363
_ = target.GetOrCreate(cacheKey, factory);
6464

65-
var cachedObject = await target[cacheKey].CacheEntity.ObjectFactory;
65+
var cachedObject = await target[cacheKey];
6666
Assert.Same(objectToCache, cachedObject);
6767
}
6868

AsyncMemoryCache/AsyncMemoryCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public async ValueTask DisposeAsync()
159159
var diposeTasks = _configuration.CacheBackingStore
160160
.Select(async x =>
161161
{
162-
var cachedObject = await x.Value.ObjectFactory;
162+
var cachedObject = await x.Value;
163163
await cachedObject.DisposeAsync().ConfigureAwait(false);
164164
});
165165

AsyncMemoryCache/CacheEntity.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using AsyncMemoryCache.ExpirationStrategy;
22
using Nito.AsyncEx;
33
using System;
4+
using System.ComponentModel;
5+
using System.Diagnostics.CodeAnalysis;
6+
using System.Runtime.CompilerServices;
47
using System.Threading.Tasks;
58

69
namespace AsyncMemoryCache;
@@ -41,6 +44,26 @@ public CacheEntity(TKey key, Func<Task<TValue>> objectFactory, AsyncLazyFlags la
4144
internal IExpirationStrategy? ExpirationStrategy { get; private set; }
4245
internal Action<TKey, TValue>? ExpirationCallback { get; private set; }
4346

47+
/// <summary>
48+
/// Asynchronous infrastructure support. This method permits instances of <see cref="CacheEntity{TKey, TValue}"/> to be await'ed.
49+
/// </summary>
50+
[EditorBrowsable(EditorBrowsableState.Never)]
51+
public TaskAwaiter<TValue> GetAwaiter()
52+
{
53+
return ObjectFactory.Task.GetAwaiter();
54+
}
55+
56+
/// <summary>
57+
/// Asynchronous infrastructure support. This method permits instances of <see cref="CacheEntity{TKey, TValue}"/> to be await'ed.
58+
/// </summary>
59+
#if NET8_0_OR_GREATER
60+
[ExcludeFromCodeCoverage(Justification = "There's no real functionality to be tested here, it's just enabling await")]
61+
#endif
62+
public ConfiguredTaskAwaitable<TValue> ConfigureAwait(bool continueOnCapturedContext)
63+
{
64+
return ObjectFactory.Task.ConfigureAwait(continueOnCapturedContext);
65+
}
66+
4467
/// <summary>
4568
/// Sets the <see cref="ExpirationStrategy"/> to a new instance of <see cref="AbsoluteExpirationStrategy"/>.<br/>
4669
/// This strategy takes an absolute date into account when evaluating whether or not it is expired.<br/>

AsyncMemoryCache/CacheEntityReference.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using AsyncMemoryCache.EvictionBehaviors;
2+
using Microsoft.Extensions.DependencyInjection;
23
using System;
4+
using System.ComponentModel;
35
using System.Diagnostics.CodeAnalysis;
6+
using System.Runtime.CompilerServices;
47
using System.Threading;
58

69
namespace AsyncMemoryCache;
@@ -39,6 +42,26 @@ public CacheEntityReference(CacheEntity<TKey, TValue> cacheEntity)
3942
_ = Interlocked.Increment(ref cacheEntity.References);
4043
}
4144

45+
/// <summary>
46+
/// Asynchronous infrastructure support. This method permits instances of <see cref="CacheEntityReference{TKey, TValue}"/> to be await'ed.
47+
/// </summary>
48+
[EditorBrowsable(EditorBrowsableState.Never)]
49+
public TaskAwaiter<TValue> GetAwaiter()
50+
{
51+
return CacheEntity.GetAwaiter();
52+
}
53+
54+
/// <summary>
55+
/// Asynchronous infrastructure support. This method permits instances of <see cref="CacheEntityReference{TKey, TValue}"/> to be await'ed.
56+
/// </summary>
57+
#if NET8_0_OR_GREATER
58+
[ExcludeFromCodeCoverage(Justification = "There's no real functionality to be tested here, it's just enabling await")]
59+
#endif
60+
public ConfiguredTaskAwaitable<TValue> ConfigureAwait(bool continueOnCapturedContext)
61+
{
62+
return CacheEntity.ConfigureAwait(continueOnCapturedContext);
63+
}
64+
4265
/// <summary>
4366
/// The finalizer for <see cref="CacheEntityReference{TKey, TValue}"/>.
4467
/// This serves as a fail-safe if <see cref="Dispose"/> is never called.

AsyncMemoryCache/EvictionBehaviors/DefaultEvictionBehavior.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ private static async Task CheckExpiredItems<TKey, TValue>(IAsyncMemoryCacheConfi
103103
foreach (var expiredItem in expiredItems)
104104
{
105105
logger.LogTrace("Expiring item with key {Key}", expiredItem.Key);
106-
var item = await expiredItem.ObjectFactory;
106+
var item = await expiredItem;
107107
if (cache.Remove(expiredItem.Key))
108108
{
109109
configuration.CacheItemExpired?.Invoke(expiredItem.Key, item);

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ var cacheEntityReference = cache.GetOrCreate("theKey", async () =>
4545
cacheEntityReference.CacheEntity.WithSlidingExpiration(TimeSpan.FromHours(12));
4646

4747
// Will block here until the object is created
48-
var theCachedObject = await cache["theKey"].CacheEntity.ObjectFactory;
48+
var theCachedObject = await cache["theKey"]; // Short-hand for await cache["theKey"].CacheEntity.ObjectFactory;
4949
```
5050

5151
### Extending the lifetime of a cached object

0 commit comments

Comments
 (0)