可以解决,方法比较简单
var notCacheKeysPermissionNames = notCacheKeys.Select(t=>t.Split(',').Last().Substring(2)).ToList();
没有考虑其他特殊情况,但是很快
根据缓存权限格式
private const string CacheKeyFormat = "pn:{0},pk:{1},n:{2}";
耗时从4989ms到96ms
GetPermissionNameFormCacheKeyOrNull 这个函数的找个方法,需要特殊处理什么字符串么,有些函数没有使用过
var result = FormattedStringValueExtracter.Extract(cacheKey, CacheKeyFormat, true);
FormattedStringValueExtracter这个类。
`
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace Volo.Abp.Text.Formatting;
///
if (str == format)
{
return new ExtractionResult(true);
}
var formatTokens = new FormatStringTokenizer().Tokenize(format);
if (formatTokens.IsNullOrEmpty())
{
return new ExtractionResult(str == "");
}
var result = new ExtractionResult(true);
for (var i = 0; i < formatTokens.Count; i++)
{
var currentToken = formatTokens[i];
var previousToken = i > 0 ? formatTokens[i - 1] : null;
if (currentToken.Type == FormatStringTokenType.ConstantText)
{
if (i == 0)
{
if (!str.StartsWith(currentToken.Text, stringComparison))
{
result.IsMatch = false;
return result;
}
str = str.Substring(currentToken.Text.Length);
}
else
{
var matchIndex = str.IndexOf(currentToken.Text, stringComparison);
if (matchIndex < 0)
{
result.IsMatch = false;
return result;
}
Debug.Assert(previousToken != null, "previousToken can not be null since i > 0 here");
result.Matches.Add(new NameValue(previousToken.Text, str.Substring(0, matchIndex)));
str = str.Substring(matchIndex + currentToken.Text.Length);
}
}
}
var lastToken = formatTokens.Last();
if (lastToken.Type == FormatStringTokenType.DynamicValue)
{
result.Matches.Add(new NameValue(lastToken.Text, str));
}
return result;
}
/// <summary>
/// Checks if given <paramref name="str"/> fits to given <paramref name="format"/>.
/// Also gets extracted values.
/// </summary>
/// <param name="str">String including dynamic values</param>
/// <param name="format">Format of the string</param>
/// <param name="values">Array of extracted values if matched</param>
/// <param name="ignoreCase">True, to search case-insensitive</param>
/// <returns>True, if matched.</returns>
public static bool IsMatch(string str, string format, out string[] values, bool ignoreCase = false)
{
var result = Extract(str, format, ignoreCase);
if (!result.IsMatch)
{
values = new string[0];
return false;
}
values = result.Matches.Select(m => m.Value).ToArray();
return true;
}
/// <summary>
/// Used as return value of <see cref="Extract"/> method.
/// </summary>
public class ExtractionResult
{
/// <summary>
/// Is fully matched.
/// </summary>
public bool IsMatch { get; set; }
/// <summary>
/// List of matched dynamic values.
/// </summary>
public List<NameValue> Matches { get; private set; }
internal ExtractionResult(bool isMatch)
{
IsMatch = isMatch;
Matches = new List<NameValue>();
}
}
}
`
PermissionStore.GetPermissionNameFormCacheKeyOrNull 存在性能问题
跟查询数据库没关系了,主要是
pn:R,pk:admin,n:AbpIdentity.Roles ,转Name 匹配
var permissions = dbpermissions.Where(x => notCacheKeys.Any(k => GetPermissionNameFormCacheKeyOrNull(k) == x.Name)).ToList();
也就是
没修改源码之前的
var permissions = (await PermissionDefinitionManager.GetPermissionsAsync()) .Where(x => notCacheKeys.Any(k => GetPermissionNameFormCacheKeyOrNull(k) == x.Name)).ToList();
权限notCacheKeys,反向匹配权限Name,性能问题
.har文件发你邮箱不,7.2.2 后权限修改代码,copy 会722 重写编译,替换722的权限模块代码可以不
接近4000个权限,50个角色,500用户,每次登录,PermissionGrants Cache,客户端4000,角色4000,用户4000,然后去重。第一登录慢,出现白屏,第二次快点,手动删除缓存,又变慢