Skip to content

Commit da7715e

Browse files
refactor(CacheManager): remove some cache item (#5161)
* feat(ICacheManager): add Keys parameter * test: 更新单元测试 * feat: add cache list page * refactor: 移除 GetStringLocalizerFromService 缓存 * doc: 增加删除所有缓存键值按钮 * refactor: 重构代码 * doc: 增加 CacheList 页面 * doc: 格式化水印文档 * doc: 增加缓存值列 * refactor: 更新值 * doc: 增加缓存项统计 * refactor: 移除 GetPlaceholder 缓存 * refactor: 移除 TryGetProperty 缓存 * doc: 更新资源文件 * refactor GetDisplayName 移除缓存 * refactor:获得属性私有字段方法移除缓存 * refactor: 弃用 ReloadOnChange 参数 * refactor: 更新 GetJsonStringFromAssembly 逻辑 * refactor: GetRange 移除缓存 * refactor: 精简 Lambda 表达式缓存 * refactor: 移除 DEBUG 模式下缓存设置 * refactor: 移除 SetSlidingExpirationByType 扩展方法 * refactor: 移除非 JsonStringLocalizer 缓存未找到键值缓存逻辑 * refactor: 代码重构减少函数调用 Co-Authored-By: Alex chow <[email protected]>
1 parent 72868a6 commit da7715e

File tree

14 files changed

+256
-292
lines changed

14 files changed

+256
-292
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
@page "/cache-list"
2+
3+
<h3>@Localizer["CacheListTitle"]</h3>
4+
5+
<h4>@((MarkupString)Localizer["CacheListIntro"].Value)</h4>
6+
7+
<div class="table-cache-list">
8+
<Table Items="_cacheList" IsBordered="true" IsStriped="true" IsFixedHeader="true"
9+
ShowToolbar="true" ShowAddButton="false" ShowEditButton="false" ShowDeleteButton="false" ShowRefresh="false">
10+
<TableColumns>
11+
<TableTemplateColumn Text="@Localizer["CacheListKey"]" TextWrap="true">
12+
<Template Context="v">
13+
@v.Row.ToString()
14+
</Template>
15+
</TableTemplateColumn>
16+
<TableTemplateColumn Text="@Localizer["CacheListValue"]" TextWrap="true" Width="220">
17+
<Template Context="v">
18+
@GetValue(v.Row)
19+
</Template>
20+
</TableTemplateColumn>
21+
<TableTemplateColumn Text="@Localizer["CacheListAction"]" Width="80">
22+
<Template Context="v">
23+
<Button Size="Size.ExtraSmall" Color="Color.Danger" OnClick="() => OnDelete(v.Row)" Icon="fa-solid fa-xmark" Text="@Localizer["CacheListDelete"]"></Button>
24+
</Template>
25+
</TableTemplateColumn>
26+
</TableColumns>
27+
<TableToolbarBeforeTemplate>
28+
<Button Text="@Localizer["CacheListDeleteAll"]" Icon="fa-solid fa-xmark" OnClick="OnDeleteAll"></Button>
29+
</TableToolbarBeforeTemplate>
30+
<TableExtensionToolbarTemplate>
31+
<Button Text="@Localizer["CacheListRefresh"]" Icon="fa-solid fa-refresh" OnClick="OnRefresh"></Button>
32+
</TableExtensionToolbarTemplate>
33+
</Table>
34+
</div>
35+
<div class="mt-2">
36+
@Localizer["CacheListCount", CacheManager.Count]
37+
</div>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
using Microsoft.Extensions.Caching.Memory;
7+
using System.Collections;
8+
9+
namespace BootstrapBlazor.Server.Components.Pages;
10+
11+
/// <summary>
12+
/// CacheManager 管理组件
13+
/// </summary>
14+
public partial class CacheList
15+
{
16+
[Inject, NotNull]
17+
private ICacheManager? CacheManager { get; set; }
18+
19+
[Inject, NotNull]
20+
private IStringLocalizer<CacheList>? Localizer { get; set; }
21+
22+
private List<object>? _cacheList;
23+
24+
/// <summary>
25+
/// <inheritdoc/>
26+
/// </summary>
27+
protected override void OnInitialized()
28+
{
29+
base.OnInitialized();
30+
UpdateCacheList();
31+
}
32+
33+
private void OnDelete(object key)
34+
{
35+
CacheManager.Clear(key);
36+
UpdateCacheList();
37+
}
38+
39+
private void OnDeleteAll()
40+
{
41+
CacheManager.Clear();
42+
UpdateCacheList();
43+
}
44+
45+
private void OnRefresh()
46+
{
47+
UpdateCacheList();
48+
}
49+
50+
private void UpdateCacheList()
51+
{
52+
_cacheList = CacheManager.Keys.OrderBy(i => i.ToString()).ToList();
53+
}
54+
55+
private string GetValue(object key)
56+
{
57+
string ret = "-";
58+
if (CacheManager.TryGetValue(key, out object? value))
59+
{
60+
if (value is string stringValue)
61+
{
62+
ret = stringValue;
63+
return ret;
64+
}
65+
66+
if (value is IEnumerable)
67+
{
68+
ret = $"{LambdaExtensions.ElementCount(value)}";
69+
}
70+
else
71+
{
72+
ret = value?.ToString() ?? "-";
73+
}
74+
}
75+
76+
return ret;
77+
}
78+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.table-cache-list {
2+
height: calc(100vh - 180px);
3+
}

src/BootstrapBlazor.Server/Components/Samples/Watermarks.razor

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,38 @@
33

44
<h3>@Localizer["WatermarkTitle"]</h3>
55

6-
<h4>@Localizer["Watermarkntro"]</h4>
6+
<h4>@Localizer["WatermarkIntro"]</h4>
77

88
<DemoBlock Title="@Localizer["WatermarkNormalTitle"]" Introduction="@Localizer["WatermarkNormalIntro"]" Name="Normal">
99
<section ignore>
1010
<div class="row form-inline g-3">
1111
<div class="col-12 col-sm-6">
1212
<BootstrapInputGroup>
13-
<BootstrapInputGroupLabel DisplayText="Text"></BootstrapInputGroupLabel>
13+
<BootstrapInputGroupLabel DisplayText="Text" Width="76"></BootstrapInputGroupLabel>
1414
<BootstrapInput @bind-Value="@_text"></BootstrapInput>
1515
</BootstrapInputGroup>
1616
</div>
1717
<div class="col-12 col-sm-6">
1818
<BootstrapInputGroup>
19-
<BootstrapInputGroupLabel DisplayText="FontSize"></BootstrapInputGroupLabel>
19+
<BootstrapInputGroupLabel DisplayText="FontSize" Width="76"></BootstrapInputGroupLabel>
2020
<Slider @bind-Value="@_fontSize" Min="12" Max="20"></Slider>
2121
</BootstrapInputGroup>
2222
</div>
2323
<div class="col-12 col-sm-6">
2424
<BootstrapInputGroup>
25-
<BootstrapInputGroupLabel DisplayText="Color"></BootstrapInputGroupLabel>
25+
<BootstrapInputGroupLabel DisplayText="Color" Width="76"></BootstrapInputGroupLabel>
2626
<ColorPicker @bind-Value="@_color" IsSupportOpacity="true"></ColorPicker>
2727
</BootstrapInputGroup>
2828
</div>
2929
<div class="col-12 col-sm-6">
3030
<BootstrapInputGroup>
31-
<BootstrapInputGroupLabel DisplayText="Rotate"></BootstrapInputGroupLabel>
31+
<BootstrapInputGroupLabel DisplayText="Rotate" Width="76"></BootstrapInputGroupLabel>
3232
<Slider @bind-Value="@_rotate" Min="-180" Max="180"></Slider>
3333
</BootstrapInputGroup>
3434
</div>
3535
<div class="col-12 col-sm-6">
3636
<BootstrapInputGroup>
37-
<BootstrapInputGroupLabel DisplayText="Gap"></BootstrapInputGroupLabel>
37+
<BootstrapInputGroupLabel DisplayText="Gap" Width="76"></BootstrapInputGroupLabel>
3838
<Slider @bind-Value="@_gap" Min="0" Max="100"></Slider>
3939
</BootstrapInputGroup>
4040
</div>

src/BootstrapBlazor.Server/Locales/en-US.json

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6881,12 +6881,11 @@
68816881
"AffixNormalTitle": "Basic usage",
68826882
"AffixNormalIntro": "Affix is fixed at the top of the page by default",
68836883
"AffixPositionTitle": "Position",
6884-
"AffixPositionIntro": "Use the parameter <code>Position</code> to control whether the top or bottom is fixed",
6885-
"AffixOffsetDesc": "The parameter <code>Position</code> controls whether the top or bottom is fixed, and the <code>Offset</code> value sets the offset to the top or bottom"
6884+
"AffixPositionIntro": "The parameter <code>Position</code> controls whether the top or bottom is fixed, and the <code>Offset</code> value sets the offset to the top or bottom"
68866885
},
68876886
"BootstrapBlazor.Server.Components.Samples.Watermarks": {
68886887
"WatermarkTitle": "Watermark",
6889-
"Watermarkntro": "Add specific text or patterns to the page",
6888+
"WatermarkIntro": "Add specific text or patterns to the page",
68906889
"WatermarkNormalTitle": "Basic usage",
68916890
"WatermarkNormalIntro": "Use the <code>Text</code> property to set a string to specify the watermark text"
68926891
},
@@ -6901,5 +6900,16 @@
69016900
"Name": "Name",
69026901
"Description": "Description",
69036902
"Type": "Type"
6903+
},
6904+
"BootstrapBlazor.Server.Components.Pages.CacheList": {
6905+
"CacheListTitle": "CacheManager",
6906+
"CacheListIntro": "Manage the component library internal cache through the <code>ICacheManager</code> interface method",
6907+
"CacheListKey": "Key",
6908+
"CacheListValue": "Value",
6909+
"CacheListAction": "Action",
6910+
"CacheListRefresh": "Refresh",
6911+
"CacheListDelete": "Delete",
6912+
"CacheListDeleteAll": "Clear",
6913+
"CacheListCount": "Total {0} Entry"
69046914
}
69056915
}

