44// Maintainer: Argo Zhang([email protected] ) Website: https://www.blazor.zone 55
66using Microsoft . Extensions . Options ;
7- using System . Collections . Frozen ;
7+ using System . Globalization ;
88
99namespace BootstrapBlazor . Server . Services ;
1010
11- class CodeSnippetService
11+ /// <summary>
12+ /// 构造方法
13+ /// </summary>
14+ /// <param name="factory"></param>
15+ /// <param name="cacheManager"></param>
16+ /// <param name="options"></param>
17+ /// <param name="localizerOptions"></param>
18+ class CodeSnippetService (
19+ IHttpClientFactory factory ,
20+ ICacheManager cacheManager ,
21+ IOptions < WebsiteOptions > options ,
22+ IOptions < JsonLocalizationOptions > localizerOptions )
1223{
13- private IHttpClientFactory Factory { get ; set ; }
14-
15- private string ServerUrl { get ; set ; }
16-
17- private string SourceCodePath { get ; set ; }
18-
19- private FrozenDictionary < string , string ? > SourceCodes { get ; set ; }
20-
21- private bool IsDevelopment { get ; }
22-
23- private string ContentRootPath { get ; }
24-
25- private ICacheManager CacheManager { get ; set ; }
26-
27- private JsonLocalizationOptions LocalizerOptions { get ; }
28-
29- /// <summary>
30- /// 构造方法
31- /// </summary>
32- /// <param name="factory"></param>
33- /// <param name="cacheManager"></param>
34- /// <param name="options"></param>
35- /// <param name="localizerOptions"></param>
36- public CodeSnippetService (
37- IHttpClientFactory factory ,
38- ICacheManager cacheManager ,
39- IOptionsMonitor < WebsiteOptions > options ,
40- IOptionsMonitor < JsonLocalizationOptions > localizerOptions )
41- {
42- LocalizerOptions = localizerOptions . CurrentValue ;
43-
44- CacheManager = cacheManager ;
45- Factory = factory ;
46-
47- IsDevelopment = options . CurrentValue . IsDevelopment ;
48- ContentRootPath = options . CurrentValue . ContentRootPath ;
49- ServerUrl = options . CurrentValue . ServerUrl ;
50- SourceCodes = options . CurrentValue . SourceCodes ;
51- SourceCodePath = options . CurrentValue . SourceCodePath ;
52- }
53-
5424 /// <summary>
5525 /// 获得示例源码方法
5626 /// </summary>
@@ -63,14 +33,16 @@ public async Task<string> GetCodeAsync(string codeFile)
6333 // codeFile = ajax.razor.cs
6434 var segs = codeFile . Split ( '.' ) ;
6535 var key = segs [ 0 ] ;
66- var typeName = SourceCodes . TryGetValue ( key . ToLowerInvariant ( ) , out var value ) ? value : string . Empty ;
36+ var typeName = options . Value . SourceCodes . TryGetValue ( key . ToLowerInvariant ( ) , out var value ) ? value : string . Empty ;
6737 if ( ! string . IsNullOrEmpty ( typeName ) )
6838 {
6939 var fileName = codeFile . Replace ( key , typeName ) ;
7040 content = await GetFileContentAsync ( fileName ) ;
7141
7242 // 源码修正
73- CacheManager . GetLocalizedStrings ( typeName , LocalizerOptions ) . ToList ( ) . ForEach ( l => content = ReplacePayload ( content , l ) ) ;
43+ var type = typeName . Replace ( '\\ ' , '.' ) ;
44+ Utility . GetJsonStringByTypeName ( localizerOptions . Value , typeof ( CodeSnippetService ) . Assembly , $ "BootstrapBlazor.Server.Components.Samples.{ type } ") . ToList ( )
45+ . ForEach ( l => content = ReplacePayload ( content , l ) ) ;
7446 content = ReplaceSymbols ( content ) ;
7547 content = RemoveBlockStatement ( content , "@inject IStringLocalizer<" ) ;
7648 }
@@ -93,25 +65,30 @@ public async Task<string> GetFileContentAsync(string fileName)
9365 string ? payload ;
9466 if ( OperatingSystem . IsBrowser ( ) )
9567 {
96- var client = Factory . CreateClient ( ) ;
68+ var client = factory . CreateClient ( ) ;
9769 client . Timeout = TimeSpan . FromSeconds ( 5 ) ;
98- client . BaseAddress = new Uri ( $ "{ ServerUrl } /api/") ;
70+ client . BaseAddress = new Uri ( $ "{ options . Value . ServerUrl } /api/") ;
9971 payload = await client . GetStringAsync ( $ "Code?fileName=BootstrapBlazor.Server/Components/Samples/{ fileName } ") ;
10072 }
10173 else
10274 {
10375 // 读取硬盘文件
104- payload = await CacheManager . GetContentFromFileAsync ( fileName , _ => ReadFileAsync ( fileName ) ) ;
76+ var key = $ "{ nameof ( GetFileContentAsync ) } -{ fileName } -{ CultureInfo . CurrentUICulture . Name } ";
77+ payload = await cacheManager . GetOrCreateAsync ( key , entry =>
78+ {
79+ entry . SlidingExpiration = TimeSpan . FromMinutes ( 10 ) ;
80+ return ReadFileAsync ( fileName ) ;
81+ } ) ;
10582 }
10683 return payload ;
10784 }
10885
10986 private async Task < string > ReadFileAsync ( string fileName )
11087 {
11188 string ? payload ;
112- var file = IsDevelopment
113- ? $ "{ ContentRootPath } \\ ..\\ BootstrapBlazor.Server\\ Components\\ Samples\\ { fileName } "
114- : $ "{ SourceCodePath } BootstrapBlazor.Server\\ Components\\ Samples\\ { fileName } ";
89+ var file = options . Value . IsDevelopment
90+ ? $ "{ options . Value . ContentRootPath } \\ ..\\ BootstrapBlazor.Server\\ Components\\ Samples\\ { fileName } "
91+ : $ "{ options . Value . SourceCodePath } BootstrapBlazor.Server\\ Components\\ Samples\\ { fileName } ";
11592 if ( ! OperatingSystem . IsWindows ( ) )
11693 {
11794 file = file . Replace ( '\\ ' , '/' ) ;
0 commit comments