Skip to content

Commit 2145ff0

Browse files
feat(CacheManager): add SetSlidingExpirationByType extension method (#5144)
* refactor: 复用 LoadModule 方法 * refactor: 增加日志输出 * refactor: 精简代码提高可读性 * doc: 移除 UseResponseCompression 中间件 * doc: 移除注释信息 * refactor: 增加 offset 参数 * refactor: 重构 SetSlidingExpirationByType 扩展方法 Co-Authored-By: Alex chow <zhouchuanglin@gmail.com> Co-Authored-By: Argo Zhang <argo@live.ca>
1 parent 84ace2e commit 2145ff0

2 files changed

Lines changed: 22 additions & 40 deletions

File tree

src/BootstrapBlazor/Extensions/ICacheEntryExtensions.cs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,17 @@ namespace BootstrapBlazor.Components;
1010
/// <summary>
1111
/// ICacheEntry 扩展类
1212
/// </summary>
13-
[ExcludeFromCodeCoverage]
14-
internal static class ICacheEntryExtensions
13+
public static class ICacheEntryExtensions
1514
{
16-
/// <summary>
17-
/// 设置滑动过期时间
18-
/// </summary>
19-
/// <param name="entry"></param>
20-
/// <param name="offset">默认 null 内部设置为 10 秒</param>
21-
/// <returns></returns>
22-
public static ICacheEntry SetSlidingExpirationForDynamicAssembly(this ICacheEntry entry, TimeSpan? offset = null)
23-
{
24-
entry.SlidingExpiration = offset ?? TimeSpan.FromSeconds(10);
25-
return entry;
26-
}
27-
2815
/// <summary>
2916
/// 设置 动态程序集滑动过期时间 10 秒
3017
/// </summary>
3118
/// <param name="entry"></param>
3219
/// <param name="type"></param>
33-
public static void SetDynamicAssemblyPolicy(this ICacheEntry entry, Type? type)
20+
/// <param name="offset">默认 null 内部设置为 10 秒</param>
21+
public static void SetSlidingExpirationByType(this ICacheEntry entry, Type type, TimeSpan? offset = null)
3422
{
35-
if (type?.Assembly.IsDynamic ?? false)
36-
{
37-
entry.SetSlidingExpiration(TimeSpan.FromSeconds(10));
38-
}
23+
offset ??= type.Assembly.IsDynamic ? TimeSpan.FromSeconds(10) : TimeSpan.FromMinutes(5);
24+
entry.SlidingExpiration = offset.Value;
3925
}
4026
}

src/BootstrapBlazor/Services/CacheManager.cs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public static int ElementCount(object? value)
149149
var cacheKey = $"Lambda-Count-{type.GetUniqueTypeName()}";
150150
var invoker = Instance.GetOrCreate(cacheKey, entry =>
151151
{
152-
entry.SetDynamicAssemblyPolicy(type);
152+
entry.SetSlidingExpirationByType(type);
153153
return LambdaExtensions.CountLambda(type).Compile();
154154
});
155155
ret = invoker(value);
@@ -271,10 +271,6 @@ private static JsonLocalizationOptions GetJsonLocalizationOption()
271271
/// <param name="includeParentCultures"></param>
272272
/// <returns></returns>
273273
public static IEnumerable<LocalizedString> GetTypeStringsFromResolve(string typeName, bool includeParentCultures = true) => Instance.Provider.GetRequiredService<ILocalizationResolve>().GetAllStringsByType(typeName, includeParentCultures);
274-
275-
/// <summary>
276-
/// </summary>
277-
/// <returns></returns>
278274
#endregion
279275

280276
#region DisplayName
@@ -310,7 +306,7 @@ public static string GetDisplayName(Type modelType, string fieldName)
310306
dn = FindDisplayAttribute(propertyInfo);
311307
}
312308

313-
entry.SetDynamicAssemblyPolicy(modelType);
309+
entry.SetSlidingExpirationByType(modelType);
314310

