是否可以检查缓存的正则表达式的数量? [英] Is it possible to check the number of cached regex?

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

问题描述

Regex.CacheSize属性获取或设置当前已编译正则表达式静态缓存中的最大条目数.

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

Regex类维护在>静态方法调用中使用的已编译正则表达式的内部缓存.如果在set操作中指定的值小于当前的>高速缓存大小,则将丢弃高速缓存条目,直到高速缓存大小等于指定的>值为止.

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的)反编译表明,缓存是 internal 链接列表.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. 存储缓存条目的内存成本;最大值的用法完全是在 Regex 创建中这样的逻辑中进行的:

  1. the memory cost of storing the cached entries; the usage of the maximum is simply in logic like this on Regex creation:

  • 一般来说,我们缓存吗?
    • 如果是,请缓存此正则表达式
    • 我们现在是否已超过最大缓存大小?
      • 如果是,请删除最后一个缓存条目


      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.

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

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