2 unstable releases

0.3.0 Jan 19, 2024
0.2.0 Mar 21, 2023

#428 in Caching

Download history 24954/week @ 2024-10-18 22862/week @ 2024-10-25 22162/week @ 2024-11-01 28383/week @ 2024-11-08 26827/week @ 2024-11-15 14745/week @ 2024-11-22 18556/week @ 2024-11-29 20737/week @ 2024-12-06 18151/week @ 2024-12-13 6328/week @ 2024-12-20 2663/week @ 2024-12-27 18142/week @ 2025-01-03 28146/week @ 2025-01-10 20110/week @ 2025-01-17 25663/week @ 2025-01-24 23910/week @ 2025-01-31

101,604 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–6MB
~25K SLoC