replace Task.Run() logic in SiteService with async SiteRepository methods

This commit is contained in:
sbwalker
2024-02-27 15:12:48 -05:00
parent e2182344a2
commit e7157a8528
4 changed files with 99 additions and 31 deletions

View File

@ -2,6 +2,8 @@ using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
namespace Oqtane.Extensions
{
@ -19,23 +21,45 @@ namespace Oqtane.Extensions
if (track)
{
// track the cache key
List<string> cachekeys;
if (!cache.TryGetValue(_cachekeys, out cachekeys))
{
cachekeys = new List<string>();
}
if (!cachekeys.Contains(key))
{
cachekeys.Add(key);
cache.Set(_cachekeys, cachekeys, new MemoryCacheEntryOptions { Priority = CacheItemPriority.NeverRemove });
}
TrackCacheKey(cache, key);
}
}
return (TItem)result;
}
public static async Task<TItem> GetOrCreateAsync<TItem>(this IMemoryCache cache, string key, Func<ICacheEntry, Task<TItem>> factory, bool track)
{
if (!cache.TryGetValue(key, out object result))
{
using ICacheEntry entry = cache.CreateEntry(key);
result = await factory(entry).ConfigureAwait(false);
entry.Value = result;
if (track)
{
TrackCacheKey(cache, key);
}
}
return (TItem)result;
}
private static void TrackCacheKey(IMemoryCache cache, string key)
{
// track the cache key
List<string> cachekeys;
if (!cache.TryGetValue(_cachekeys, out cachekeys))
{
cachekeys = new List<string>();
}
if (!cachekeys.Contains(key))
{
cachekeys.Add(key);
cache.Set(_cachekeys, cachekeys, new MemoryCacheEntryOptions { Priority = CacheItemPriority.NeverRemove });
}
}
public static void Remove(this IMemoryCache cache, string key, bool track)
{
List<string> cachekeys;