Skip to content

Commit

Permalink
feat(CacheManagerOptions): add CacheManagerOptions support configue c…
Browse files Browse the repository at this point in the history
…ache expiration (#5310)

* feat: 增加 CacheManagerOptions 配置类

* feat: 增加 CacheManagerOptions 参数

* feat: 使用 CacheManagerOptions 值代替常量

* doc: 更新缓存时长计算方法

* test: 增加单元测试

* feat: 增加 SetDefaultSlidingExpiration 扩展方法精简代码
  • Loading branch information
ArgoZhang authored Feb 6, 2025
1 parent 61eefd1 commit 93817bc
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 18 deletions.
16 changes: 14 additions & 2 deletions src/BootstrapBlazor.Server/Extensions/ICacheEntryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}
}
13 changes: 13 additions & 0 deletions src/BootstrapBlazor/Extensions/ICacheEntryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,17 @@ public static class ICacheEntryExtensions
}

private static PropertyInfo? _lastAccessedProperty = null;

/// <summary>
/// Sets default sliding expiration if no expiration is configured
/// </summary>
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);
}
}
}
17 changes: 11 additions & 6 deletions src/BootstrapBlazor/Options/BootstrapBlazorOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,35 +91,40 @@ public class BootstrapBlazorOptions : IOptions<BootstrapBlazorOptions>
public TableSettings TableSettings { get; set; } = new();

/// <summary>
/// 获得/设置 Step 配置实例
/// 获得/设置 <see cref="StepSettings"/> 配置实例
/// </summary>
public StepSettings StepSettings { get; set; } = new();

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

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

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

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

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

/// <summary>
/// 获得/设置 CacheManagerOptions 配置 默认不为空
/// </summary>
public CacheManagerOptions CacheManagerOptions { get; set; } = new();

BootstrapBlazorOptions IOptions<BootstrapBlazorOptions>.Value => this;

/// <summary>
Expand Down
27 changes: 27 additions & 0 deletions src/BootstrapBlazor/Options/CacheManagerOptions.cs
Original file line number Diff line number Diff line change
@@ -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([email protected]) Website: https://www.blazor.zone

namespace BootstrapBlazor.Components;

/// <summary>
/// CacheManagerOptions 配置类
/// </summary>
public class CacheManagerOptions
{
/// <summary>
/// 获得/设置 是否开启 CacheManager 功能 默认 true 开启
/// </summary>
public bool Enable { get; set; } = true;

/// <summary>
/// 获得/设置 滑动缓存过期时间 默认 5 分钟
/// </summary>
public TimeSpan SlidingExpiration { get; set; } = TimeSpan.FromMinutes(5);

/// <summary>
/// 获得/设置 绝对缓存过期时间 默认 10 秒钟
/// </summary>
public TimeSpan AbsoluteExpiration { get; set; } = TimeSpan.FromSeconds(10);
}
18 changes: 8 additions & 10 deletions src/BootstrapBlazor/Services/CacheManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/// <summary>
Expand All @@ -42,6 +45,7 @@ public CacheManager(IServiceProvider provider, IMemoryCache memoryCache)
Provider = provider;
Cache = memoryCache;
Instance = this;
Options = Provider.GetRequiredService<IOptions<BootstrapBlazorOptions>>().Value;
}

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

if (entry.SlidingExpiration == null && entry.AbsoluteExpiration == null && entry.Priority != CacheItemPriority.NeverRemove)
{
entry.SetSlidingExpiration(TimeSpan.FromMinutes(5));
}
entry.SetDefaultSlidingExpiration(Options.CacheManagerOptions.SlidingExpiration);
return item;
})!;

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

if (entry.SlidingExpiration == null && entry.AbsoluteExpiration == null && entry.Priority != CacheItemPriority.NeverRemove)
{
entry.SetSlidingExpiration(TimeSpan.FromMinutes(5));
}
entry.SetDefaultSlidingExpiration(Options.CacheManagerOptions.SlidingExpiration);
return item;
})!;

Expand Down Expand Up @@ -530,7 +528,7 @@ private static TResult GetValue<TModel, TResult>(TModel model, string fieldName)
{
if (type.Assembly.IsDynamic)
{
entry.SetAbsoluteExpiration(TimeSpan.FromSeconds(10));
entry.SetAbsoluteExpiration(Options.CacheManagerOptions.AbsoluteExpiration);
}

return LambdaExtensions.GetPropertyValueLambda<TModel, TResult>(model, fieldName).Compile();
Expand All @@ -557,7 +555,7 @@ public static void SetPropertyValue<TModel, TValue>(TModel model, string fieldNa
{
if (type.Assembly.IsDynamic)
{
entry.SetAbsoluteExpiration(TimeSpan.FromSeconds(10));
entry.SetAbsoluteExpiration(Options.CacheManagerOptions.AbsoluteExpiration);
}
return LambdaExtensions.SetPropertyValueLambda<TModel, TValue>(model, fieldName).Compile();
});
Expand Down
15 changes: 15 additions & 0 deletions test/UnitTest/Options/BootstrapBlazorOptionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 93817bc

Please sign in to comment.