feat(ICacheManager): add TryGetCacheEntry method (#5216)

* doc: 增加到期时间列

* test: 增加 benmark 测试功能

* feat: 增加 TryGetCacheEntry 方法

* doc: 增加过期时间

* test: 更新单元测试

* test: 更新单元测试
This commit is contained in:
Argo Zhang
2025-01-26 11:49:08 +08:00
committed by GitHub
parent 01b3e1d17a
commit 895170dc36
15 changed files with 291 additions and 1 deletions

View File

@@ -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(argo@live.ca) 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);
}
}

13
tools/Benchmarks/Foo.cs Normal file
View File

@@ -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(argo@live.ca) Website: https://www.blazor.zone
namespace UnitTest.Benchmarks;
public class Foo
{
private string _name = "test";
public string GetName() => _name;
}

View File

@@ -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(argo@live.ca) Website: https://www.blazor.zone
using BenchmarkDotNet.Running;
using UnitTest.Benchmarks;
BenchmarkRunner.Run<Benchmarks>();
Console.ReadKey();

View File

@@ -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>