是否有可能来检查在C#中缓存的正则表达式的数量? [英] Is it possible to check the number of cached regex in C#?

查看:222
本文介绍了是否有可能来检查在C#中缓存的正则表达式的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Regex.CacheSize物业
获取或设置条目的编辑>正则表达式的当前静态缓存的最大数量。

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.

默认情况下,高速缓存保存15编译静态正则表达式。您的应用程序>通常没有修改高速缓存的大小。使用CacheSize属性只>当你想关闭缓存或者当你有一个非常大的缓存。

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.

所以我想有洞察在缓存表达式的当前数目。任何人都知道,如果/如何这是可能的。

So I'd like to have insight into the current number of expressions in the cache. Anyone know if/how that is possible?

想法是,我重用<?;其中15现在这么不想乱动的的CacheSize ,但希望能够在某些时候检查实际缓存使用率要么登录,如果我打最大(为正则表达式使用扩展)或动态调整的CacheSize

Idea being that I reuse < 15 of them now so don't want to fiddle with the CacheSize, but would like to be able to check the actual cache usage at some point to either log if I am hitting the max (as regex usage expands) or dynamically adjust CacheSize.

此外,任何意见作为开销简单地增加的CacheSize 来一些任意大多少​​?

Alternatively, any comments as to the overhead of simply increasing the CacheSize to some arbitrarily large number?

感谢。

推荐答案

mscorlib程序 4.0)反编译显示,缓存是一个内部 CachedCodeEntry ,所以你不会得到它没有反映。

Decompilation (of mscorlib 4.0) reveals that the cache is an internal linked list of CachedCodeEntry, so you're not going to get at it without reflection.

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

The overheads of increasing the maximum cache size would be:


  1. 存储缓存条目的存储成本;最大的使用仅仅是在这样的逻辑上的正则表达式创建:


  • 我们有缓存,有什么看法?

    • 如果是这样,缓存此正则表达式

    • 的我们现在已经超过了最大缓存大小?

      • 如果是这样,删除最后一个缓存项



      2.遍历缓存寻找成本增加匹配


      2. the increased cost to traverse the cache looking for a match

      只要你的数字是不是荒谬的,你应该确定起动不起来。

      So long as your numbers aren't absurd, you should be OK cranking it up.

      这里的反射代码你需要检索当前缓存大小:

      Here's the reflection code you'd need to retrieve the current cache size:

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

      我们用强制转换为的ICollection 以避免转换为泛型列表上的内部类型的并发症。

      We use the cast to ICollection to avoid the complication of having to cast to a generic list on an internal type.

      这篇关于是否有可能来检查在C#中缓存的正则表达式的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