-
-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ICacheEntry): add GetLastAccessed extension method (#5219)
* doc: 更改示例代码 * doc: 增加缓存时长描述 * refactor: 更改 OnGetDisplayText 增加可为空 * refactor: 重构 CacheManager 优化性能 * doc: 更新示例 * refactor: 增加扩展方法 * doc: 更新超时时长列内容 * doc: 实现过期时间实时更新 * refactor: 精简代码 * test: 更新单元测试 * test: 更新单元测试
- Loading branch information
Showing
11 changed files
with
195 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/BootstrapBlazor.Server/Extensions/ICacheEntryExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the Apache 2.0 License | ||
// See the LICENSE file in the project root for more information. | ||
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone | ||
|
||
using Microsoft.Extensions.Caching.Memory; | ||
using System.Reflection; | ||
|
||
namespace BootstrapBlazor.Server.Extensions; | ||
|
||
/// <summary> | ||
/// <see cref="ICacheEntry"/> 扩展方法 | ||
/// </summary> | ||
public static class ICacheEntryExtensions | ||
{ | ||
/// <summary> | ||
/// 获取缓存项过期时间 | ||
/// </summary> | ||
/// <param name="entry"></param> | ||
/// <returns></returns> | ||
public static string GetExpiration(this ICacheEntry entry) | ||
{ | ||
string? ret; | ||
if (entry.Priority == CacheItemPriority.NeverRemove) | ||
{ | ||
ret = "Never Remove"; | ||
} | ||
else if (entry.SlidingExpiration.HasValue) | ||
{ | ||
ret = $"Sliding: {entry.GetSlidingLeftTime().TotalSeconds:###}/{entry.SlidingExpiration.Value.TotalSeconds}"; | ||
} | ||
else if (entry.AbsoluteExpiration.HasValue) | ||
{ | ||
ret = $"Absolute: {entry.AbsoluteExpiration.Value}"; | ||
} | ||
else if (entry.ExpirationTokens.Count != 0) | ||
{ | ||
ret = $"Token: {entry.ExpirationTokens.Count}"; | ||
} | ||
else | ||
{ | ||
ret = "Not Set"; | ||
} | ||
return ret; | ||
} | ||
|
||
private static TimeSpan GetSlidingLeftTime(this ICacheEntry entry) | ||
{ | ||
var lastAccessed = entry.GetLastAccessed(); | ||
return lastAccessed == null ? TimeSpan.Zero : entry.SlidingExpiration!.Value - (DateTime.UtcNow - lastAccessed.Value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the Apache 2.0 License | ||
// See the LICENSE file in the project root for more information. | ||
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone | ||
|
||
using Microsoft.Extensions.Caching.Memory; | ||
using System.Reflection; | ||
|
||
namespace BootstrapBlazor.Components; | ||
|
||
/// <summary> | ||
/// <see cref="ICacheEntry"/> 扩展方法 | ||
/// </summary> | ||
public static class ICacheEntryExtensions | ||
{ | ||
/// <summary> | ||
/// 获得缓存项 <see cref="ICacheEntry"/> 最后访问时间 | ||
/// </summary> | ||
/// <param name="entry"></param> | ||
/// <param name="force"></param> | ||
/// <returns></returns> | ||
public static DateTime? GetLastAccessed(this ICacheEntry entry, bool force = false) | ||
{ | ||
if (force) | ||
{ | ||
_lastAccessedProperty = null; | ||
} | ||
_lastAccessedProperty ??= entry.GetType().GetProperty("LastAccessed", BindingFlags.Instance | BindingFlags.NonPublic); | ||
|
||
DateTime? ret = null; | ||
if (_lastAccessedProperty != null) | ||
{ | ||
var v = _lastAccessedProperty.GetValue(entry); | ||
if (v is DateTime val) | ||
{ | ||
ret = val; | ||
} | ||
} | ||
return ret; | ||
} | ||
|
||
private static PropertyInfo? _lastAccessedProperty = null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the Apache 2.0 License | ||
// See the LICENSE file in the project root for more information. | ||
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone | ||
|
||
using Microsoft.Extensions.Caching.Memory; | ||
using Microsoft.Extensions.Primitives; | ||
|
||
namespace UnitTest.Extensions; | ||
|
||
public class ICacheEntryExtensionsTest : BootstrapBlazorTestBase | ||
{ | ||
[Fact] | ||
public void GetLastAccessed_Ok() | ||
{ | ||
Cache.GetOrCreate("test_01", entry => | ||
{ | ||
return 1; | ||
}); | ||
|
||
Assert.True(Cache.TryGetCacheEntry("test_01", out var entry)); | ||
var v = entry.GetLastAccessed(true); | ||
Assert.NotNull(v); | ||
} | ||
|
||
[Fact] | ||
public void GetLastAccessed_Null() | ||
{ | ||
var mock = new MockCacheEntry(); | ||
var v = mock.GetLastAccessed(true); | ||
Assert.Null(v); | ||
} | ||
|
||
class MockCacheEntry : ICacheEntry | ||
{ | ||
public object Key { get; } | ||
public object? Value { get; set; } | ||
public DateTimeOffset? AbsoluteExpiration { get; set; } | ||
public TimeSpan? AbsoluteExpirationRelativeToNow { get; set; } | ||
public TimeSpan? SlidingExpiration { get; set; } | ||
public IList<IChangeToken> ExpirationTokens { get; } | ||
public IList<PostEvictionCallbackRegistration> PostEvictionCallbacks { get; } | ||
public CacheItemPriority Priority { get; set; } | ||
public long? Size { get; set; } | ||
|
||
private int LastAccessed { get; set; } | ||
|
||
public MockCacheEntry() | ||
{ | ||
Key = "_test"; | ||
ExpirationTokens = []; | ||
PostEvictionCallbacks = []; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |