#sync #data-structure #cache #hash-map

cachemap2

A concurrent insert-only hashmap for caching values

1 unstable release

0.2.0 Mar 21, 2023

#615 in Caching

Download history 235/week @ 2023-08-11 276/week @ 2023-08-18 163/week @ 2023-08-25 185/week @ 2023-09-01 175/week @ 2023-09-08 187/week @ 2023-09-15 222/week @ 2023-09-22 377/week @ 2023-09-29 350/week @ 2023-10-06 316/week @ 2023-10-13 198/week @ 2023-10-20 738/week @ 2023-10-27 467/week @ 2023-11-03 6794/week @ 2023-11-10 21550/week @ 2023-11-17 11216/week @ 2023-11-24

40,311 downloads per month
Used in 7 crates (3 directly)

MIT license

16KB
361 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–6.5MB
~19K SLoC