From 443f3792f9c9a4a3ec0a64bfa2507be44d9edde7 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Thu, 6 Feb 2025 00:11:28 +0800 Subject: [PATCH 1/6] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=20CacheManagerOp?= =?UTF-8?q?tions=20=E9=85=8D=E7=BD=AE=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Options/CacheManagerOptions.cs | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/BootstrapBlazor/Options/CacheManagerOptions.cs diff --git a/src/BootstrapBlazor/Options/CacheManagerOptions.cs b/src/BootstrapBlazor/Options/CacheManagerOptions.cs new file mode 100644 index 00000000000..af2a81ff2bd --- /dev/null +++ b/src/BootstrapBlazor/Options/CacheManagerOptions.cs @@ -0,0 +1,27 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the Apache 2.0 License +// See the LICENSE file in the project root for more information. +// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone + +namespace BootstrapBlazor.Components; + +/// +/// CacheManagerOptions 配置类 +/// +public class CacheManagerOptions +{ + /// + /// 获得/设置 是否开启 CacheManager 功能 默认 true 开启 + /// + public bool Enable { get; set; } = true; + + /// + /// 获得/设置 滑动缓存过期时间 默认 5 分钟 + /// + public TimeSpan SlidingExpiration { get; set; } = TimeSpan.FromMinutes(5); + + /// + /// 获得/设置 绝对缓存过期时间 默认 10 秒钟 + /// + public TimeSpan AbsoluteExpiration { get; set; } = TimeSpan.FromSeconds(10); +} From 3173fbfe82f095fbbabc5a889eb0a7f2aeda9099 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Thu, 6 Feb 2025 00:11:55 +0800 Subject: [PATCH 2/6] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=20CacheManagerOp?= =?UTF-8?q?tions=20=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Options/BootstrapBlazorOptions.cs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/BootstrapBlazor/Options/BootstrapBlazorOptions.cs b/src/BootstrapBlazor/Options/BootstrapBlazorOptions.cs index e0a1dabdf06..139868f135e 100644 --- a/src/BootstrapBlazor/Options/BootstrapBlazorOptions.cs +++ b/src/BootstrapBlazor/Options/BootstrapBlazorOptions.cs @@ -91,35 +91,40 @@ public class BootstrapBlazorOptions : IOptions public TableSettings TableSettings { get; set; } = new(); /// - /// 获得/设置 Step 配置实例 + /// 获得/设置 配置实例 /// public StepSettings StepSettings { get; set; } = new(); /// - /// 获得/设置 ConnectionHubOptions 配置 默认不为空 + /// 获得/设置 配置 默认不为空 /// public ConnectionHubOptions ConnectionHubOptions { get; set; } = new(); /// - /// 获得/设置 WebClientOptions 配置 默认不为空 + /// 获得/设置 配置 默认不为空 /// public WebClientOptions WebClientOptions { get; set; } = new(); /// - /// 获得/设置 IpLocatorOptions 配置 默认不为空 + /// 获得/设置 配置 默认不为空 /// public IpLocatorOptions IpLocatorOptions { get; set; } = new(); /// - /// 获得/设置 ScrollOptions 配置 默认不为空 + /// 获得/设置 配置 默认不为空 /// public ScrollOptions ScrollOptions { get; set; } = new(); /// - /// 获得/设置 ContextMenuOptions 配置 默认不为空 + /// 获得/设置 配置 默认不为空 /// public ContextMenuOptions ContextMenuOptions { get; set; } = new(); + /// + /// 获得/设置 CacheManagerOptions 配置 默认不为空 + /// + public CacheManagerOptions CacheManagerOptions { get; set; } = new(); + BootstrapBlazorOptions IOptions.Value => this; /// From cb7d53e136e2415ddf15b39ee652ceed4358ce38 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Thu, 6 Feb 2025 00:12:25 +0800 Subject: [PATCH 3/6] =?UTF-8?q?feat:=20=E4=BD=BF=E7=94=A8=20CacheManagerOp?= =?UTF-8?q?tions=20=E5=80=BC=E4=BB=A3=E6=9B=BF=E5=B8=B8=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/Services/CacheManager.cs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/BootstrapBlazor/Services/CacheManager.cs b/src/BootstrapBlazor/Services/CacheManager.cs index 74a03cf0c2b..007b154346e 100644 --- a/src/BootstrapBlazor/Services/CacheManager.cs +++ b/src/BootstrapBlazor/Services/CacheManager.cs @@ -30,6 +30,9 @@ internal class CacheManager : ICacheManager [NotNull] private static CacheManager? Instance { get; set; } + [NotNull] + private static BootstrapBlazorOptions? Options { get; set; } + private const string CacheKeyPrefix = "BootstrapBlazor"; /// @@ -42,6 +45,7 @@ public CacheManager(IServiceProvider provider, IMemoryCache memoryCache) Provider = provider; Cache = memoryCache; Instance = this; + Options = Provider.GetRequiredService>().Value; } /// @@ -51,9 +55,11 @@ public TItem GetOrCreate(object key, Func factory) => { var item = factory(entry); - if (entry.SlidingExpiration == null && entry.AbsoluteExpiration == null && entry.Priority != CacheItemPriority.NeverRemove) + if (entry.SlidingExpiration == null && entry.AbsoluteExpiration == null + && entry.AbsoluteExpirationRelativeToNow == null + && entry.Priority != CacheItemPriority.NeverRemove) { - entry.SetSlidingExpiration(TimeSpan.FromMinutes(5)); + entry.SetSlidingExpiration(Options.CacheManagerOptions.SlidingExpiration); } return item; })!; @@ -65,9 +71,11 @@ public Task GetOrCreateAsync(object key, Func(TModel model, string fieldName) { if (type.Assembly.IsDynamic) { - entry.SetAbsoluteExpiration(TimeSpan.FromSeconds(10)); + entry.SetAbsoluteExpiration(Options.CacheManagerOptions.AbsoluteExpiration); } return LambdaExtensions.GetPropertyValueLambda(model, fieldName).Compile(); @@ -557,7 +565,7 @@ public static void SetPropertyValue(TModel model, string fieldNa { if (type.Assembly.IsDynamic) { - entry.SetAbsoluteExpiration(TimeSpan.FromSeconds(10)); + entry.SetAbsoluteExpiration(Options.CacheManagerOptions.AbsoluteExpiration); } return LambdaExtensions.SetPropertyValueLambda(model, fieldName).Compile(); }); From fb2c14d5274a23e940f5a465c50f03d35ef3cf1c Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Thu, 6 Feb 2025 00:12:40 +0800 Subject: [PATCH 4/6] =?UTF-8?q?doc:=20=E6=9B=B4=E6=96=B0=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E6=97=B6=E9=95=BF=E8=AE=A1=E7=AE=97=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Extensions/ICacheEntryExtensions.cs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/BootstrapBlazor.Server/Extensions/ICacheEntryExtensions.cs b/src/BootstrapBlazor.Server/Extensions/ICacheEntryExtensions.cs index c5fca810dd5..736a41d87d2 100644 --- a/src/BootstrapBlazor.Server/Extensions/ICacheEntryExtensions.cs +++ b/src/BootstrapBlazor.Server/Extensions/ICacheEntryExtensions.cs @@ -27,11 +27,13 @@ public static string GetExpiration(this ICacheEntry entry) } else if (entry.SlidingExpiration.HasValue) { - ret = $"Sliding: {entry.GetSlidingLeftTime().TotalSeconds:###}/{entry.SlidingExpiration.Value.TotalSeconds}"; + var ts = entry.GetSlidingLeftTime(); + ret = ts == TimeSpan.Zero ? "Expirated" : $"Sliding: {ts.TotalSeconds:###}/{entry.SlidingExpiration.Value.TotalSeconds}"; } else if (entry.AbsoluteExpiration.HasValue) { - ret = $"Absolute: {entry.AbsoluteExpiration.Value}"; + var ts = entry.GetAbsoluteLeftTime(); + ret = ts == TimeSpan.Zero ? "Expirated" : $"Absolute: {ts.TotalSeconds:###}"; } else if (entry.ExpirationTokens.Count != 0) { @@ -59,4 +61,14 @@ private static TimeSpan GetSlidingLeftTime(this ICacheEntry entry) } return ts; } + + private static TimeSpan GetAbsoluteLeftTime(this ICacheEntry entry) + { + var ts = entry.AbsoluteExpiration!.Value - DateTimeOffset.UtcNow; + if (ts < TimeSpan.Zero) + { + ts = TimeSpan.Zero; + } + return ts; + } } From d3980ae1adfec1d4e16b3daad31ab3913e3ff05a Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Thu, 6 Feb 2025 00:21:51 +0800 Subject: [PATCH 5/6] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Options/BootstrapBlazorOptionsTest.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/UnitTest/Options/BootstrapBlazorOptionsTest.cs b/test/UnitTest/Options/BootstrapBlazorOptionsTest.cs index 04a3e5d41b0..81420ae7474 100644 --- a/test/UnitTest/Options/BootstrapBlazorOptionsTest.cs +++ b/test/UnitTest/Options/BootstrapBlazorOptionsTest.cs @@ -103,4 +103,19 @@ public void Options_TableExportOptions() Assert.Equal(",", exportOptions.ArrayDelimiter); } + + [Fact] + public void CacheManagerOptions_Ok() + { + var options = new BootstrapBlazorOptions(); + Assert.NotNull(options.CacheManagerOptions); + + options.CacheManagerOptions.Enable = true; + options.CacheManagerOptions.SlidingExpiration = TimeSpan.FromSeconds(1); + options.CacheManagerOptions.AbsoluteExpiration = TimeSpan.FromSeconds(1); + + Assert.Equal(TimeSpan.FromSeconds(1), options.CacheManagerOptions.AbsoluteExpiration); + Assert.Equal(TimeSpan.FromSeconds(1), options.CacheManagerOptions.SlidingExpiration); + Assert.True(options.CacheManagerOptions.Enable); + } } From 2ca04a6a8fc03e5aafcbf3b1d11ec9277c8d8014 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Thu, 6 Feb 2025 11:10:57 +0800 Subject: [PATCH 6/6] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=20SetDefaultSlid?= =?UTF-8?q?ingExpiration=20=E6=89=A9=E5=B1=95=E6=96=B9=E6=B3=95=E7=B2=BE?= =?UTF-8?q?=E7=AE=80=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Extensions/ICacheEntryExtensions.cs | 13 +++++++++++++ src/BootstrapBlazor/Services/CacheManager.cs | 14 ++------------ 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/BootstrapBlazor/Extensions/ICacheEntryExtensions.cs b/src/BootstrapBlazor/Extensions/ICacheEntryExtensions.cs index 925fefcf6fa..6bde1aaf3ca 100644 --- a/src/BootstrapBlazor/Extensions/ICacheEntryExtensions.cs +++ b/src/BootstrapBlazor/Extensions/ICacheEntryExtensions.cs @@ -40,4 +40,17 @@ public static class ICacheEntryExtensions } private static PropertyInfo? _lastAccessedProperty = null; + + /// + /// Sets default sliding expiration if no expiration is configured + /// + internal static void SetDefaultSlidingExpiration(this ICacheEntry entry, TimeSpan offset) + { + if (entry.SlidingExpiration == null && entry.AbsoluteExpiration == null + && entry.AbsoluteExpirationRelativeToNow == null + && entry.Priority != CacheItemPriority.NeverRemove) + { + entry.SetSlidingExpiration(offset); + } + } } diff --git a/src/BootstrapBlazor/Services/CacheManager.cs b/src/BootstrapBlazor/Services/CacheManager.cs index 007b154346e..90eca88ad94 100644 --- a/src/BootstrapBlazor/Services/CacheManager.cs +++ b/src/BootstrapBlazor/Services/CacheManager.cs @@ -55,12 +55,7 @@ public TItem GetOrCreate(object key, Func factory) => { var item = factory(entry); - if (entry.SlidingExpiration == null && entry.AbsoluteExpiration == null - && entry.AbsoluteExpirationRelativeToNow == null - && entry.Priority != CacheItemPriority.NeverRemove) - { - entry.SetSlidingExpiration(Options.CacheManagerOptions.SlidingExpiration); - } + entry.SetDefaultSlidingExpiration(Options.CacheManagerOptions.SlidingExpiration); return item; })!; @@ -71,12 +66,7 @@ public Task GetOrCreateAsync(object key, Func