#cache #hash-map #sync #data-structures

cachemap2

A concurrent insert-only hashmap for caching values

2 unstable releases

0.3.0 Jan 19, 2024
0.2.0 Mar 21, 2023

#36 in Caching

Download history 16853/week @ 2024-01-22 21443/week @ 2024-01-29 12534/week @ 2024-02-05 23716/week @ 2024-02-12 20759/week @ 2024-02-19 28416/week @ 2024-02-26 26042/week @ 2024-03-04 19403/week @ 2024-03-11 26798/week @ 2024-03-18 24938/week @ 2024-03-25 30209/week @ 2024-04-01 9481/week @ 2024-04-08 22947/week @ 2024-04-15 24519/week @ 2024-04-22 24190/week @ 2024-04-29 20040/week @ 2024-05-06

92,116 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–6.5MB
~23K SLoC