Crates.io | regex-cache |
lib.rs | regex-cache |
version | 0.2.1 |
source | src |
created_at | 2017-07-06 17:17:45.027283 |
updated_at | 2020-10-16 22:41:28.302527 |
description | Lazy and cached regular expressions. |
homepage | |
repository | https://github.com/1aim/rust-regex-cache |
max_upload_size | |
id | 22212 |
size | 26,058 |
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.
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.
LazyRegex
When you have many regular expressions you don't want to use instantly and don't care about delaying the regular expression compilation.
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.
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.