亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 IsMatch 時的 REGEX 性能問題

使用 IsMatch 時的 REGEX 性能問題

C#
慕尼黑5688855 2022-01-16 14:43:12
我有以下用于執行重定向的正則表達式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 finalRequestedURL = string.Empty;finalRequestedURL = Regex.IsMatch(requestedPathAndQuery,matchPattern.Trim(),RegexOptions.IgnoreCase)                    ? requestedPathAndQuery                    : Regex.IsMatch(requestedPath,matchPattern.Trim(),RegexOptions.IgnoreCase)                        ? requestedPath                        : Regex.IsMatch(requestedPathWithCulture,matchPattern.Trim(),RegexOptions.IgnoreCase)                            ? requestedPathWithCulture                            : Regex.IsMatch(requestedRawUrl,matchPattern.Trim(),RegexOptions.IgnoreCase)                                ? requestedRawUrl                                : Regex.IsMatch(requestedUrl,matchPattern.Trim(),RegexOptions.IgnoreCase)                                    ? requestedRawUrlDomainAppended                                    : string.Empty;matchPattern變量是 URL 。示例:(.*)/articles/my-article(.*)應重定向到http://www.google.com正則表達式工作正常,但是當涉及到大量請求時,我們的 CPU 會達到 100%。有什么解決方案可以優化上述內容嗎?
查看完整描述

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;


查看完整回答
反對 回復 2022-01-16
?
森林海

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選項是否適合您,因為它實際上會使事情變慢


查看完整回答
反對 回復 2022-01-16
  • 2 回答
  • 0 關注
  • 268 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號