src/BootstrapBlazor.Server/Locales/zh-CN.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6885,7 +6885,7 @@
68856885
},
68866886
"BootstrapBlazor.Server.Components.Samples.Watermarks": {
68876887
"WatermarkTitle": "Watermark 水印组件",
6888-
"Watermarkntro": "在页面上添加文本或图片等水印信息",
6888+
"WatermarkIntro": "在页面上添加文本或图片等水印信息",
68896889
"WatermarkNormalTitle": "基础用法",
68906890
"WatermarkNormalIntro": "使用 <code>Text</code> 属性设置一个字符串指定水印内容"
68916891
},
@@ -6900,5 +6900,16 @@
69006900
"Name": "参数",
69016901
"Description": "说明",
69026902
"Type": "类型"
6903+
},
6904+
"BootstrapBlazor.Server.Components.Pages.CacheList": {
6905+
"CacheListTitle": "缓存管理",
6906+
"CacheListIntro": "通过 <code>ICacheManager</code> 接口方法管理组件库内部缓存",
6907+
"CacheListKey": "",
6908+
"CacheListValue": "",
6909+
"CacheListAction": "操作",
6910+
"CacheListRefresh": "刷新",
6911+
"CacheListDelete": "删除",
6912+
"CacheListDeleteAll": "清除全部",
6913+
"CacheListCount": "共 {0} 个键值"
69036914
}
69046915
}

