// ========================================================================== // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex UG (haftungsbeschraenkt) // All rights reserved. Licensed under the MIT license. // ========================================================================== using System; using System.Threading.Tasks; namespace Squidex.Infrastructure.Caching { public static class RequestCacheExtensions { public static async Task GetOrCreateAsync(this ILocalCache cache, object key, Func> task) { if (cache.TryGetValue(key, out var value) && value is T typedValue) { return typedValue; } typedValue = await task(); cache.Add(key, typedValue); return typedValue; } public static T GetOrCreate(this ILocalCache cache, object key, Func task) { if (cache.TryGetValue(key, out var value) && value is T typedValue) { return typedValue; } typedValue = task(); cache.Add(key, typedValue); return typedValue; } } }