2 unstable releases

0.3.0 Jan 19, 2024
0.2.0 Mar 21, 2023

#177 in Concurrency

Download history 28951/week @ 2024-07-21 22410/week @ 2024-07-28 20798/week @ 2024-08-04 14534/week @ 2024-08-11 17255/week @ 2024-08-18 19837/week @ 2024-08-25 17592/week @ 2024-09-01 16613/week @ 2024-09-08 16597/week @ 2024-09-15 20872/week @ 2024-09-22 15606/week @ 2024-09-29 21174/week @ 2024-10-06 20539/week @ 2024-10-13 24279/week @ 2024-10-20 19307/week @ 2024-10-27 28320/week @ 2024-11-03

92,704 downloads per month
Used in 8 crates (3 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–5.5MB
~21K SLoC