Skip to content

Commit 1d9f784

Browse files
authored
doc(CacheList): update GetSlidingLeftTime logic (#5228)
* doc: 更新获取滑动时间剩余时间逻辑 * doc: add CacheCounter component
1 parent 3e217f1 commit 1d9f784

File tree

7 files changed

+73
-34
lines changed

7 files changed

+73
-34
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<div class="footer-cache ms-2">
2+
<i class="fa-solid fa-microchip text-success"></i>
3+
<a href="./cache-list" target="_blank" style="color: #ddd;">@CacheManager.Keys.Count()</a>
4+
</div>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.Server.Components.Components;
7+
8+
/// <summary>
9+
/// 缓存数量组件
10+
/// </summary>
11+
public partial class CacheCounter : IDisposable
12+
{
13+
[Inject, NotNull]
14+
private ICacheManager? CacheManager { get; set; }
15+
16+
private readonly CancellationTokenSource _cancellationTokenSource = new();
17+
18+
/// <summary>
19+
/// <inheritdoc/>
20+
/// </summary>
21+
/// <param name="firstRender"></param>
22+
protected override async Task OnAfterRenderAsync(bool firstRender)
23+
{
24+
await base.OnAfterRenderAsync(firstRender);
25+
26+
try
27+
{
28+
await Task.Delay(5000, _cancellationTokenSource.Token);
29+
StateHasChanged();
30+
}
31+
catch { }
32+
}
33+
34+
/// <summary>
35+
/// <inheritdoc/>
36+
/// </summary>
37+
/// <exception cref="NotImplementedException"></exception>
38+
public void Dispose()
39+
{
40+
_cancellationTokenSource.Cancel();
41+
_cancellationTokenSource.Dispose();
42+
GC.SuppressFinalize(this);
43+
}
44+
}

src/BootstrapBlazor.Server/Components/Components/FooterCounter.razor

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
@if (_options.Enable)
66
{
77
<div class="footer-online ms-2">
8-
<i class="fa-solid fa-people-group"></i>
9-
<a href="./online" target="_blank">@ConnectionService.Count</a>
8+
<i class="fa-solid fa-people-group text-success"></i>
9+
<a href="./online" target="_blank" style="color: #ddd;">@ConnectionService.Count</a>
1010
</div>
1111
}

src/BootstrapBlazor.Server/Components/Components/FooterCounter.razor.cs

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public partial class FooterCounter : IDisposable
1414
{
1515
private string? Runtime { get; set; }
1616

17-
private CancellationTokenSource DisposeTokenSource { get; } = new();
17+
private CancellationTokenSource _disposeTokenSource = new();
1818

1919
private ConnectionHubOptions _options = default!;
2020

@@ -37,30 +37,17 @@ protected override void OnInitialized()
3737
/// <inheritdoc />
3838
/// </summary>
3939
/// <param name="firstRender"></param>
40-
protected override void OnAfterRender(bool firstRender)
40+
protected override async Task OnAfterRenderAsync(bool firstRender)
4141
{
42-
if (firstRender)
43-
{
44-
_ = Task.Run(async () =>
45-
{
46-
while (!DisposeTokenSource.IsCancellationRequested)
47-
{
48-
try
49-
{
50-
await Task.Delay(1000, DisposeTokenSource.Token);
51-
}
52-
catch (TaskCanceledException)
53-
{
42+
await base.OnAfterRenderAsync(firstRender);
5443

55-
}
56-
if (!DisposeTokenSource.IsCancellationRequested)
57-
{
58-
UpdateRuntime();
59-
await InvokeAsync(StateHasChanged);
60-
}
61-
}
62-
});
44+
try
45+
{
46+
await Task.Delay(1000, _disposeTokenSource.Token);
47+
UpdateRuntime();
48+
StateHasChanged();
6349
}
50+
catch { }
6451
}
6552

6653
private void UpdateRuntime()
@@ -73,8 +60,8 @@ private void Dispose(bool disposing)
7360
{
7461
if (disposing)
7562
{
76-
DisposeTokenSource.Cancel();
77-
DisposeTokenSource.Dispose();
63+
_disposeTokenSource.Cancel();
64+
_disposeTokenSource.Dispose();
7865
}
7966
}
8067

src/BootstrapBlazor.Server/Components/Components/FooterCounter.razor.css

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

src/BootstrapBlazor.Server/Components/Layout/HomeLayout.razor

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
<div>Powered by .NET @Version on @OS</div>
6464
<div class="ms-1">BB @VersionService.Version</div>
6565
<FooterCounter></FooterCounter>
66+
<CacheCounter></CacheCounter>
6667
</div>
6768
<div class="d-flex flex-fill align-items-center justify-content-center">
6869
<a class="d-none d-md-block me-3" href="@WebsiteOption.CurrentValue.GiteeRepositoryUrl" target="_blank">@Localizer["Footer"]</a>

src/BootstrapBlazor.Server/Extensions/ICacheEntryExtensions.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ public static string GetExpiration(this ICacheEntry entry)
4747
private static TimeSpan GetSlidingLeftTime(this ICacheEntry entry)
4848
{
4949
var lastAccessed = entry.GetLastAccessed();
50-
return lastAccessed == null ? TimeSpan.Zero : entry.SlidingExpiration!.Value - (DateTime.UtcNow - lastAccessed.Value);
50+
if (lastAccessed == null)
51+
{
52+
return TimeSpan.Zero;
53+
}
54+
55+
var ts = entry.SlidingExpiration!.Value - (DateTime.UtcNow - lastAccessed.Value);
56+
if (ts < TimeSpan.Zero)
57+
{
58+
ts = TimeSpan.Zero;
59+
}
60+
return ts;
5161
}
5262
}

0 commit comments

Comments
 (0)