Skip to content

Commit 87c7cdf

Browse files
committed
doc: 增加过期时间
1 parent 2929299 commit 87c7cdf

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@ExpirationTime
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
8+
namespace BootstrapBlazor.Server.Components.Pages;
9+
10+
/// <summary>
11+
/// CacaheExpiration 组件
12+
/// </summary>
13+
public partial class CacaheExpiration
14+
{
15+
[Inject, NotNull]
16+
private ICacheManager? CacheManager { get; set; }
17+
18+
/// <summary>
19+
/// 获得/设置 <see cref="TableColumnContext{TItem, TValue}"/> 实例
20+
/// </summary>
21+
[Parameter, NotNull]
22+
public object? Key { get; set; }
23+
24+
private string? ExpirationTime { get; set; }
25+
26+
/// <summary>
27+
/// <inheritdoc/>
28+
/// </summary>
29+
/// <returns></returns>
30+
protected override async Task OnParametersSetAsync()
31+
{
32+
await base.OnParametersSetAsync();
33+
34+
await GetCacheEntryExpiration();
35+
}
36+
37+
private async Task GetCacheEntryExpiration()
38+
{
39+
ExpirationTime = "loading ...";
40+
await Task.Yield();
41+
42+
if (CacheManager.TryGetCacheEntry(Key, out ICacheEntry? entry))
43+
{
44+
if (entry.Priority == CacheItemPriority.NeverRemove)
45+
{
46+
ExpirationTime = "Never Remove";
47+
}
48+
else if (entry.SlidingExpiration.HasValue)
49+
{
50+
ExpirationTime = $"Sliding: {entry.SlidingExpiration.Value}";
51+
}
52+
else if (entry.AbsoluteExpiration.HasValue)
53+
{
54+
ExpirationTime = $"Absolute: {entry.AbsoluteExpiration.Value}";
55+
}
56+
else if (entry.ExpirationTokens.Count != 0)
57+
{
58+
ExpirationTime = $"Token: {entry.ExpirationTokens.Count}";
59+
}
60+
}
61+
}
62+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
</TableTemplateColumn>
2121
<TableTemplateColumn Text="@Localizer["CacheListExpiration"]" Width="160">
2222
<Template Context="v">
23-
23+
<CacaheExpiration Key="v.Row"></CacaheExpiration>
2424
</Template>
2525
</TableTemplateColumn>
2626
<TableTemplateColumn Text="@Localizer["CacheListAction"]" Width="80">

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private void OnRefresh()
4949

5050
private void UpdateCacheList()
5151
{
52-
_cacheList = CacheManager.Keys.OrderBy(i => i.ToString()).ToList();
52+
_cacheList = [.. CacheManager.Keys.OrderBy(i => i.ToString())];
5353
}
5454

5555
private string GetValue(object key)

0 commit comments

Comments
 (0)