RhSolutions-AddIn/Source/AddIn/MemoryCacheUtil.cs

37 lines
960 B
C#
Raw Normal View History

using System;
using System.Runtime.Caching;
using System.Threading.Tasks;
2021-12-24 16:22:03 +03:00
using RehauSku.Assistant;
2021-12-24 16:22:03 +03:00
namespace RehauSku
{
2021-12-24 16:22:03 +03:00
static class MemoryCacheUtil
{
public static bool IsCached(this string request)
{
2021-12-24 16:22:03 +03:00
return AddIn.memoryCache.Contains(request);
}
public static IProduct GetFromCache(this string request)
{
2021-12-24 16:22:03 +03:00
return AddIn.memoryCache[request] as IProduct;
}
public static async Task<IProduct> RequestAndCache(this string request)
{
IProduct product = await SkuAssist.GetProductAsync(request);
if (product == null)
return null;
2021-12-24 16:22:03 +03:00
AddIn.memoryCache.Add(request, product, DateTime.Now.AddMinutes(10));
return product;
}
2021-12-24 16:22:03 +03:00
public static void ClearCache()
{
AddIn.memoryCache.Dispose();
AddIn.memoryCache = new MemoryCache("RehauSku");
}
}
}