Skip to content

Commit 81f99be

Browse files
authored
refactor(CacheList): prevent null item cause exception (#5430)
* doc: 更正单词拼写错误 * refactor: 重构 cache-list 逻辑 * fix: 修复删除按钮 * refactor: 增加过滤条件防止空记录
1 parent cf9977e commit 81f99be

File tree

4 files changed

+14
-13
lines changed

4 files changed

+14
-13
lines changed

src/BootstrapBlazor.Server/Components/Pages/CacheList.razor.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,24 @@ public partial class CacheList
1919
[Inject, NotNull]
2020
private IStringLocalizer<CacheList>? Localizer { get; set; }
2121

22-
private List<object> _cacheList = [];
22+
private List<object?> _cacheList = [];
2323

2424
/// <summary>
2525
/// <inheritdoc/>
2626
/// </summary>
27-
protected override async Task OnParametersSetAsync()
27+
protected override void OnInitialized()
2828
{
29-
await base.OnParametersSetAsync();
30-
31-
await Task.Yield();
29+
base.OnInitialized();
3230
UpdateCacheList();
3331
}
3432

3533
private void OnDelete(object key)
3634
{
37-
CacheManager.Clear(key);
38-
UpdateCacheList();
35+
if (key is ICacheEntry entry)
36+
{
37+
CacheManager.Clear(entry.Key);
38+
UpdateCacheList();
39+
}
3940
}
4041

4142
private void OnDeleteAll()
@@ -51,15 +52,15 @@ private void OnRefresh()
5152

5253
private void UpdateCacheList()
5354
{
54-
_cacheList = CacheManager.Keys.OrderBy(i => i.ToString()).Select(key =>
55+
_cacheList = [.. CacheManager.Keys.OrderBy(i => i.ToString()).Select(key =>
5556
{
5657
ICacheEntry? entry = null;
5758
if (CacheManager.TryGetCacheEntry(key, out var val))
5859
{
5960
entry = val;
6061
}
61-
return (object)entry!;
62-
}).ToList();
62+
return entry;
63+
}).Where(i => i != null)];
6364
}
6465

6566
private static string GetKey(object data) => data is ICacheEntry entry ? entry.Key.ToString()! : "-";

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

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

6-
<DemoBlock Title="@Localizer["BasicTitle"]" Introduction="@Localizer["BasicIntroe"]" Name="Normal">
6+
<DemoBlock Title="@Localizer["BasicTitle"]" Introduction="@Localizer["BasicIntro"]" Name="Normal">
77
<section ignore class="row g-3">
88
<div class="col-12">
99
<BootstrapInputGroup>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6469,7 +6469,7 @@
64696469
"BootstrapBlazor.Server.Components.Samples.Stacks": {
64706470
"Title": "Stack Layout",
64716471
"BasicTitle": "Basic",
6472-
"BasicIntroe": "Lets you arrange its subcomponents in a horizontal or vertical stack",
6472+
"BasicIntro": "Lets you arrange its subcomponents in a horizontal or vertical stack",
64736473
"RowMode": "Row",
64746474
"ColumnMode": "Column",
64756475
"Mode": "mode",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6469,7 +6469,7 @@
64696469
"BootstrapBlazor.Server.Components.Samples.Stacks": {
64706470
"Title": "Stack 布局",
64716471
"BasicTitle": "普通用法",
6472-
"BasicIntroe": "可用于在水平或垂直堆栈中排列其子组件",
6472+
"BasicIntro": "可用于在水平或垂直堆栈中排列其子组件",
64736473
"RowMode": "行布局",
64746474
"ColumnMode": "列布局",
64756475
"Mode": "布局类型",

0 commit comments

Comments
 (0)