2 unstable releases

0.3.0 Jan 19, 2024
0.2.0 Mar 21, 2023

#329 in Concurrency

Download history 25191/week @ 2024-11-16 12166/week @ 2024-11-23 20818/week @ 2024-11-30 19230/week @ 2024-12-07 20021/week @ 2024-12-14 3051/week @ 2024-12-21 4809/week @ 2024-12-28 19702/week @ 2025-01-04 28659/week @ 2025-01-11 19609/week @ 2025-01-18 26650/week @ 2025-01-25 29171/week @ 2025-02-01 30362/week @ 2025-02-08 23023/week @ 2025-02-15 28241/week @ 2025-02-22 25571/week @ 2025-03-01

113,222 downloads per month
Used in 7 crates (2 directly)

MIT license

17KB
378 lines

CacheMap

CacheMap is a data structure for concurrently caching values.

The cache function will look up a value in the map, or generate and store a new one using the provided function.

This is a updated and maintained fork of hclarke/cachemap.

Example

use cachemap::CacheMap;
	
let m = CacheMap::new();

let fst = m.cache("key", || 5u32);
let snd = m.cache("key", || 7u32);

assert_eq!(*fst, *snd);
assert_eq!(*fst, 5u32);

Features

  • Can cache values concurrently (using &CacheMap<K,V> rather than &mut CacheMap<K,V>).
  • Returned references use the map's lifetime, so clients can avoid smart pointers.
  • Clients can optionally enable the dashmap feature, which uses dashmap internally and allows:
    • getting Arc<V> pointers, in case values need to outlive the map, and
    • adding Arc<V> directly, allowing unsized values, and re-using Arc<V>s from elsewhere.
  • Clients can optionally enable the abi_stable feature which will derive abi_stable::StableAbi on the type.

AntiFeatures

  • There is no cache invalidation: the only way to remove things from a CacheMap is to drop it.

Dependencies

~0–6MB
~21K SLoC