using Umbraco.Cms.Core.Cache; namespace Umbraco.Extensions; /// /// Extensions for strongly typed access /// public static class AppCacheExtensions { public static T? GetCacheItem( this IAppPolicyCache provider, string cacheKey, Func getCacheItem, TimeSpan? timeout, bool isSliding = false) { var result = provider.Get(cacheKey, () => getCacheItem(), timeout, isSliding); return result == null ? default : result.TryConvertTo().Result; } public static void InsertCacheItem( this IAppPolicyCache provider, string cacheKey, Func getCacheItem, TimeSpan? timeout = null, bool isSliding = false) => provider.Insert(cacheKey, () => getCacheItem(), timeout, isSliding); public static IEnumerable GetCacheItemsByKeySearch(this IAppCache provider, string keyStartsWith) { IEnumerable result = provider.SearchByKey(keyStartsWith); return result.Select(x => x.TryConvertTo().Result); } public static IEnumerable GetCacheItemsByKeyExpression(this IAppCache provider, string regexString) { IEnumerable result = provider.SearchByRegex(regexString); return result.Select(x => x.TryConvertTo().Result); } public static T? GetCacheItem(this IAppCache provider, string cacheKey) { var result = provider.Get(cacheKey); if (result == null) { return default; } return result.TryConvertTo().Result; } public static T? GetCacheItem(this IAppCache provider, string cacheKey, Func getCacheItem) { var result = provider.Get(cacheKey, () => getCacheItem()); if (result == null) { return default; } return result.TryConvertTo().Result; } public static async Task GetCacheItemAsync( this IAppPolicyCache provider, string cacheKey, Func> getCacheItemAsync, TimeSpan? timeout, bool isSliding = false) { var result = provider.Get(cacheKey); if (result == null) { result = await getCacheItemAsync(); provider.Insert(cacheKey, () => result, timeout, isSliding); } return result == null ? default : result.TryConvertTo().Result; } public static async Task InsertCacheItemAsync( this IAppPolicyCache provider, string cacheKey, Func> getCacheItemAsync, TimeSpan? timeout = null, bool isSliding = false) { T value = await getCacheItemAsync(); provider.Insert(cacheKey, () => value, timeout, isSliding); } }