-
-
Notifications
You must be signed in to change notification settings - Fork 365
feat(ICacheManager): add TryGetCacheEntry method #5216
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions
1
src/BootstrapBlazor.Server/Components/Pages/CacaheExpiration.razor
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 @@ | ||
| @ExpirationTime |
62 changes: 62 additions & 0 deletions
62
src/BootstrapBlazor.Server/Components/Pages/CacaheExpiration.razor.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,62 @@ | ||
| // 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; | ||
|
|
||
| namespace BootstrapBlazor.Server.Components.Pages; | ||
|
|
||
| /// <summary> | ||
| /// CacaheExpiration 组件 | ||
| /// </summary> | ||
| public partial class CacaheExpiration | ||
| { | ||
| [Inject, NotNull] | ||
| private ICacheManager? CacheManager { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// 获得/设置 <see cref="TableColumnContext{TItem, TValue}"/> 实例 | ||
| /// </summary> | ||
| [Parameter, NotNull] | ||
| public object? Key { get; set; } | ||
|
|
||
| private string? ExpirationTime { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// <inheritdoc/> | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| protected override async Task OnParametersSetAsync() | ||
| { | ||
| await base.OnParametersSetAsync(); | ||
|
|
||
| await GetCacheEntryExpiration(); | ||
| } | ||
|
|
||
| private async Task GetCacheEntryExpiration() | ||
| { | ||
| ExpirationTime = "loading ..."; | ||
| await Task.Yield(); | ||
|
|
||
| if (CacheManager.TryGetCacheEntry(Key, out ICacheEntry? entry)) | ||
| { | ||
| if (entry.Priority == CacheItemPriority.NeverRemove) | ||
| { | ||
| ExpirationTime = "Never Remove"; | ||
| } | ||
| else if (entry.SlidingExpiration.HasValue) | ||
| { | ||
| ExpirationTime = $"Sliding: {entry.SlidingExpiration.Value}"; | ||
| } | ||
| else if (entry.AbsoluteExpiration.HasValue) | ||
| { | ||
| ExpirationTime = $"Absolute: {entry.AbsoluteExpiration.Value}"; | ||
| } | ||
| else if (entry.ExpirationTokens.Count != 0) | ||
| { | ||
| ExpirationTime = $"Token: {entry.ExpirationTokens.Count}"; | ||
| } | ||
| } | ||
| } | ||
| } | ||
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
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,41 @@ | ||
| // 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 System.Runtime.CompilerServices; | ||
|
|
||
| namespace UnitTest.Performance; | ||
|
|
||
| public class UnsafeAccessorTest | ||
| { | ||
| [Fact] | ||
| public void GetField_Ok() | ||
| { | ||
| var dummy = new Dummy(); | ||
| dummy.SetName("test"); | ||
| Assert.Equal("test", GetNameField(dummy)); | ||
| } | ||
|
|
||
| [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_name")] | ||
| static extern ref string GetNameField(Dummy @this); | ||
|
|
||
| private class Dummy | ||
| { | ||
| private string? _name; | ||
|
|
||
| /// <summary> | ||
| /// | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| public string? GetName() | ||
| { | ||
| return _name; | ||
| } | ||
|
|
||
| public void SetName(string? name) | ||
| { | ||
| _name = name; | ||
| } | ||
| } | ||
| } |
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,62 @@ | ||
| // 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 BenchmarkDotNet.Attributes; | ||
| using BenchmarkDotNet.Jobs; | ||
| using Microsoft.Diagnostics.Runtime.Utilities; | ||
| using System.Dynamic; | ||
| using System.Linq.Expressions; | ||
| using System.Reflection; | ||
| using System.Runtime.CompilerServices; | ||
|
|
||
| namespace UnitTest.Benchmarks; | ||
|
|
||
| [SimpleJob(RuntimeMoniker.Net90)] | ||
| public class Benchmarks | ||
| { | ||
| static readonly Foo _instance = new(); | ||
|
|
||
| static readonly FieldInfo _privateField = typeof(Foo).GetField("_name", BindingFlags.Instance | BindingFlags.NonPublic)!; | ||
|
|
||
| [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_name")] | ||
| static extern ref string GetNameValue(Foo @this); | ||
|
|
||
| [Benchmark] | ||
| public object? Reflection() | ||
| { | ||
| var fieldInfo = typeof(Foo).GetField("_name", BindingFlags.Instance | BindingFlags.NonPublic)!; | ||
| return fieldInfo.GetValue(_instance); | ||
| } | ||
|
|
||
| [Benchmark] | ||
| public object? ReflectionWithCache() => _privateField.GetValue(_instance); | ||
|
|
||
| [Benchmark] | ||
| public string UnsafeAccessor() => GetNameValue(_instance); | ||
|
|
||
| [Benchmark] | ||
| public string DirectAccess() => _instance.GetName(); | ||
|
|
||
| [Benchmark] | ||
| public string Lambda() => GetFieldValue(); | ||
|
|
||
| [Benchmark] | ||
| public string LambdaWithCache() => GetFooFieldFunc(_instance); | ||
|
|
||
| public string GetFieldValue() | ||
| { | ||
| var method = GetFooFieldExpression().Compile(); | ||
| return method.Invoke(_instance); | ||
| } | ||
|
|
||
| private static Func<Foo, string> GetFooFieldFunc = GetFooFieldExpression().Compile(); | ||
|
|
||
| static Expression<Func<Foo, string>> GetFooFieldExpression() | ||
| { | ||
| var param_p1 = Expression.Parameter(typeof(Foo)); | ||
| var body = Expression.Field(param_p1, _privateField); | ||
| return Expression.Lambda<Func<Foo, string>>(Expression.Convert(body, typeof(string)), param_p1); | ||
| } | ||
| } |
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,13 @@ | ||
| // 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 | ||
|
|
||
| namespace UnitTest.Benchmarks; | ||
|
|
||
| public class Foo | ||
| { | ||
| private string _name = "test"; | ||
|
|
||
| public string GetName() => _name; | ||
| } |
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,11 @@ | ||
| // 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 BenchmarkDotNet.Running; | ||
| using UnitTest.Benchmarks; | ||
|
|
||
| BenchmarkRunner.Run<Benchmarks>(); | ||
|
|
||
| Console.ReadKey(); |
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,14 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="BenchmarkDotNet" Version="0.14.0" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
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.