Skip to content

Commit 93817bc

Browse files
authored
feat(CacheManagerOptions): add CacheManagerOptions support configue cache expiration (#5310)
* feat: 增加 CacheManagerOptions 配置类 * feat: 增加 CacheManagerOptions 参数 * feat: 使用 CacheManagerOptions 值代替常量 * doc: 更新缓存时长计算方法 * test: 增加单元测试 * feat: 增加 SetDefaultSlidingExpiration 扩展方法精简代码
1 parent 61eefd1 commit 93817bc

File tree

6 files changed

+88
-18
lines changed

6 files changed

+88
-18
lines changed

src/BootstrapBlazor.Server/Extensions/ICacheEntryExtensions.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ public static string GetExpiration(this ICacheEntry entry)
2727
}
2828
else if (entry.SlidingExpiration.HasValue)
2929
{
30-
ret = $"Sliding: {entry.GetSlidingLeftTime().TotalSeconds:###}/{entry.SlidingExpiration.Value.TotalSeconds}";
30+
var ts = entry.GetSlidingLeftTime();
31+
ret = ts == TimeSpan.Zero ? "Expirated" : $"Sliding: {ts.TotalSeconds:###}/{entry.SlidingExpiration.Value.TotalSeconds}";
3132
}
3233
else if (entry.AbsoluteExpiration.HasValue)
3334
{
34-
ret = $"Absolute: {entry.AbsoluteExpiration.Value}";
35+
var ts = entry.GetAbsoluteLeftTime();
36+
ret = ts == TimeSpan.Zero ? "Expirated" : $"Absolute: {ts.TotalSeconds:###}";
3537
}
3638
else if (entry.ExpirationTokens.Count != 0)
3739
{
@@ -59,4 +61,14 @@ private static TimeSpan GetSlidingLeftTime(this ICacheEntry entry)
5961
}
6062
return ts;
6163
}
64+
65+
private static TimeSpan GetAbsoluteLeftTime(this ICacheEntry entry)
66+
{
67+
var ts = entry.AbsoluteExpiration!.Value - DateTimeOffset.UtcNow;
68+
if (ts < TimeSpan.Zero)
69+
{
70+
ts = TimeSpan.Zero;
71+
}
72+
return ts;
73+
}
6274
}

src/BootstrapBlazor/Extensions/ICacheEntryExtensions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,17 @@ public static class ICacheEntryExtensions
4040
}
4141

4242
private static PropertyInfo? _lastAccessedProperty = null;
43+
44+
/// <summary>
45+
/// Sets default sliding expiration if no expiration is configured
46+
/// </summary>
47+
internal static void SetDefaultSlidingExpiration(this ICacheEntry entry, TimeSpan offset)
48+
{
49+
if (entry.SlidingExpiration == null && entry.AbsoluteExpiration == null
50+
&& entry.AbsoluteExpirationRelativeToNow == null
51+
&& entry.Priority != CacheItemPriority.NeverRemove)
52+
{
53+
entry.SetSlidingExpiration(offset);
54+
}
55+
}
4356
}

src/BootstrapBlazor/Options/BootstrapBlazorOptions.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,35 +91,40 @@ public class BootstrapBlazorOptions : IOptions<BootstrapBlazorOptions>
9191
public TableSettings TableSettings { get; set; } = new();
9292

9393
/// <summary>
94-
/// 获得/设置 Step 配置实例
94+
/// 获得/设置 <see cref="StepSettings"/> 配置实例
9595
/// </summary>
9696
public StepSettings StepSettings { get; set; } = new();
9797

9898
/// <summary>
99-
/// 获得/设置 ConnectionHubOptions 配置 默认不为空
99+
/// 获得/设置 <see cref="ConnectionHubOptions"/> 配置 默认不为空
100100
/// </summary>
101101
public ConnectionHubOptions ConnectionHubOptions { get; set; } = new();
102102

103103
/// <summary>
104-
/// 获得/设置 WebClientOptions 配置 默认不为空
104+
/// 获得/设置 <see cref="WebClientOptions"/> 配置 默认不为空
105105
/// </summary>
106106
public WebClientOptions WebClientOptions { get; set; } = new();
107107

108108
/// <summary>
109-
/// 获得/设置 IpLocatorOptions 配置 默认不为空
109+
/// 获得/设置 <see cref="IpLocatorOptions"/> 配置 默认不为空
110110
/// </summary>
111111
public IpLocatorOptions IpLocatorOptions { get; set; } = new();
112112

113113
/// <summary>
114-
/// 获得/设置 ScrollOptions 配置 默认不为空
114+
/// 获得/设置 <see cref="ScrollOptions"/> 配置 默认不为空
115115
/// </summary>
116116
public ScrollOptions ScrollOptions { get; set; } = new();
117117

118118
/// <summary>
119-
/// 获得/设置 ContextMenuOptions 配置 默认不为空
119+
/// 获得/设置 <see cref="ContextMenuOptions"/> 配置 默认不为空
120120
/// </summary>
121121
public ContextMenuOptions ContextMenuOptions { get; set; } = new();
122122

123+
/// <summary>
124+
/// 获得/设置 CacheManagerOptions 配置 默认不为空
125+
/// </summary>
126+
public CacheManagerOptions CacheManagerOptions { get; set; } = new();
127+
123128
BootstrapBlazorOptions IOptions<BootstrapBlazorOptions>.Value => this;
124129

