@@ -29,6 +29,8 @@ internal class CacheManager : ICacheManager
29
29
[ NotNull ]
30
30
private static CacheManager ? Instance { get ; set ; }
31
31
32
+ private const string CacheKeyPrefix = "BootstrapBlazor" ;
33
+
32
34
/// <summary>
33
35
/// 构造函数
34
36
/// </summary>
@@ -60,11 +62,13 @@ public TItem GetOrCreate<TItem>(object key, Func<ICacheEntry, TItem> factory) =>
60
62
/// </summary>
61
63
public Task < TItem > GetOrCreateAsync < TItem > ( object key , Func < ICacheEntry , Task < TItem > > factory ) => Cache . GetOrCreateAsync ( key , async entry =>
62
64
{
63
- if ( key is not string )
65
+ var item = await factory ( entry ) ;
66
+
67
+ if ( entry . SlidingExpiration == null && entry . AbsoluteExpiration == null && entry . Priority != CacheItemPriority . NeverRemove )
64
68
{
65
69
entry . SetSlidingExpiration ( TimeSpan . FromMinutes ( 5 ) ) ;
66
70
}
67
- return await factory ( entry ) ;
71
+ return item ;
68
72
} ) ! ;
69
73
70
74
/// <summary>
@@ -177,7 +181,7 @@ public static int ElementCount(object? value)
177
181
if ( value != null )
178
182
{
179
183
var type = value . GetType ( ) ;
180
- var cacheKey = $ "Lambda-Count-{ type . GetUniqueTypeName ( ) } ";
184
+ var cacheKey = $ "{ CacheKeyPrefix } - Lambda-Count-{ type . GetUniqueTypeName ( ) } ";
181
185
var invoker = Instance . GetOrCreate ( cacheKey , entry =>
182
186
{
183
187
return LambdaExtensions . CountLambda ( type ) . Compile ( ) ;
@@ -258,15 +262,19 @@ private static JsonLocalizationOptions GetJsonLocalizationOption()
258
262
return null ;
259
263
}
260
264
261
- IEnumerable < LocalizedString > ? localizedItems = null ;
262
265
cultureName ??= CultureInfo . CurrentUICulture . Name ;
263
- var key = $ "{ nameof ( GetJsonStringByTypeName ) } -{ assembly . GetUniqueName ( ) } -{ cultureName } ";
266
+ if ( string . IsNullOrEmpty ( cultureName ) )
267
+ {
268
+ return [ ] ;
269
+ }
270
+
271
+ var key = $ "{ CacheKeyPrefix } -{ nameof ( GetJsonStringByTypeName ) } -{ assembly . GetUniqueName ( ) } -{ cultureName } ";
264
272
if ( forceLoad )
265
273
{
266
274
Instance . Cache . Remove ( key ) ;
267
275
}
268
276
269
- localizedItems = Instance . GetOrCreate ( key , _ =>
277
+ var localizedItems = Instance . GetOrCreate ( key , entry =>
270
278
{
271
279
var sections = option . GetJsonStringFromAssembly ( assembly , cultureName ) ;
272
280
var items = sections . SelectMany ( section => section . GetChildren ( ) . Select ( kv =>
@@ -345,7 +353,7 @@ public static string GetDisplayName(Type modelType, string fieldName)
345
353
346
354
public static List < SelectedItem > GetNullableBoolItems ( Type modelType , string fieldName )
347
355
{
348
- var cacheKey = $ "{ nameof ( GetNullableBoolItems ) } -{ CultureInfo . CurrentUICulture . Name } - { modelType . GetUniqueTypeName ( ) } -{ fieldName } ";
356
+ var cacheKey = $ "{ CacheKeyPrefix } - { nameof ( GetNullableBoolItems ) } -{ modelType . GetUniqueTypeName ( ) } -{ fieldName } - { CultureInfo . CurrentUICulture . Name } ";
349
357
return Instance . GetOrCreate ( cacheKey , entry =>
350
358
{
351
359
var items = new List < SelectedItem > ( ) ;
@@ -475,7 +483,7 @@ public static TResult GetPropertyValue<TModel, TResult>(TModel model, string fie
475
483
TResult GetValue ( )
476
484
{
477
485
var type = model . GetType ( ) ;
478
- var cacheKey = ( $ " Lambda-Get-{ type . GetUniqueTypeName ( ) } " , typeof ( TModel ) , fieldName , typeof ( TResult ) ) ;
486
+ var cacheKey = $ " { CacheKeyPrefix } - Lambda-Get-{ type . GetUniqueTypeName ( ) } - { typeof ( TModel ) } - { fieldName } - { typeof ( TResult ) } " ;
479
487
var invoker = Instance . GetOrCreate ( cacheKey , entry =>
480
488
{
481
489
if ( type . Assembly . IsDynamic )
@@ -503,7 +511,7 @@ public static void SetPropertyValue<TModel, TValue>(TModel model, string fieldNa
503
511
else
504
512
{
505
513
var type = model . GetType ( ) ;
506
- var cacheKey = ( $ " Lambda-Set-{ type . GetUniqueTypeName ( ) } " , typeof ( TModel ) , fieldName , typeof ( TValue ) ) ;
514
+ var cacheKey = $ " { CacheKeyPrefix } - Lambda-Set-{ type . GetUniqueTypeName ( ) } - { typeof ( TModel ) } - { fieldName } - { typeof ( TValue ) } " ;
507
515
var invoker = Instance . GetOrCreate ( cacheKey , entry =>
508
516
{
509
517
if ( type . Assembly . IsDynamic )
@@ -530,7 +538,7 @@ public static void SetPropertyValue<TModel, TValue>(TModel model, string fieldNa
530
538
if ( model != null )
531
539
{
532
540
var type = model . GetType ( ) ;
533
- var cacheKey = ( $ " Lambda-GetKeyValue-{ type . GetUniqueTypeName ( ) } -{ customAttribute ? . GetUniqueTypeName ( ) } ", typeof ( TModel ) ) ;
541
+ var cacheKey = $ " { CacheKeyPrefix } - Lambda-{ nameof ( GetKeyValue ) } -{ type . GetUniqueTypeName ( ) } -{ typeof ( TModel ) } - { customAttribute ? . GetUniqueTypeName ( ) } ";
534
542
var invoker = Instance . GetOrCreate ( cacheKey , entry => LambdaExtensions . GetKeyValue < TModel , TValue > ( customAttribute ) . Compile ( ) ) ;
535
543
ret = invoker ( model ) ;
536
544
}
@@ -541,21 +549,21 @@ public static void SetPropertyValue<TModel, TValue>(TModel model, string fieldNa
541
549
#region Lambda Sort
542
550
public static Func < IEnumerable < T > , string , SortOrder , IEnumerable < T > > GetSortFunc < T > ( )
543
551
{
544
- var cacheKey = $ "Lambda-{ nameof ( LambdaExtensions . GetSortLambda ) } -{ typeof ( T ) . GetUniqueTypeName ( ) } ";
552
+ var cacheKey = $ "{ CacheKeyPrefix } - Lambda-{ nameof ( GetSortFunc ) } -{ typeof ( T ) . GetUniqueTypeName ( ) } ";
545
553
return Instance . GetOrCreate ( cacheKey , entry => LambdaExtensions . GetSortLambda < T > ( ) . Compile ( ) ) ;
546
554
}
547
555
548
556
public static Func < IEnumerable < T > , List < string > , IEnumerable < T > > GetSortListFunc < T > ( )
549
557
{
550
- var cacheKey = $ "Lambda-{ nameof ( LambdaExtensions . GetSortListLambda ) } -{ typeof ( T ) . GetUniqueTypeName ( ) } ";
558
+ var cacheKey = $ "{ CacheKeyPrefix } - Lambda-{ nameof ( GetSortListFunc ) } -{ typeof ( T ) . GetUniqueTypeName ( ) } ";
551
559
return Instance . GetOrCreate ( cacheKey , entry => LambdaExtensions . GetSortListLambda < T > ( ) . Compile ( ) ) ;
552
560
}
553
561
#endregion
554
562
555
563
#region Lambda ConvertTo
556
564
public static Func < object , IEnumerable < string ? > > CreateConverterInvoker ( Type type )
557
565
{
558
- var cacheKey = $ "Lambda-{ nameof ( CreateConverterInvoker ) } -{ type . GetUniqueTypeName ( ) } ";
566
+ var cacheKey = $ "{ CacheKeyPrefix } - Lambda-{ nameof ( CreateConverterInvoker ) } -{ type . GetUniqueTypeName ( ) } ";
559
567
return Instance . GetOrCreate ( cacheKey , entry =>
560
568
{
561
569
var method = typeof ( CacheManager )
@@ -583,15 +591,15 @@ public static Func<IEnumerable<T>, List<string>, IEnumerable<T>> GetSortListFunc
583
591
/// <returns></returns>
584
592
public static Func < TModel , ITableColumn , Func < TModel , ITableColumn , object ? , Task > , object > GetOnValueChangedInvoke < TModel > ( Type fieldType )
585
593
{
586
- var cacheKey = $ "Lambda-{ nameof ( GetOnValueChangedInvoke ) } -{ typeof ( TModel ) . GetUniqueTypeName ( ) } -{ fieldType . GetUniqueTypeName ( ) } ";
594
+ var cacheKey = $ "{ CacheKeyPrefix } - Lambda-{ nameof ( GetOnValueChangedInvoke ) } -{ typeof ( TModel ) . GetUniqueTypeName ( ) } -{ fieldType . GetUniqueTypeName ( ) } ";
587
595
return Instance . GetOrCreate ( cacheKey , entry => Utility . CreateOnValueChanged < TModel > ( fieldType ) . Compile ( ) ) ;
588
596
}
589
597
#endregion
590
598
591
599
#region Format
592
600
public static Func < object , string , IFormatProvider ? , string > GetFormatInvoker ( Type type )
593
601
{
594
- var cacheKey = $ "{ nameof ( GetFormatInvoker ) } -{ type . GetUniqueTypeName ( ) } ";
602
+ var cacheKey = $ "{ CacheKeyPrefix } -Lambda- { nameof ( GetFormatInvoker ) } -{ type . GetUniqueTypeName ( ) } ";
595
603
return Instance . GetOrCreate ( cacheKey , entry => GetFormatLambda ( type ) . Compile ( ) ) ;
596
604
597
605
static Expression < Func < object , string , IFormatProvider ? , string > > GetFormatLambda ( Type type )
@@ -629,7 +637,7 @@ public static Func<IEnumerable<T>, List<string>, IEnumerable<T>> GetSortListFunc
629
637
630
638
public static Func < object , IFormatProvider ? , string > GetFormatProviderInvoker ( Type type )
631
639
{
632
- var cacheKey = $ "{ nameof ( GetFormatProviderInvoker ) } -{ type . GetUniqueTypeName ( ) } ";
640
+ var cacheKey = $ "{ CacheKeyPrefix } -Lambda- { nameof ( GetFormatProviderInvoker ) } -{ type . GetUniqueTypeName ( ) } ";
633
641
return Instance . GetOrCreate ( cacheKey , entry => GetFormatProviderLambda ( type ) . Compile ( ) ) ;
634
642
635
643
static Expression < Func < object , IFormatProvider ? , string > > GetFormatProviderLambda ( Type type )
@@ -656,7 +664,7 @@ public static Func<IEnumerable<T>, List<string>, IEnumerable<T>> GetSortListFunc
656
664
657
665
public static object GetFormatterInvoker ( Type type , Func < object , Task < string ? > > formatter )
658
666
{
659
- var cacheKey = $ "{ nameof ( GetFormatterInvoker ) } -{ type . GetUniqueTypeName ( ) } ";
667
+ var cacheKey = $ "{ CacheKeyPrefix } -Lambda- { nameof ( GetFormatterInvoker ) } -{ type . GetUniqueTypeName ( ) } ";
660
668
var invoker = Instance . GetOrCreate ( cacheKey , entry => GetFormatterInvokerLambda ( type ) . Compile ( ) ) ;
661
669
return invoker ( formatter ) ;
662
670
0 commit comments