105040 Add 内存缓存支持

This commit is contained in:
lihanbo 2024-10-31 17:48:11 +08:00
parent dafd320b05
commit 5ff1390e60
7 changed files with 157 additions and 0 deletions

View File

@ -28,6 +28,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LFlow.Cache\LFlow.Cache.csproj" />
<ProjectReference Include="..\LFlow.InternalEventBus\LFlow.InternalEventBus.csproj" />
<ProjectReference Include="..\LFlow.Middleware\LFlow.Middleware.csproj" />
</ItemGroup>

View File

@ -1,5 +1,6 @@
using LFlow.Base.Interfaces;
using LFlow.Base.Utils;
using LFlow.Cache;
using LFlow.InternalEventBus;
using LFlow.Middleware;
@ -40,7 +41,10 @@ public static class Program
// 注册中间件
builder.Services.RegisterMiddlewares(App.SubServiceAssembly);
// 注册内部事件总线
builder.Services.AddInternalEventBus(App.SubServiceAssembly);
// 注册自身缓存器
builder.Services.AddSelfCache();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

View File

@ -0,0 +1,54 @@
using LFlow.Cache.Interface;
using Microsoft.Extensions.Caching.Memory;
namespace LFlow.Cache.Cacher
{
/// <summary>
/// 自身的单实例缓存
/// </summary>
public class SelfSingleCache : ISelfCache
{
protected MemoryCache _cache = new(new MemoryCacheOptions()
{
ExpirationScanFrequency = new TimeSpan(0, 0, 10)
});
public MemoryCache CacheProvider => _cache;
public Task ClearAsync()
{
CacheProvider.Clear();
return Task.CompletedTask;
}
public Task<T?> GetAsync<T>(string key)
{
return Task.FromResult(CacheProvider.Get<T>(key));
}
public Task<object?> GetAsync(string key)
{
return Task.FromResult(CacheProvider.Get(key));
}
public Task RemoveAsync(string key)
{
CacheProvider.Remove(key);
return Task.CompletedTask;
}
public Task SetAsync<T>(string key, T data)
{
CacheProvider.Set(key, data);
return Task.CompletedTask;
}
public Task SetAsync<T>(string key, T data, TimeSpan expiredTime)
{
CacheProvider.Set(key, data, expiredTime);
return Task.CompletedTask;
}
}
}

View File

@ -0,0 +1,59 @@
using Microsoft.Extensions.Caching.Memory;
namespace LFlow.Cache.Interface
{
/// <summary>
/// 自身缓存接口
/// 后续扩展,可分为内部缓存和外部缓存
/// ISelfCache 只作为内部缓存接口
/// 内外部缓存的区别在于,内部缓存是程序自身的缓存,外部缓存则可共享给其他系统,并暴露在外
/// </summary>
public interface ISelfCache
{
/// <summary>
/// 缓存提供者目前只支持MemoryCache后续可扩展为使用接口然后实现不同的缓存提供者
/// </summary>
protected MemoryCache CacheProvider { get; }
/// <summary>
/// 获取
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="key">键</param>
/// <returns></returns>
Task<T?> GetAsync<T>(string key);
/// <summary>
/// 获取
/// </summary>
/// <param name="key">键</param>
/// <returns></returns>
Task<object?> GetAsync(string key);
/// <summary>
/// 存入一个值
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <param name="key">键</param>
/// <param name="data"></param>
/// <returns></returns>
Task SetAsync<T>(string key, T data);
/// <summary>
/// 存入一个值
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <param name="key">键</param>
/// <param name="data">数据</param>
/// <param name="expiredTime">过期时间</param>
/// <returns></returns>
Task SetAsync<T>(string key, T data, TimeSpan expiredTime);
/// <summary>
/// 移除一个键与他的值
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
Task RemoveAsync(string key);
/// <summary>
/// 清空缓存
/// </summary>
/// <returns></returns>
Task ClearAsync();
}
}

View File

@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputPath>../LFlow_Bin/</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<UseCommonOutputDirectory>true</UseCommonOutputDirectory>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.0-rc.2.24473.5" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,16 @@
using LFlow.Cache.Cacher;
using LFlow.Cache.Interface;
using Microsoft.Extensions.DependencyInjection;
namespace LFlow.Cache
{
public static class MemoryCacheExtensions
{
public static void AddSelfCache(this IServiceCollection services)
{
services.AddSingleton<ISelfCache, SelfSingleCache>();
}
}
}

View File

@ -15,6 +15,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LFlow.Middleware", "LFlow.M
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LFlow.InternalEventBus", "LFlow.InternalEventBus\LFlow.InternalEventBus.csproj", "{72CB0C72-9725-43A5-882D-3E93FD1F8706}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LFlow.Cache", "LFlow.Cache\LFlow.Cache.csproj", "{97D56496-BE2A-4431-A882-7DEA20B72EB0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -45,6 +47,10 @@ Global
{72CB0C72-9725-43A5-882D-3E93FD1F8706}.Debug|Any CPU.Build.0 = Debug|Any CPU
{72CB0C72-9725-43A5-882D-3E93FD1F8706}.Release|Any CPU.ActiveCfg = Release|Any CPU
{72CB0C72-9725-43A5-882D-3E93FD1F8706}.Release|Any CPU.Build.0 = Release|Any CPU
{97D56496-BE2A-4431-A882-7DEA20B72EB0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{97D56496-BE2A-4431-A882-7DEA20B72EB0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{97D56496-BE2A-4431-A882-7DEA20B72EB0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{97D56496-BE2A-4431-A882-7DEA20B72EB0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE