SharpRSS/SharpRss/Core/Cache/TempCache.cs
2023-06-17 14:04:26 +02:00

37 lines
1.4 KiB
C#

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SharpRss.Models;
namespace SharpRss.Core.Cache
{
internal enum CacheFetch
{
Category,
Syndication
}
internal static class TempCache
{
private static Dictionary<string, CategoryModel> _categoryCache = new Dictionary<string, CategoryModel>();
private static Dictionary<string, SyndicationModel> _syndicationCache = new Dictionary<string, SyndicationModel>();
public static async Task UpdateCache(CacheFetch cf)
{
switch (cf)
{
case CacheFetch.Category:
var cats = await DbAccess.GetCategoriesAsync();
_categoryCache = cats.ToDictionary(x => x.Id);
break;
case CacheFetch.Syndication:
var syndications = await DbAccess.GetSyndicationsAsync();
_syndicationCache = syndications.ToDictionary(x => x.EncodedUrl);
break;
}
}
public static CategoryModel? GetCategory(string catId) => _categoryCache.TryGetValue(catId, out CategoryModel catModel) ? catModel : null;
public static SyndicationModel? GetSyndication(string encodedUrl) => _syndicationCache.TryGetValue(encodedUrl, out SyndicationModel synModel) ? synModel : null;
}
}