315311
return dn;
316312
});
@@ -427,7 +423,7 @@ string FindDisplayTextByItemName(string itemName)
427423
dn = propertyInfo.GetCustomAttribute<RangeAttribute>(true);
428424
}
429425

430-
entry.SetDynamicAssemblyPolicy(modelType);
426+
entry.SetSlidingExpirationByType(modelType);
431427
return dn;
432428
});
433429
}
@@ -458,7 +454,7 @@ string FindDisplayTextByItemName(string itemName)
458454
}
459455
}
460456

461-
entry.SetDynamicAssemblyPolicy(modelType);
457+
entry.SetSlidingExpirationByType(modelType);
462458
}
463459
return ret;
464460
});
@@ -482,7 +478,7 @@ public static bool TryGetProperty(Type modelType, string fieldName, [NotNullWhen
482478

483479
var pi = props.FirstOrDefault(p => p.Name == fieldName);
484480

485-
entry.SetDynamicAssemblyPolicy(modelType);
481+
entry.SetSlidingExpirationByType(modelType);
486482

487483
return pi;
488484
});
@@ -506,7 +502,7 @@ TResult GetValue()
506502
var cacheKey = ($"Lambda-Get-{type.GetUniqueTypeName()}", typeof(TModel), fieldName, typeof(TResult));
507503
var invoker = Instance.GetOrCreate(cacheKey, entry =>
508504
{
509-
entry.SetDynamicAssemblyPolicy(type);
505+
entry.SetSlidingExpirationByType(type);
510506
return LambdaExtensions.GetPropertyValueLambda<TModel, TResult>(model, fieldName).Compile();
511507
})!;
512508
return invoker(model);
@@ -535,7 +531,7 @@ void SetValue()
535531
var cacheKey = ($"Lambda-Set-{type.GetUniqueTypeName()}", typeof(TModel), fieldName, typeof(TValue));
536532
var invoker = Instance.GetOrCreate(cacheKey, entry =>
537533
{
538-
entry.SetDynamicAssemblyPolicy(type);
534+
entry.SetSlidingExpirationByType(type);
539535
return LambdaExtensions.SetPropertyValueLambda<TModel, TValue>(model, fieldName).Compile();
540536
})!;
541537
invoker(model, value);
@@ -559,7 +555,7 @@ void SetValue()
559555
var cacheKey = ($"Lambda-GetKeyValue-{type.GetUniqueTypeName()}-{customAttribute?.GetUniqueTypeName()}", typeof(TModel));
560556
var invoker = Instance.GetOrCreate(cacheKey, entry =>
561557
{
562-
entry.SetDynamicAssemblyPolicy(type);
558+
entry.SetSlidingExpirationByType(type);
563559

564560
return LambdaExtensions.GetKeyValue<TModel, TValue>(customAttribute).Compile();
565561
})!;
@@ -575,7 +571,7 @@ public static Func<IEnumerable<T>, string, SortOrder, IEnumerable<T>> GetSortFun
575571
var cacheKey = $"Lambda-{nameof(LambdaExtensions.GetSortLambda)}-{typeof(T).GetUniqueTypeName()}";
576572
return Instance.GetOrCreate(cacheKey, entry =>
577573
{
578-
entry.SetDynamicAssemblyPolicy(typeof(T));
574+
entry.SetSlidingExpirationByType(typeof(T));
579575
return LambdaExtensions.GetSortLambda<T>().Compile();
580576
})!;
581577
}
@@ -585,7 +581,7 @@ public static Func<IEnumerable<T>, List<string>, IEnumerable<T>> GetSortListFunc
585581
var cacheKey = $"Lambda-{nameof(LambdaExtensions.GetSortListLambda)}-{typeof(T).GetUniqueTypeName()}";
586582
return Instance.GetOrCreate(cacheKey, entry =>
587583
{
588-
entry.SetDynamicAssemblyPolicy(typeof(T));
584+
entry.SetSlidingExpirationByType(typeof(T));
589585
return LambdaExtensions.GetSortListLambda<T>().Compile();
590586
})!;
591587
}
@@ -605,7 +601,7 @@ public static Func<IEnumerable<T>, List<string>, IEnumerable<T>> GetSortListFunc
605601
var convert = Expression.Convert(para_exp, typeof(List<>).MakeGenericType(type));
606602
var body = Expression.Call(method, convert);
607603

