How do I clear the regular expression cache in Python?

Presently, when regular expressions are compiled, the result is cached so that if the same regex is compiled again, it is retrieved from the cache and no extra effort is required. This cache supports up to 100 entries. Once the 100th entry is reached, the cache is cleared and a new compile must occur.

The objective of caching is to decrease the average call time of the function. The overhead associated with keeping more information in _cache and paring it instead of clearing it would increase that average call time. The _cache.clear() call will complete quickly, and even though cache is lost this is preferable over maintaining a cache state and having the overhead of removing individual elements from the cache when the limit is reached.

There are a few things to think about when calculating the cache efficiency −

  • Average call time on cache hits (very short)

  • Average call time on cache misses (longer)

  • Frequency of cache hits (fairly uncommon)

  • Call time when cache is cleared or pruned (fairly uncommon)

The regular expression cache is cleared using _cache.clear() when it reaches _MAXCACHE of entries.