var permissions = (await PermissionDefinitionManager.GetPermissionsAsync())
.Where(x => notCacheKeys.Any(k => GetPermissionNameFormCacheKeyOrNull(k) == x.Name)).ToList();
改为
var names = notCacheKeys.Select(k => GetPermissionNameFormCacheKeyOrNull(k)).ToArray();
var permissions = (await PermissionDefinitionManager.GetPermissionsAsync())
.Where(x => names .Any(k => k == x.Name)).ToList();
速度从6秒到几秒?
这几行虽然可以改进, 但是似乎也不会造成问题.
PermissionDefinitionManager不会查询数据库.
你可以分享一个项目复现问题吗?
liming.ma@volosoft.com
谢谢
这是我的单元测试代码, 你可以试试吗?
public class GetPermissionNameFormCacheKeyOrNull_Tests
{
[Fact]
public void Test()
{
var key = PermissionGrantCacheItem.CalculateCacheKey("Permission1", "Role", "1");
key.ShouldBe("pn:Role,pk:1,n:Permission1");
var stopwatch = System.Diagnostics.Stopwatch.StartNew();
for (int i = 0; i < 50000; i++)
{
var name = PermissionGrantCacheItem.GetPermissionNameFormCacheKeyOrNull(key);
name.ShouldBe("Permission1");
}
stopwatch.Stop();
var elapsed = stopwatch.Elapsed.Seconds;
elapsed.ShouldBeLessThan(2);
var name2 = PermissionGrantCacheItem.GetPermissionNameFormCacheKeyOrNull("invalid-key");
name2.ShouldBeNull();
}
}
hi
Can you copy your code to the test project?
I will check and fix it.
Thanks.
ok, no problem
Can you share an access token?
What is the output if you use the access token to request the profile
endpoint?
GET /api/account/my-profile
好的, 我会测试和修复GetPermissionNameFormCacheKeyOrNull
非常感谢
它会尝试匹配字符串格式.
public static string GetPermissionNameFormCacheKeyOrNull(string cacheKey)
{
var result = FormattedStringValueExtracter.Extract(cacheKey, CacheKeyFormat, true);
return result.IsMatch ? result.Matches.Last().Value : null;
}
你修改或者替换后是否解决了问题?
我没有看懂, 你修改后问题解决了?