memory_stash

Crates.iomemory_stash
lib.rsmemory_stash
version0.1.1
created_at2025-11-02 23:30:08.529442+00
updated_at2025-11-02 23:39:37.905237+00
descriptionIn-memory cache with policies
homepage
repositoryhttps://codeberg.org/nahratzah/hoard
max_upload_size
id1913720
size284,904
Ariane van der Steldt (nahratzah)

documentation

README

An in-memory cache, with policies.

Each cache has:

  • a Key type, that indicates the lookup key of the cache.
  • a T type, that indicates the value in the cache.
  • a Error type, that indicates the errors that can occur when using the map.
  • a Resolver, which (on a cache miss) takes a Key, and computes a Result<T, Error>.
  • one or more policies controlling cache behaviour.

Synchronous and Asynchronous

This crate contains three caches: BlockingCache, AsyncCache, and DualModeCache. And it contains both a single-threaded version and a thread-safe version of these caches.

  • BlockingCache: this is a cache that waits for the resolution to finish.
  • AsyncCache: this is a cache that uses async code, so it'll allow the program to do something else while resolution is done.
  • DualModeCache: this cache allows both regular access (like BlockingCache) and asynchronous access (like AsyncCache).

Policies

There are multiple policies that control when elements expire:

  • MaxAge: expire elements after a certain amount of time passes.
  • MaxAgeErr: enable negative cache (caching of errors) and expire errors after a certain amount of time passes.
  • MaxAgeWith: expire elements after a certain amount of time, but use a function to figure out the time.
  • MaxAgeErrWith: enable negative cache (caching of errors) and expire errors after a certain amount of time, but use a function to figure out the time.

There are multiple policies that control the size of the cache:

  • MaxSize: limit the cache to a specific number of elements.
  • SimpleCostWith: limit the cache using a cost function.

The cache allows you to have multiple policies in effect simultaneously using a tuple:

/// Create a cache that holds at most 1000 elements,
/// caches values for 1 hour,
/// and caches errors for 5 minutes.
(MaxSize(1000), MaxAge(Duration::from_secs(3600)), MaxAgeErr(Duration::from_secs(300)))

You can write your own policies too.

Further Reading

See also documentation on docs.rs and memory_stash on crates.io.

Commit count: 0

cargo fmt