Skip to content

Commit fb2c14d

Browse files
committed
doc: 更新缓存时长计算方法
1 parent cb7d53e commit fb2c14d

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/BootstrapBlazor.Server/Extensions/ICacheEntryExtensions.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ public static string GetExpiration(this ICacheEntry entry)
2727
}
2828
else if (entry.SlidingExpiration.HasValue)
2929
{
30-
ret = $"Sliding: {entry.GetSlidingLeftTime().TotalSeconds:###}/{entry.SlidingExpiration.Value.TotalSeconds}";
30+
var ts = entry.GetSlidingLeftTime();
31+
ret = ts == TimeSpan.Zero ? "Expirated" : $"Sliding: {ts.TotalSeconds:###}/{entry.SlidingExpiration.Value.TotalSeconds}";
3132
}
3233
else if (entry.AbsoluteExpiration.HasValue)
3334
{
34-
ret = $"Absolute: {entry.AbsoluteExpiration.Value}";
35+
var ts = entry.GetAbsoluteLeftTime();
36+
ret = ts == TimeSpan.Zero ? "Expirated" : $"Absolute: {ts.TotalSeconds:###}";
3537
}
3638
else if (entry.ExpirationTokens.Count != 0)
3739
{
@@ -59,4 +61,14 @@ private static TimeSpan GetSlidingLeftTime(this ICacheEntry entry)
5961
}
6062
return ts;
6163
}
64+
65+
private static TimeSpan GetAbsoluteLeftTime(this ICacheEntry entry)
66+
{
67+
var ts = entry.AbsoluteExpiration!.Value - DateTimeOffset.UtcNow;
68+
if (ts < TimeSpan.Zero)
69+
{
70+
ts = TimeSpan.Zero;
71+
}
72+
return ts;
73+
}
6274
}

0 commit comments

Comments
 (0)