diff --git a/src/BootstrapBlazor.Server/Services/DemoLookupService.cs b/src/BootstrapBlazor.Server/Services/DemoLookupService.cs index f75e384897b..1e50be4fe54 100644 --- a/src/BootstrapBlazor.Server/Services/DemoLookupService.cs +++ b/src/BootstrapBlazor.Server/Services/DemoLookupService.cs @@ -3,6 +3,8 @@ // See the LICENSE file in the project root for more information. // Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone +using System.Collections.Concurrent; + namespace Microsoft.Extensions.DependencyInjection; /// @@ -10,19 +12,46 @@ namespace Microsoft.Extensions.DependencyInjection; /// internal class DemoLookupService(IServiceProvider provider) : LookupServiceBase { - private IServiceProvider Provider { get; } = provider; + /// + /// + /// + /// + /// + /// + public override IEnumerable? GetItemsByKey(string? key, object? data) => null; + + private static readonly ConcurrentDictionary> _cache = []; - public override IEnumerable? GetItemsByKey(string? key, object? data) + /// + /// + /// + /// + /// + /// + public override async Task?> GetItemsByKeyAsync(string? key, object? data) { IEnumerable? items = null; if (key == "Foo.Complete") { - var localizer = Provider.GetRequiredService>(); - items = new List() + // 使用缓存技术防止多次调用提高应用性能 + if (_cache.TryGetValue(key, out var value)) { - new() { Value = "True", Text = localizer["True"].Value }, - new() { Value = "False", Text = localizer["False"].Value } - }; + items = value; + } + else + { + // 模拟异步延时实战中大概率从数据库中获得数据 + await Task.Delay(1); + + var localizer = provider.GetRequiredService>(); + var v = new List() + { + new() { Value = "True", Text = localizer["True"].Value }, + new() { Value = "False", Text = localizer["False"].Value } + }; + _cache.TryAdd(key, v); + items = v; + } } return items; }