#regex #lazy-evaluation #cache

regex-cache

Lazy and cached regular expressions

8 releases

0.2.1 Oct 16, 2020
0.2.0 May 22, 2018
0.1.6 Mar 8, 2018
0.1.5 Jul 12, 2017

#123 in Caching

Download history 27639/week @ 2024-07-26 29762/week @ 2024-08-02 30682/week @ 2024-08-09 28113/week @ 2024-08-16 27772/week @ 2024-08-23 35385/week @ 2024-08-30 45550/week @ 2024-09-06 40659/week @ 2024-09-13 47583/week @ 2024-09-20 47095/week @ 2024-09-27 44341/week @ 2024-10-04 39589/week @ 2024-10-11 51273/week @ 2024-10-18 46724/week @ 2024-10-25 49568/week @ 2024-11-01 41813/week @ 2024-11-08

197,544 downloads per month
Used in 39 crates (2 directly)

MIT license

25KB
382 lines

regex-cache Crates.io regex-cache MIT Build Status

This crate provides a library for caching or lazily creating regular expressions.

Lazy regular expressions are backed by a OnceMutex, while the regular expression cache is backed by a Least Recently Used cache.

Why not lazy_static!?

lazy_static! is great but it's only usable when you know what your regular expression is.

In some cases you're loading an insane number of regular expressions (looking at you libphonenumber) that you might eventually use but don't actually want to store as String since that makes you lose type information and ease of use.

When to use LazyRegex

When you have many regular expressions you don't want to use instantly and don't care about delaying the regular expression compilation.

When to use RegexCache

When you want to limit the number of active regular expressions, the RegexCache only keeps around a specified number of actively used regular expressions.

Since it's an LRU cache having a small limit and using different regular expressions every time ends up wasting memory and time for nothing.

When to use CachedRegex

When you want to transparently use a RegexCache and still have an easy to use and typed regular expression instead of storing a String to then compile with the RegexCache.

Keep in mind what's stored inside a CachedRegex is an Arc<Mutex<RegexCache>>, which means there is locking involved when calling any methods on CachedRegex.

For technical reasons there is no way to get a &Regex out of a CachedRegex and some methods from Regex aren't usable on a CachedRegex, it should still be fine for most uses.

Dependencies

~2.3–3.5MB
~57K SLoC