#hash-map #cache #sync

cachemap2

A concurrent insert-only hashmap for caching values

2 unstable releases

0.3.0 Jan 19, 2024
0.2.0 Mar 21, 2023

#281 in Caching

Download history 31864/week @ 2026-01-05 44568/week @ 2026-01-12 42620/week @ 2026-01-19 42425/week @ 2026-01-26 47909/week @ 2026-02-02 44059/week @ 2026-02-09 32004/week @ 2026-02-16 36451/week @ 2026-02-23 29886/week @ 2026-03-02 39634/week @ 2026-03-09 40180/week @ 2026-03-16 48752/week @ 2026-03-23 34775/week @ 2026-03-30 37080/week @ 2026-04-06 45982/week @ 2026-04-13 33010/week @ 2026-04-20

151,470 downloads per month
Used in 8 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–1.3MB
~21K SLoC