Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private async Task<IEnumerable<Foo>> OnSearchFoo(string searchText)
}).ToList();
}

private async Task<IEnumerable<string>> OnModelSearch(string v)
private async Task<IEnumerable<string?>> OnModelSearch(string v)
{
// 模拟异步延时
await Task.Delay(100);
Expand Down
42 changes: 25 additions & 17 deletions src/BootstrapBlazor/Services/CacheManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ internal class CacheManager : ICacheManager
[NotNull]
private static CacheManager? Instance { get; set; }

private const string CacheKeyPrefix = "BootstrapBlazor";

/// <summary>
/// 构造函数
/// </summary>
Expand Down Expand Up @@ -60,11 +62,13 @@ public TItem GetOrCreate<TItem>(object key, Func<ICacheEntry, TItem> factory) =>
/// </summary>
public Task<TItem> GetOrCreateAsync<TItem>(object key, Func<ICacheEntry, Task<TItem>> factory) => Cache.GetOrCreateAsync(key, async entry =>
{
if (key is not string)
var item = await factory(entry);

if (entry.SlidingExpiration == null && entry.AbsoluteExpiration == null && entry.Priority != CacheItemPriority.NeverRemove)
{
entry.SetSlidingExpiration(TimeSpan.FromMinutes(5));
}
return await factory(entry);
return item;
})!;

/// <summary>
Expand Down Expand Up @@ -177,7 +181,7 @@ public static int ElementCount(object? value)
if (value != null)
{
var type = value.GetType();
var cacheKey = $"Lambda-Count-{type.GetUniqueTypeName()}";
var cacheKey = $"{CacheKeyPrefix}-Lambda-Count-{type.GetUniqueTypeName()}";
var invoker = Instance.GetOrCreate(cacheKey, entry =>
{
return LambdaExtensions.CountLambda(type).Compile();
Expand Down Expand Up @@ -258,15 +262,19 @@ private static JsonLocalizationOptions GetJsonLocalizationOption()
return null;
}

IEnumerable<LocalizedString>? localizedItems = null;
cultureName ??= CultureInfo.CurrentUICulture.Name;
var key = $"{nameof(GetJsonStringByTypeName)}-{assembly.GetUniqueName()}-{cultureName}";
if (string.IsNullOrEmpty(cultureName))
{
return [];
}

var key = $"{CacheKeyPrefix}-{nameof(GetJsonStringByTypeName)}-{assembly.GetUniqueName()}-{cultureName}";
if (forceLoad)
{
Instance.Cache.Remove(key);
}

localizedItems = Instance.GetOrCreate(key, _ =>
var localizedItems = Instance.GetOrCreate(key, entry =>
{
var sections = option.GetJsonStringFromAssembly(assembly, cultureName);
var items = sections.SelectMany(section => section.GetChildren().Select(kv =>
Expand Down Expand Up @@ -345,7 +353,7 @@ public static string GetDisplayName(Type modelType, string fieldName)

public static List<SelectedItem> GetNullableBoolItems(Type modelType, string fieldName)
{
var cacheKey = $"{nameof(GetNullableBoolItems)}-{CultureInfo.CurrentUICulture.Name}-{modelType.GetUniqueTypeName()}-{fieldName}";
var cacheKey = $"{CacheKeyPrefix}-{nameof(GetNullableBoolItems)}-{modelType.GetUniqueTypeName()}-{fieldName}-{CultureInfo.CurrentUICulture.Name}";
return Instance.GetOrCreate(cacheKey, entry =>
{
var items = new List<SelectedItem>();
Expand Down Expand Up @@ -475,7 +483,7 @@ public static TResult GetPropertyValue<TModel, TResult>(TModel model, string fie
TResult GetValue()
{
var type = model.GetType();
var cacheKey = ($"Lambda-Get-{type.GetUniqueTypeName()}", typeof(TModel), fieldName, typeof(TResult));
var cacheKey = $"{CacheKeyPrefix}-Lambda-Get-{type.GetUniqueTypeName()}-{typeof(TModel)}-{fieldName}-{typeof(TResult)}";
var invoker = Instance.GetOrCreate(cacheKey, entry =>
{
if (type.Assembly.IsDynamic)
Expand Down Expand Up @@ -503,7 +511,7 @@ public static void SetPropertyValue<TModel, TValue>(TModel model, string fieldNa
else
{
var type = model.GetType();
var cacheKey = ($"Lambda-Set-{type.GetUniqueTypeName()}", typeof(TModel), fieldName, typeof(TValue));
var cacheKey = $"{CacheKeyPrefix}-Lambda-Set-{type.GetUniqueTypeName()}-{typeof(TModel)}-{fieldName}-{typeof(TValue)}";
var invoker = Instance.GetOrCreate(cacheKey, entry =>
{
if (type.Assembly.IsDynamic)
Expand All @@ -530,7 +538,7 @@ public static void SetPropertyValue<TModel, TValue>(TModel model, string fieldNa
if (model != null)
{
var type = model.GetType();
var cacheKey = ($"Lambda-GetKeyValue-{type.GetUniqueTypeName()}-{customAttribute?.GetUniqueTypeName()}", typeof(TModel));
var cacheKey = $"{CacheKeyPrefix}-Lambda-{nameof(GetKeyValue)}-{type.GetUniqueTypeName()}-{typeof(TModel)}-{customAttribute?.GetUniqueTypeName()}";
var invoker = Instance.GetOrCreate(cacheKey, entry => LambdaExtensions.GetKeyValue<TModel, TValue>(customAttribute).Compile());
ret = invoker(model);
}
Expand All @@ -541,21 +549,21 @@ public static void SetPropertyValue<TModel, TValue>(TModel model, string fieldNa
#region Lambda Sort
public static Func<IEnumerable<T>, string, SortOrder, IEnumerable<T>> GetSortFunc<T>()
{
var cacheKey = $"Lambda-{nameof(LambdaExtensions.GetSortLambda)}-{typeof(T).GetUniqueTypeName()}";
var cacheKey = $"{CacheKeyPrefix}-Lambda-{nameof(GetSortFunc)}-{typeof(T).GetUniqueTypeName()}";
return Instance.GetOrCreate(cacheKey, entry => LambdaExtensions.GetSortLambda<T>().Compile());
}

public static Func<IEnumerable<T>, List<string>, IEnumerable<T>> GetSortListFunc<T>()
{
var cacheKey = $"Lambda-{nameof(LambdaExtensions.GetSortListLambda)}-{typeof(T).GetUniqueTypeName()}";
var cacheKey = $"{CacheKeyPrefix}-Lambda-{nameof(GetSortListFunc)}-{typeof(T).GetUniqueTypeName()}";
return Instance.GetOrCreate(cacheKey, entry => LambdaExtensions.GetSortListLambda<T>().Compile());
}
#endregion

#region Lambda ConvertTo
public static Func<object, IEnumerable<string?>> CreateConverterInvoker(Type type)
{
var cacheKey = $"Lambda-{nameof(CreateConverterInvoker)}-{type.GetUniqueTypeName()}";
var cacheKey = $"{CacheKeyPrefix}-Lambda-{nameof(CreateConverterInvoker)}-{type.GetUniqueTypeName()}";
return Instance.GetOrCreate(cacheKey, entry =>
{
var method = typeof(CacheManager)
Expand Down Expand Up @@ -583,15 +591,15 @@ public static Func<IEnumerable<T>, List<string>, IEnumerable<T>> GetSortListFunc
/// <returns></returns>
public static Func<TModel, ITableColumn, Func<TModel, ITableColumn, object?, Task>, object> GetOnValueChangedInvoke<TModel>(Type fieldType)
{
var cacheKey = $"Lambda-{nameof(GetOnValueChangedInvoke)}-{typeof(TModel).GetUniqueTypeName()}-{fieldType.GetUniqueTypeName()}";
var cacheKey = $"{CacheKeyPrefix}-Lambda-{nameof(GetOnValueChangedInvoke)}-{typeof(TModel).GetUniqueTypeName()}-{fieldType.GetUniqueTypeName()}";
return Instance.GetOrCreate(cacheKey, entry => Utility.CreateOnValueChanged<TModel>(fieldType).Compile());
}
#endregion

#region Format
public static Func<object, string, IFormatProvider?, string> GetFormatInvoker(Type type)
{
var cacheKey = $"{nameof(GetFormatInvoker)}-{type.GetUniqueTypeName()}";
var cacheKey = $"{CacheKeyPrefix}-Lambda-{nameof(GetFormatInvoker)}-{type.GetUniqueTypeName()}";
return Instance.GetOrCreate(cacheKey, entry => GetFormatLambda(type).Compile());

static Expression<Func<object, string, IFormatProvider?, string>> GetFormatLambda(Type type)
Expand Down Expand Up @@ -629,7 +637,7 @@ public static Func<IEnumerable<T>, List<string>, IEnumerable<T>> GetSortListFunc

public static Func<object, IFormatProvider?, string> GetFormatProviderInvoker(Type type)
{
var cacheKey = $"{nameof(GetFormatProviderInvoker)}-{type.GetUniqueTypeName()}";
var cacheKey = $"{CacheKeyPrefix}-Lambda-{nameof(GetFormatProviderInvoker)}-{type.GetUniqueTypeName()}";
return Instance.GetOrCreate(cacheKey, entry => GetFormatProviderLambda(type).Compile());

static Expression<Func<object, IFormatProvider?, string>> GetFormatProviderLambda(Type type)
Expand All @@ -656,7 +664,7 @@ public static Func<IEnumerable<T>, List<string>, IEnumerable<T>> GetSortListFunc

public static object GetFormatterInvoker(Type type, Func<object, Task<string?>> formatter)
{
var cacheKey = $"{nameof(GetFormatterInvoker)}-{type.GetUniqueTypeName()}";
var cacheKey = $"{CacheKeyPrefix}-Lambda-{nameof(GetFormatterInvoker)}-{type.GetUniqueTypeName()}";
var invoker = Instance.GetOrCreate(cacheKey, entry => GetFormatterInvokerLambda(type).Compile());
return invoker(formatter);

Expand Down
17 changes: 17 additions & 0 deletions test/UnitTest/Services/CacheManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,23 @@ public async Task GetOrCreateAsync_Ok()
actual = await GetOrCreateAsync(key);
Assert.Equal(1, actual);

await Cache.GetOrCreateAsync("test-GetOrCreateAsync", async entry =>
{
await Task.Delay(1);
entry.Priority = CacheItemPriority.NeverRemove;
return "test";
});
Cache.Clear();
Assert.True(Cache.TryGetValue("test-GetOrCreateAsync", out string? v));
Assert.Equal("test", v);

await Cache.GetOrCreateAsync("test", async entry =>
{
await Task.Delay(1);
entry.AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(1);
return "test";
});

Task<int> GetOrCreateAsync(object key) => Cache.GetOrCreateAsync<int>(key, entry =>
{
val++;
Expand Down
7 changes: 5 additions & 2 deletions test/UnitTest/Utils/UtilityTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,11 +372,14 @@ public void GetJsonStringByTypeName_Ok()
{
// improve code coverage
var option = Context.Services.GetRequiredService<IOptions<JsonLocalizationOptions>>().Value;
Utility.GetJsonStringByTypeName(option, this.GetType().Assembly, "UnitTest.Utils.UtilityTest+Cat", null, true);
Assert.Empty(Utility.GetJsonStringByTypeName(option, this.GetType().Assembly, "UnitTest.Utils.UtilityTest+Cat1", null, true));

// dynamic
var dynamicType = EmitHelper.CreateTypeByName("test_type", new InternalTableColumn[] { new("Name", typeof(string)) });
Utility.GetJsonStringByTypeName(option, dynamicType!.Assembly, "Test");
Assert.Empty(Utility.GetJsonStringByTypeName(option, dynamicType!.Assembly, "Test"));

// empty cultureName
Assert.Empty(Utility.GetJsonStringByTypeName(option, this.GetType().Assembly, "UnitTest.Utils.UtilityTest+Cat1", "", false));
}

[Fact]
Expand Down
Loading