worm

Worm is used to persist the results of a function call indefinitely in a write-once, read-many cache.

Usage

Worm is a write-once, read-many cache designed to be used for caching deterministic values. Good examples of things to use this for are regex compilation and lookup tables - things that are calculated once and never change.

Limitations

Functions

pub fn persist(generator: fn() -> a) -> a

Persist the result of a callback.

The generator function is expected to be deterministic and pure. Your generator function will be called at least once, but may be called more than once due to race conditions during the initial filling of the cache. After the cache is filled, your generator function will never be called again. If your generator produces side effects then those side effects will be extremely unreliable.

Examples

pub type RegexCache {
  RegexCache(greeting: regex.Regex, name: regex.Regex)
}

fn regexes() -> RegexCache {
  use <- worm.persist()
  let assert Ok(greeting) = regex.from_string("^\\w+")
  let assert Ok(name) = regex.from_string("\\w+$")
  RegexCache(greeting, name)
}
Search Document