src/BootstrapBlazor/Extensions/ICacheEntryExtensions.cs

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/BootstrapBlazor/Extensions/LocalizationOptionsExtensions.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,6 @@ public static IEnumerable<IConfigurationSection> GetJsonStringFromAssembly(this
2828

2929
// 获取程序集中的资源文件
3030
var assemblies = new List<Assembly>() { assembly };
31-
32-
var entryAssembly = GetAssembly();
33-
if (assembly != entryAssembly)
34-
{
35-
assemblies.Add(entryAssembly);
36-
}
37-
3831
if (option.AdditionalJsonAssemblies != null)
3932
{
4033
assemblies.AddRange(option.AdditionalJsonAssemblies);
@@ -58,7 +51,7 @@ public static IEnumerable<IConfigurationSection> GetJsonStringFromAssembly(this
5851
});
5952
foreach (var file in files)
6053
{
61-
builder.AddJsonFile(file, true, option.ReloadOnChange);
54+
builder.AddJsonFile(file, true, false);
6255
}
6356
}
6457

@@ -72,9 +65,6 @@ public static IEnumerable<IConfigurationSection> GetJsonStringFromAssembly(this
7265
}
7366

7467
return config.GetChildren();
75-
76-
[ExcludeFromCodeCoverage]
77-
Assembly GetAssembly() => Assembly.GetEntryAssembly() ?? assembly;
7868
}
7969

