feat: 增加异步缓存方法

This commit is contained in:
Argo-Tianyi 2022-01-10 15:28:40 +08:00
parent 70e42a8268
commit bbf51ea3f4
5 changed files with 72 additions and 13 deletions

View File

@ -7,6 +7,8 @@ namespace BootStarpAdmin.DataAccess.FreeSql.Service;
class DictService : IDict
{
private const string DictServiceCacheKey = "DictService-GetAll";
private IFreeSql FreeSql { get; }
private string? AppId { get; set; }
@ -33,10 +35,11 @@ class DictService : IDict
return ret;
}
public List<Dict> GetAll()
{
return FreeSql.Select<Dict>().ToList();
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public List<Dict> GetAll() => CacheManager.GetOrCreate<List<Dict>>(DictServiceCacheKey, entry => FreeSql.Select<Dict>().ToList());
public Dictionary<string, string> GetApps()
{
@ -122,7 +125,7 @@ class DictService : IDict
if (ret)
{
// 更新缓存
CacheManager.Remove(DictServiceCacheKey);
CacheManager.Clear(DictServiceCacheKey);
}
return ret;
}

View File

@ -16,8 +16,7 @@ public static class ServiceCollectionExtensions
/// <returns></returns>
public static IServiceCollection AddCacheManager(this IServiceCollection services)
{
services.AddMemoryCache();
services.TryAddSingleton<ICacheManager>(DefaultCacheManager.Instance);
services.TryAddSingleton<ICacheManager>(provider => DefaultCacheManager.Instance);
return services;
}
}

View File

@ -20,6 +20,15 @@ public interface ICacheManager
/// <returns></returns>
T GetOrCreate<T>(string key, Func<ICacheEntry, T> factory);
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="factory"></param>
/// <returns></returns>
Task<T> GetOrCreateAsync<T>(string key, Func<ICacheEntry, Task<T>> factory);
/// <summary>
///
/// </summary>

View File

@ -5,17 +5,58 @@ namespace BootstrapAdmin.Web.Core.Services;
class DefaultCacheManager : ICacheManager
{
private static readonly Lazy<ICacheManager> cache = new(() => new DefaultCacheManager());
private IMemoryCache Cache { get; }
public static ICacheManager Instance { get; } = cache.Value;
private List<string> Keys { get; } = new List<string>(256);
public T GetOrCreate<T>(string key, Func<ICacheEntry, T> factory)
/// <summary>
///
/// </summary>
public DefaultCacheManager()
{
throw new NotImplementedException();
Cache = new MemoryCache(new MemoryCacheOptions());
}
public T GetOrCreate<T>(string key, Func<ICacheEntry, T> factory) => Cache.GetOrCreate(key, entry =>
{
HandlerEntry(key, entry);
return factory(entry);
});
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="factory"></param>
/// <returns></returns>
public Task<T> GetOrCreateAsync<T>(string key, Func<ICacheEntry, Task<T>> factory) => Cache.GetOrCreate(key, entry =>
{
HandlerEntry(key, entry);
return factory(entry);
});
private void HandlerEntry(string key, ICacheEntry entry)
{
Keys.Add(key);
entry.SetAbsoluteExpiration(TimeSpan.FromSeconds(10));
entry.RegisterPostEvictionCallback((key, value, reason, state) =>
{
var k = key.ToString();
if (!string.IsNullOrEmpty(k))
{
Keys.Remove(k);
}
});
}
public void Clear(string? key)
{
throw new NotImplementedException();
}
#region
[NotNull]
internal static ICacheManager? Instance { get; } = new DefaultCacheManager();
#endregion
}

View File

@ -33,6 +33,10 @@ public partial class Healths
[NotNull]
private DialogService? DialogService { get; set; }
[Inject]
[NotNull]
private BootstrapBlazor.Web.Core.ICacheManager? CacheManager { get; set; }
[NotNull]
private HttpClient? Client { get; set; }
@ -49,8 +53,11 @@ public partial class Healths
private async Task<QueryData<HealthCheckReportItem>> OnQueryAsync(QueryPageOptions options)
{
var payload = await Client.GetStringAsync("/Healths");
var report = HealthCheckHelper.Parse(payload);
var report = await CacheManager.GetOrCreateAsync("Health", async entry =>
{
var payload = await Client.GetStringAsync("/Healths");
return HealthCheckHelper.Parse(payload);
});
var ret = new QueryData<HealthCheckReportItem>()
{