加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

可以检查C#中缓存的正则表达式的数量吗?

发布时间:2020-12-15 18:05:23 所属栏目:百科 来源:网络整理
导读:Regex.CacheSize Property Gets or sets the maximum number of entries in the current static cache of compiled regular expressions. The Regex class maintains an internal cache of compiled regular expressions used in static method calls. If th

Regex.CacheSize Property
Gets or sets the maximum number of entries in the current static cache of compiled >regular expressions.

The Regex class maintains an internal cache of compiled regular expressions used in >static method calls. If the value specified in a set operation is less than the current >cache size,cache entries are discarded until the cache size is equal to the specified >value.

By default,the cache holds 15 compiled static regular expressions. Your application >typically will not have to modify the size of the cache. Use the CacheSize property only >when you want to turn off caching or when you have an unusually large cache.

所以我想深入了解缓存中当前的表达式数量.任何人知道/如何/如何可能?

想法是,我重用<现在,其中15个现在不想搞定CacheSize,但是如果我正在触发max(正则表达式使用扩展)或动态调整CacheSize,则希望能够检查某些时刻的实际缓存使用情况. 或者,任何关于简单地将CacheSize增加到某些任意大数的开销的注释? 谢谢.

解决方法

解压缩(mscorlib 4.0)显示缓存是CachedCodeEntry的内部链接列表,所以你不会在没有反射的情况下得到它.

增加最大缓存大小的开销将是:

>存储缓存条目的内存成本;最大值的使用在正则表达式创建时就像这样逻辑:

我们是缓存吗?

>如果是这样,缓存这个正则表达式
>我们现在是否超过最大缓存大小?

>如果是,删除最后一个缓存条目

2.越过缓存查找匹配的成本增加

只要你的数字不荒谬,你应该可以开始吧.

以下是您需要检索当前缓存大小的反射代码:

public static int RegexCacheSize()
    {
        var fi = typeof(Regex).GetField("livecode",BindingFlags.Static 
                                                  | BindingFlags.NonPublic);
        var coll = (ICollection)(fi.GetValue(null));

        return coll.Count;
    }

我们使用cast to ICollection来避免必须将其转换为内部类型的通用列表的复杂性.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读