-
-
Notifications
You must be signed in to change notification settings - Fork 365
feat(ICacheEntry): add GetLastAccessed extension method #5219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5a6fc88
doc: 更改示例代码
ArgoZhang 829d7c0
doc: 增加缓存时长描述
ArgoZhang c8a5b52
refactor: 更改 OnGetDisplayText 增加可为空
ArgoZhang 5b5d5ad
refactor: 重构 CacheManager 优化性能
ArgoZhang 123d5b3
doc: 更新示例
ArgoZhang 107f6ca
refactor: 增加扩展方法
ArgoZhang cc4158d
doc: 更新超时时长列内容
ArgoZhang 0f08dc5
doc: 实现过期时间实时更新
ArgoZhang cff8544
refactor: 精简代码
ArgoZhang b3e6d57
test: 更新单元测试
ArgoZhang 6eb8e8c
test: 更新单元测试
ArgoZhang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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); | ||
ArgoZhang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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() | ||
ArgoZhang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| 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(); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.