feat: 增加缓存层

This commit is contained in:
Argo-Tianyi 2022-01-12 23:25:08 +08:00
parent 1317b2828f
commit 0df8267cb2
8 changed files with 47 additions and 18 deletions

View File

@ -163,6 +163,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BootstrapClient.Web.Core",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BootstrapClient.DataAccess.Models", "src\blazor\client\BootstrapClient.Web.Models\BootstrapClient.DataAccess.Models.csproj", "{CC3DF23A-2880-438F-BDEB-DB093E919ABA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BootstrapAdmin.Caching", "src\blazor\admin\BootstrapAdmin.Caching\BootstrapAdmin.Caching.csproj", "{ADD20515-1C1C-418B-84F6-8B05A7AA315B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -245,6 +247,10 @@ Global
{CC3DF23A-2880-438F-BDEB-DB093E919ABA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CC3DF23A-2880-438F-BDEB-DB093E919ABA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CC3DF23A-2880-438F-BDEB-DB093E919ABA}.Release|Any CPU.Build.0 = Release|Any CPU
{ADD20515-1C1C-418B-84F6-8B05A7AA315B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ADD20515-1C1C-418B-84F6-8B05A7AA315B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ADD20515-1C1C-418B-84F6-8B05A7AA315B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ADD20515-1C1C-418B-84F6-8B05A7AA315B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -286,6 +292,7 @@ Global
{1D20E6CF-9825-4CDE-B732-AE586BD1AABA} = {45ADEF9B-C8BD-4224-9E12-F6716E85A22C}
{FFDF9FF9-0B29-47D3-AD42-53A476B570EC} = {55A2459A-6BDE-4493-B2C0-5BE1673E99EE}
{CC3DF23A-2880-438F-BDEB-DB093E919ABA} = {55A2459A-6BDE-4493-B2C0-5BE1673E99EE}
{ADD20515-1C1C-418B-84F6-8B05A7AA315B} = {45ADEF9B-C8BD-4224-9E12-F6716E85A22C}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {221EAE38-5F75-4391-9A48-E462A9F3B8FC}

View File

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="6.0.0" />
</ItemGroup>
</Project>

View File

@ -1,8 +1,7 @@
using BootstrapAdmin.Web.Core.Services;
using BootstrapBlazor.Web.Core;
using BootstrapAdmin.Caching.Services;
using Microsoft.Extensions.Caching.Memory;
namespace BootstrapAdmin.Web.Core;
namespace BootstrapAdmin.Caching;
/// <summary>
/// 缓存管理类
@ -18,11 +17,17 @@ public static class CacheManager
/// <param name="key"></param>
/// <param name="valueFactory"></param>
/// <returns></returns>
public static TItem GetOrCreate<TItem>(string key, Func<ICacheEntry, TItem> valueFactory) => Cache.GetOrCreate(key, valueFactory);
public static TItem GetOrAdd<TItem>(string key, Func<ICacheEntry, TItem> valueFactory)
{
return Cache.GetOrAdd(key, valueFactory);
}
/// <summary>
/// 清除指定键值缓存项
/// </summary>
/// <param name="key"></param>
public static void Clear(string? key) => Cache.Clear(key);
public static void Clear(string? key)
{
Cache.Clear(key);
}
}

View File

@ -1,5 +1,6 @@
using BootstrapAdmin.Web.Core.Services;
using BootstrapBlazor.Web.Core;
using BootstrapAdmin.Caching;
using BootstrapAdmin.Caching.Services;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection.Extensions;
namespace Microsoft.Extensions.DependencyInjection;

View File

@ -4,7 +4,7 @@
using Microsoft.Extensions.Caching.Memory;
namespace BootstrapBlazor.Web.Core;
namespace BootstrapAdmin.Caching;
/// <summary>
/// CacheManager 接口类
@ -18,7 +18,7 @@ public interface ICacheManager
/// <param name="key"></param>
/// <param name="factory"></param>
/// <returns></returns>
T GetOrCreate<T>(string key, Func<ICacheEntry, T> factory);
T GetOrAdd<T>(string key, Func<ICacheEntry, T> factory);
/// <summary>
///
@ -27,7 +27,7 @@ public interface ICacheManager
/// <param name="key"></param>
/// <param name="factory"></param>
/// <returns></returns>
Task<T> GetOrCreateAsync<T>(string key, Func<ICacheEntry, Task<T>> factory);
Task<T> GetOrAddAsync<T>(string key, Func<ICacheEntry, Task<T>> factory);
/// <summary>
///

View File

@ -1,13 +1,12 @@
using BootstrapBlazor.Web.Core;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Primitives;
namespace BootstrapAdmin.Web.Core.Services;
namespace BootstrapAdmin.Caching.Services;
class DefaultCacheManager : ICacheManager
{
[NotNull]
private IMemoryCache? Cache { get; set; }
private MemoryCache? Cache { get; set; }
private List<(string Key, CancellationTokenSource Token)> Keys { get; } = new(256);
@ -25,7 +24,7 @@ class DefaultCacheManager : ICacheManager
Cache = new MemoryCache(new MemoryCacheOptions());
}
public T GetOrCreate<T>(string key, Func<ICacheEntry, T> factory) => Cache.GetOrCreate(key, entry =>
public T GetOrAdd<T>(string key, Func<ICacheEntry, T> factory) => Cache.GetOrCreate(key, entry =>
{
HandlerEntry(key, entry);
return factory(entry);
@ -38,7 +37,7 @@ class DefaultCacheManager : ICacheManager
/// <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 =>
public Task<T> GetOrAddAsync<T>(string key, Func<ICacheEntry, Task<T>> factory) => Cache.GetOrCreate(key, entry =>
{
HandlerEntry(key, entry);
return factory(entry);
@ -91,7 +90,7 @@ class DefaultCacheManager : ICacheManager
}
else
{
Init();
Cache.Compact(100);
}
}

View File

@ -66,6 +66,9 @@ public static class ServiceCollectionExtensions
// 增加数据服务
services.AddSingleton(typeof(IDataService<>), typeof(DefaultDataService<>));
// 增加缓存服务
services.AddCacheManager();
// 增加业务服务
services.AddSingleton<IApp, AppService>();
services.AddSingleton<IDict, DictService>();

View File

@ -53,7 +53,7 @@ public partial class Healths
private async Task<QueryData<HealthCheckReportItem>> OnQueryAsync(QueryPageOptions options)
{
var report = await CacheManager.GetOrCreateAsync("Health", async entry =>
var report = await CacheManager.GetOrAddAsync("Health", async entry =>
{
var payload = await Client.GetStringAsync("/Healths");
return HealthCheckHelper.Parse(payload);