8070
private static List<Stream> GetResourceStream(this JsonLocalizationOptions option, Assembly assembly, string cultureName)

src/BootstrapBlazor/Extensions/TypeExtensions.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ namespace BootstrapBlazor.Components;
1212

1313
internal static class TypeExtensions
1414
{
15-
public static PropertyInfo? GetPropertyByName(this Type type, string propertyName) => CacheManager.GetRuntimeProperties(type).Find(p => p.Name == propertyName);
15+
public static PropertyInfo? GetPropertyByName(this Type type, string propertyName) => type.GetRuntimeProperties().FirstOrDefault(p => p.Name == propertyName);
1616

17-
public static FieldInfo? GetFieldByName(this Type type, string fieldName) => CacheManager.GetRuntimeFields(type).Find(p => p.Name == fieldName);
17+
public static FieldInfo? GetFieldByName(this Type type, string fieldName) => type.GetRuntimeFields().FirstOrDefault(p => p.Name == fieldName);
1818

1919
public static async Task<bool> IsAuthorizedAsync(this Type type, IServiceProvider serviceProvider, Task<AuthenticationState>? authenticateState, object? resource = null)
2020
{
@@ -46,9 +46,8 @@ void EnsureNoAuthenticationSchemeSpecified()
4646
// It's not meaningful to specify a nonempty scheme, since by the time Components
4747
// authorization runs, we already have a specific ClaimsPrincipal (we're stateful).
4848
// To avoid any confusion, ensure the developer isn't trying to specify a scheme.
49-
for (var i = 0; i < authorizeData.Length; i++)
49+
foreach (var entry in authorizeData)
5050
{
51-
var entry = authorizeData[i];
5251
if (!string.IsNullOrEmpty(entry.AuthenticationSchemes))
5352
{
5453
throw new NotSupportedException($"The authorization data specifies an authentication scheme with value '{entry.AuthenticationSchemes}'. Authentication schemes cannot be specified for components.");

src/BootstrapBlazor/Localization/Json/JsonLocalizationOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public class JsonLocalizationOptions : LocalizationOptions
5151
/// <summary>
5252
/// 获得/设置 资源文件是否热加载 默认 false
5353
/// </summary>
54+
[Obsolete("已弃用 Deprecated")]
55+
[ExcludeFromCodeCoverage]
5456
public bool ReloadOnChange { get; set; }
5557

5658
/// <summary>

0 commit comments

Comments
 (0)