125130
/// <summary>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License
3+
// See the LICENSE file in the project root for more information.
4+
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
5+
6+
namespace BootstrapBlazor.Components;
7+
8+
/// <summary>
9+
/// CacheManagerOptions 配置类
10+
/// </summary>
11+
public class CacheManagerOptions
12+
{
13+
/// <summary>
14+
/// 获得/设置 是否开启 CacheManager 功能 默认 true 开启
15+
/// </summary>
16+
public bool Enable { get; set; } = true;
17+
18+
/// <summary>
19+
/// 获得/设置 滑动缓存过期时间 默认 5 分钟
20+
/// </summary>
21+
public TimeSpan SlidingExpiration { get; set; } = TimeSpan.FromMinutes(5);
22+
23+
/// <summary>
24+
/// 获得/设置 绝对缓存过期时间 默认 10 秒钟
25+
/// </summary>
26+
public TimeSpan AbsoluteExpiration { get; set; } = TimeSpan.FromSeconds(10);
27+
}

src/BootstrapBlazor/Services/CacheManager.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ internal class CacheManager : ICacheManager
3030
[NotNull]
3131
private static CacheManager? Instance { get; set; }
3232

33+
[NotNull]
34+
private static BootstrapBlazorOptions? Options { get; set; }
35+
3336
private const string CacheKeyPrefix = "BootstrapBlazor";
3437

3538
/// <summary>
@@ -42,6 +45,7 @@ public CacheManager(IServiceProvider provider, IMemoryCache memoryCache)
4245
Provider = provider;
4346
Cache = memoryCache;
4447
Instance = this;
48+
Options = Provider.GetRequiredService<IOptions<BootstrapBlazorOptions>>().Value;
4549
}
4650

4751
/// <summary>
@@ -51,10 +55,7 @@ public TItem GetOrCreate<TItem>(object key, Func<ICacheEntry, TItem> factory) =>
5155
{
5256
var item = factory(entry);
5357

54-
if (entry.SlidingExpiration == null && entry.AbsoluteExpiration == null && entry.Priority != CacheItemPriority.NeverRemove)
55-
{
56-
entry.SetSlidingExpiration(TimeSpan.FromMinutes(5));
57-
}
58+
entry.SetDefaultSlidingExpiration(Options.CacheManagerOptions.SlidingExpiration);
5859
return item;
5960
})!;
6061

@@ -65,10 +66,7 @@ public Task<TItem> GetOrCreateAsync<TItem>(object key, Func<ICacheEntry, Task<TI
6566
{
6667
var item = await factory(entry);
6768

68-
if (entry.SlidingExpiration == null && entry.AbsoluteExpiration == null && entry.Priority != CacheItemPriority.NeverRemove)
69-
{
70-
entry.SetSlidingExpiration(TimeSpan.FromMinutes(5));
71-
}
69+
entry.SetDefaultSlidingExpiration(Options.CacheManagerOptions.SlidingExpiration);
7270
return item;
7371
})!;
7472

@@ -530,7 +528,7 @@ private static TResult GetValue<TModel, TResult>(TModel model, string fieldName)
530528
{
531529
if (type.Assembly.IsDynamic)
532530
{
533-
entry.SetAbsoluteExpiration(TimeSpan.FromSeconds(10));
531+
entry.SetAbsoluteExpiration(Options.CacheManagerOptions.AbsoluteExpiration);
534532
}
535533

536534
return LambdaExtensions.GetPropertyValueLambda<TModel, TResult>(model, fieldName).Compile();
@@ -557,7 +555,7 @@ public static void SetPropertyValue<TModel, TValue>(TModel model, string fieldNa
557555
{
558556
if (type.Assembly.IsDynamic)
559557
{
560-
entry.SetAbsoluteExpiration(TimeSpan.FromSeconds(10));
558+
entry.SetAbsoluteExpiration(Options.CacheManagerOptions.AbsoluteExpiration);
561559
}
562560
return LambdaExtensions.SetPropertyValueLambda<TModel, TValue>(model, fieldName).Compile();
563561
});

test/UnitTest/Options/BootstrapBlazorOptionsTest.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,19 @@ public void Options_TableExportOptions()
103103

104104
Assert.Equal(",", exportOptions.ArrayDelimiter);
105105
}
106+
107+
[Fact]
108+
public void CacheManagerOptions_Ok()
109+
{
110+
var options = new BootstrapBlazorOptions();
111+
Assert.NotNull(options.CacheManagerOptions);
112+
113+
options.CacheManagerOptions.Enable = true;
114+
options.CacheManagerOptions.SlidingExpiration = TimeSpan.FromSeconds(1);
115+
options.CacheManagerOptions.AbsoluteExpiration = TimeSpan.FromSeconds(1);
116+
117+
Assert.Equal(TimeSpan.FromSeconds(1), options.CacheManagerOptions.AbsoluteExpiration);
118+
Assert.Equal(TimeSpan.FromSeconds(1), options.CacheManagerOptions.SlidingExpiration);
119+
Assert.True(options.CacheManagerOptions.Enable);
120+
}
106121
}

0 commit comments

Comments
 (0)