2 回答

TA貢獻1900條經驗 獲得超5個贊
我會嘗試創建一個實際Regex變量并重用它。這應該有助于加快速度。我也可能會建議將三元業務更改為常規的 if/else if/else 語句。我認為它會更具可讀性(只是個人意見)。
string requestedPath = HttpUtility.UrlDecode(this.StripLanguage(currentContext.InputUrl.AbsolutePath));
string requestedPathAndQuery = HttpUtility.UrlDecode(currentContext.InputUrl.PathAndQuery);
string requestedRawUrl = HttpUtility.UrlDecode(currentContext.InputUrl.PathAndQuery);
string requestedUrl =
HttpUtility.UrlDecode(
string.Concat(
currentContext.InputUrl.Scheme,
"://",
currentContext.InputUrl.Host,
requestedRawUrl));
string requestedRawUrlDomainAppended = HttpUtility.UrlDecode(currentContext.InputUrl.AbsoluteUri);
string requestedPathWithCulture = HttpUtility.UrlDecode(currentContext.InputUrl.AbsolutePath);
var regex = new Regex(matchPattern.Trim(), RegexOptions.IgnoreCase);
var finalRequestedURL = regex.IsMatch(requestedPathAndQuery)
? requestedPathAndQuery
: regex.IsMatch(requestedPath)
? requestedPath
: regex.IsMatch(requestedPathWithCulture)
? requestedPathWithCulture
: regex.IsMatch(requestedRawUrl)
? requestedRawUrl
: regex.IsMatch(requestedUrl)
? requestedRawUrlDomainAppended
: string.Empty;
編輯
正如我在上面的評論中指出的那樣,有兩個相同的字符串,如果您刪除其中一個,可以節省比較。
string requestedPath = HttpUtility.UrlDecode(this.StripLanguage(currentContext.InputUrl.AbsolutePath));
string requestedPathAndQuery = HttpUtility.UrlDecode(currentContext.InputUrl.PathAndQuery);
// This string is identical to requestPathAndQuery, so I am removing it
// string requestedRawUrl = HttpUtility.UrlDecode(currentContext.InputUrl.PathAndQuery);
string requestedUrl =
HttpUtility.UrlDecode(
string.Concat(
currentContext.InputUrl.Scheme,
"://",
currentContext.InputUrl.Host,
requestedRawUrl));
string requestedRawUrlDomainAppended = HttpUtility.UrlDecode(currentContext.InputUrl.AbsoluteUri);
string requestedPathWithCulture = HttpUtility.UrlDecode(currentContext.InputUrl.AbsolutePath);
var regex = new Regex(matchPattern.Trim(), RegexOptions.IgnoreCase);
var finalRequestedURL = string.Empty;
// You could even add in brackets here to aid readability but this
// helps remove the indententation/nesting that makes the code harder
// to read and follow
if (regex.IsMatch(requestedPathAndQuery)) finalRequestURL = requestedPathAndQuery;
else if(regex.IsMatch(requestedPath)) finalRequestURL = requestedPath;
else if (regex.IsMatch(requestedPathWithCulture)) finalRequestURL = requestedPathWithCulture;
else if (regex.IsMatch(requestedUrl)) finalRequestURL = requestedRawUrlDomainAppended;

TA貢獻2011條經驗 獲得超2個贊
正如我在評論中所說,如果您預計只有有限數量的不同模式可以在您的應用程序的生命周期中重用,您可以創建一個靜態Dictionary(我認為最好使用并發模式)并緩存此正則表達式并重用它們。
編輯
示例代碼:
public class MyHandler
{
private static ConcurrentDictionary<string, Regex> dict = new ConcurrentDictionary<string, Regex>();
public void Handle(string urlPattern)
{
urlPattern = urlPattern.Trim();
var regex = dict.GetOrAdd(urlPattern, s => new Regex(urlPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase));
// use regex
}
}
還要測試RegexOptions.Compiled選項是否適合您,因為它實際上會使事情變慢
- 2 回答
- 0 關注
- 268 瀏覽
添加回答
舉報