608-
entry.SetDynamicAssemblyPolicy(type);
604+
entry.SetSlidingExpirationByType(type);
609605
return Expression.Lambda<Func<object, IEnumerable<string?>>>(body, para_exp).Compile();
610606
})!;
611607
}
@@ -627,7 +623,7 @@ public static Func<IEnumerable<T>, List<string>, IEnumerable<T>> GetSortListFunc
627623
var cacheKey = $"Lambda-{nameof(GetOnValueChangedInvoke)}-{typeof(TModel).GetUniqueTypeName()}-{fieldType.GetUniqueTypeName()}";
628624
return Instance.GetOrCreate(cacheKey, entry =>
629625
{
630-
entry.SetDynamicAssemblyPolicy(fieldType);
626+
entry.SetSlidingExpirationByType(fieldType);
631627
return Utility.CreateOnValueChanged<TModel>(fieldType).Compile();
632628
})!;
633629
}
@@ -639,7 +635,7 @@ public static Func<IEnumerable<T>, List<string>, IEnumerable<T>> GetSortListFunc
639635
var cacheKey = $"{nameof(GetFormatInvoker)}-{type.GetUniqueTypeName()}";
640636
return Instance.GetOrCreate(cacheKey, entry =>
641637
{
642-
entry.SetDynamicAssemblyPolicy(type);
638+
entry.SetSlidingExpirationByType(type);
643639
return GetFormatLambda(type).Compile();
644640
})!;
645641

@@ -681,7 +677,7 @@ public static Func<IEnumerable<T>, List<string>, IEnumerable<T>> GetSortListFunc
681677
var cacheKey = $"{nameof(GetFormatProviderInvoker)}-{type.GetUniqueTypeName()}";
682678
return Instance.GetOrCreate(cacheKey, entry =>
683679
{
684-
entry.SetDynamicAssemblyPolicy(type);
680+
entry.SetSlidingExpirationByType(type);
685681
return GetFormatProviderLambda(type).Compile();
686682
})!;
687683

@@ -712,7 +708,7 @@ public static object GetFormatterInvoker(Type type, Func<object, Task<string?>>
712708
var cacheKey = $"{nameof(GetFormatterInvoker)}-{type.GetUniqueTypeName()}";
713709
var invoker = Instance.GetOrCreate(cacheKey, entry =>
714710
{
715-
entry.SetDynamicAssemblyPolicy(type);
711+
entry.SetSlidingExpirationByType(type);
716712
return GetFormatterInvokerLambda(type).Compile();
717713
});
718714
return invoker(formatter);
@@ -741,7 +737,7 @@ public static List<PropertyInfo> GetRuntimeProperties(Type type)
741737
var cacheKey = $"{nameof(GetRuntimeProperties)}-{type.GetUniqueTypeName()}";
742738
return Instance.GetOrCreate(cacheKey, entry =>
743739
{
744-
entry.SetDynamicAssemblyPolicy(type);
740+
entry.SetSlidingExpirationByType(type);
745741
return type.GetRuntimeProperties().ToList();
746742
});
747743
}
@@ -756,7 +752,7 @@ public static List<FieldInfo> GetRuntimeFields(Type type)
756752
var cacheKey = $"{nameof(GetRuntimeFields)}-{type.GetUniqueTypeName()}";
757753
return Instance.GetOrCreate(cacheKey, entry =>
758754
{
759-
entry.SetDynamicAssemblyPolicy(type);
755+
entry.SetSlidingExpirationByType(type);
760756
return type.GetRuntimeFields().ToList();
761757
})!;
762758
}

0 commit comments

Comments
 (0)