3 releases

0.1.4 Jul 29, 2024
0.1.3 Jul 4, 2022
0.1.2 Jun 16, 2022
0.1.1 Jun 16, 2022
0.1.0 Jun 16, 2022

#333 in Database interfaces

Download history 322/week @ 2024-08-03 370/week @ 2024-08-10 363/week @ 2024-08-17 295/week @ 2024-08-24 311/week @ 2024-08-31 256/week @ 2024-09-07 230/week @ 2024-09-14 252/week @ 2024-09-21 278/week @ 2024-09-28 773/week @ 2024-10-05 274/week @ 2024-10-12 442/week @ 2024-10-19 384/week @ 2024-10-26 395/week @ 2024-11-02 118/week @ 2024-11-09 127/week @ 2024-11-16

1,095 downloads per month

Apache-2.0

17KB
390 lines

sqlite-cache

crates.io

SQLite-based on-disk cache for Rust.

Usage

let cache = Cache::new(
    CacheConfig::default(),
    rusqlite::Connection::open_in_memory().unwrap(),
).unwrap();
let topic = cache.topic("test-topic").unwrap();
assert!(topic.get("hello").unwrap().is_none());
topic.set("hello", b"world", Duration::from_secs(60))
assert!(&topic.get("hello").unwrap().unwrap().data[..] == b"world");

Locked updates

This library supports locked updates to prevent the thundering herd problem on cache misses. The get_for_update API acquires a per-key lock and returns a KeyUpdater; subsequent get_for_update calls on the same key will block until the previous KeyUpdater is dropped.

let (updater, current_value) = topic.get_for_update("hello").await.unwrap();
let new_value = expensive_computation(current_value).await;
updater.write(new_value, Duration::from_secs(60)).unwrap();

Benchmark

These results are from running benches/cache_benchmark.rs on an Apple M1 processor.

  • mt(4): Per-thread operation latency when running the same task on 4 threads.
lookup - cache size 10000
                        time:   [1.5978 us 1.6051 us 1.6130 us]
lookup mt(4) - cache size 10000
                        time:   [9.7801 us 9.8464 us 9.9329 us]
insert - cache size 10000
                        time:   [4.6316 us 4.6785 us 4.7169 us]
insert mt(4) - cache size 10000
                        time:   [21.195 us 21.420 us 21.614 us]

Dependencies

~23MB
~442K SLoC