From 5ff1390e60bfc3cc5f9f3ff654d522597a2035ab Mon Sep 17 00:00:00 2001 From: lihanbo Date: Thu, 31 Oct 2024 17:48:11 +0800 Subject: [PATCH] =?UTF-8?q?105040=20Add=20=E5=86=85=E5=AD=98=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LFlow.Base/LFlow.Base.csproj | 1 + LFlow.Base/Program.cs | 4 ++ LFlow.Cache/Cacher/SelfSingleCache.cs | 54 ++++++++++++++++++++++++ LFlow.Cache/Interface/ISelfCache.cs | 59 +++++++++++++++++++++++++++ LFlow.Cache/LFlow.Cache.csproj | 17 ++++++++ LFlow.Cache/MemoryCacheExtensions.cs | 16 ++++++++ LFlow.sln | 6 +++ 7 files changed, 157 insertions(+) create mode 100644 LFlow.Cache/Cacher/SelfSingleCache.cs create mode 100644 LFlow.Cache/Interface/ISelfCache.cs create mode 100644 LFlow.Cache/LFlow.Cache.csproj create mode 100644 LFlow.Cache/MemoryCacheExtensions.cs diff --git a/LFlow.Base/LFlow.Base.csproj b/LFlow.Base/LFlow.Base.csproj index 002592e..a868336 100644 --- a/LFlow.Base/LFlow.Base.csproj +++ b/LFlow.Base/LFlow.Base.csproj @@ -28,6 +28,7 @@ + diff --git a/LFlow.Base/Program.cs b/LFlow.Base/Program.cs index 6840cec..c3a8545 100644 --- a/LFlow.Base/Program.cs +++ b/LFlow.Base/Program.cs @@ -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(); diff --git a/LFlow.Cache/Cacher/SelfSingleCache.cs b/LFlow.Cache/Cacher/SelfSingleCache.cs new file mode 100644 index 0000000..b531a8e --- /dev/null +++ b/LFlow.Cache/Cacher/SelfSingleCache.cs @@ -0,0 +1,54 @@ +using LFlow.Cache.Interface; +using Microsoft.Extensions.Caching.Memory; + +namespace LFlow.Cache.Cacher +{ + /// + /// 自身的单实例缓存 + /// + 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 GetAsync(string key) + { + return Task.FromResult(CacheProvider.Get(key)); + } + + public Task GetAsync(string key) + { + return Task.FromResult(CacheProvider.Get(key)); + } + + public Task RemoveAsync(string key) + { + CacheProvider.Remove(key); + return Task.CompletedTask; + } + + public Task SetAsync(string key, T data) + { + CacheProvider.Set(key, data); + return Task.CompletedTask; + } + + public Task SetAsync(string key, T data, TimeSpan expiredTime) + { + CacheProvider.Set(key, data, expiredTime); + return Task.CompletedTask; + + } + } +} diff --git a/LFlow.Cache/Interface/ISelfCache.cs b/LFlow.Cache/Interface/ISelfCache.cs new file mode 100644 index 0000000..9a29856 --- /dev/null +++ b/LFlow.Cache/Interface/ISelfCache.cs @@ -0,0 +1,59 @@ +using Microsoft.Extensions.Caching.Memory; + +namespace LFlow.Cache.Interface +{ + /// + /// 自身缓存接口 + /// 后续扩展,可分为内部缓存和外部缓存 + /// ISelfCache 只作为内部缓存接口 + /// 内外部缓存的区别在于,内部缓存是程序自身的缓存,外部缓存则可共享给其他系统,并暴露在外 + /// + public interface ISelfCache + { + /// + /// 缓存提供者,目前只支持MemoryCache,后续可扩展为使用接口,然后实现不同的缓存提供者 + /// + protected MemoryCache CacheProvider { get; } + /// + /// 获取 + /// + /// 类型 + /// 键 + /// + Task GetAsync(string key); + /// + /// 获取 + /// + /// 键 + /// + Task GetAsync(string key); + /// + /// 存入一个值 + /// + /// 数据类型 + /// 键 + /// + /// + Task SetAsync(string key, T data); + /// + /// 存入一个值 + /// + /// 数据类型 + /// 键 + /// 数据 + /// 过期时间 + /// + Task SetAsync(string key, T data, TimeSpan expiredTime); + /// + /// 移除一个键与他的值 + /// + /// + /// + Task RemoveAsync(string key); + /// + /// 清空缓存 + /// + /// + Task ClearAsync(); + } +} diff --git a/LFlow.Cache/LFlow.Cache.csproj b/LFlow.Cache/LFlow.Cache.csproj new file mode 100644 index 0000000..b326963 --- /dev/null +++ b/LFlow.Cache/LFlow.Cache.csproj @@ -0,0 +1,17 @@ + + + + net8.0 + enable + enable + ../LFlow_Bin/ + false + false + true + + + + + + + diff --git a/LFlow.Cache/MemoryCacheExtensions.cs b/LFlow.Cache/MemoryCacheExtensions.cs new file mode 100644 index 0000000..808b526 --- /dev/null +++ b/LFlow.Cache/MemoryCacheExtensions.cs @@ -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(); + } + + + } +} diff --git a/LFlow.sln b/LFlow.sln index 0d75106..7d7df22 100644 --- a/LFlow.sln +++ b/LFlow.sln @@ -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