Headless CMS and Content Managment Hub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

61 lines
1.9 KiB

// ==========================================================================
// CachingAppProvider.cs
// PinkParrot Headless CMS
// ==========================================================================
// Copyright (c) PinkParrot Group
// All rights reserved.
// ==========================================================================
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory;
using PinkParrot.Infrastructure;
using PinkParrot.Read.Apps.Repositories;
using PinkParrot.Read.Utils;
// ReSharper disable InvertIf
namespace PinkParrot.Read.Apps.Services.Implementations
{
public class CachingAppProvider : CachingProvider, IAppProvider
{
private static readonly TimeSpan CacheDuration = TimeSpan.FromMinutes(30);
private readonly IAppRepository appRepository;
private sealed class CacheItem
{
public IAppEntity Entity;
}
public CachingAppProvider(IMemoryCache cache, IAppRepository appRepository)
: base(cache)
{
Guard.NotNull(cache, nameof(cache));
this.appRepository = appRepository;
}
public async Task<Guid?> FindAppIdByNameAsync(string name)
{
Guard.NotNullOrEmpty(name, nameof(name));
var cacheKey = BulidCacheKey(name);
var cacheItem = Cache.Get<CacheItem>(cacheKey);
if (cacheItem == null)
{
var app = await appRepository.FindAppByNameAsync(name);
cacheItem = new CacheItem { Entity = app };
Cache.Set(cacheKey, cacheItem, new MemoryCacheEntryOptions { SlidingExpiration = CacheDuration });
}
return cacheItem.Entity?.Id;
}
private static string BulidCacheKey(string name)
{
return $"App_{name}";
}
}
}