Add async caching extension methods

This commit is contained in:
kjac
2023-02-14 11:39:39 +01:00
parent 341db24ec0
commit bdffbb49ae
2 changed files with 52 additions and 0 deletions

View File

@@ -61,4 +61,35 @@ public static class AppCacheExtensions
return result.TryConvertTo<T>().Result;
}
public static async Task<T?> GetCacheItemAsync<T>(
this IAppPolicyCache provider,
string cacheKey,
Func<Task<T?>> getCacheItemAsync,
TimeSpan? timeout,
bool isSliding = false,
string[]? dependentFiles = null)
{
var result = provider.Get(cacheKey);
if (result == null)
{
result = await getCacheItemAsync();
provider.Insert(cacheKey, () => result, timeout, isSliding, dependentFiles);
}
return result == null ? default : result.TryConvertTo<T>().Result;
}
public static async Task InsertCacheItemAsync<T>(
this IAppPolicyCache provider,
string cacheKey,
Func<Task<T>> getCacheItemAsync,
TimeSpan? timeout = null,
bool isSliding = false,
string[]? dependentFiles = null)
{
T value = await getCacheItemAsync();
provider.Insert(cacheKey, () => value, timeout, isSliding, dependentFiles);
}
}