#cache #coalescing #coalesce

cache-compute

This crate implements request/async computation coalescing

3 releases (breaking)

0.3.0 Aug 11, 2023
0.2.0 Jul 23, 2023
0.1.0 Apr 9, 2023

#208 in Caching

Download history 26/week @ 2024-09-03 227/week @ 2024-09-10 139/week @ 2024-09-17 202/week @ 2024-09-24 193/week @ 2024-10-01 147/week @ 2024-10-08 273/week @ 2024-10-15 182/week @ 2024-10-22 107/week @ 2024-10-29 131/week @ 2024-11-05

747 downloads per month

MIT license

38KB
618 lines

This crate implements request/async computation coalescing.

The starting point for this implementation was fasterthanlime's excellent article on request coalescing in async rust.

Caching of async computations can be a bit of a tough problem. If no cached value is available when we need it, we would want to compute it, often asynchronously. This crate helps ensure that this computation doesn't happen more than it needs to by avoiding starting new computations when one is already happening. Instead, we will subscribe to that computation and work with the result of it as well.

Example

use cache_compute::Cached;

pub async fn get_answer(cached_answer: Cached<u32, ()>) -> u32 {
    if answer_too_old() {
        cached_answer.invalidate();
    }

    cached_answer.get_or_compute(|| async {
        // Really long async computation
        // Phew the computer and network sure need a lot of time to work on this
        // Good thing we cache it
        // ...
        // Ok done
        // Other calls to get_answer will now also use that same value
        // without having to compute it, until it's too old again
        refresh_answer_timer();
        Ok(42)
    })
    .await
    .unwrap()
}

Contributing

Feel free :)

Docs

Here

Dependencies

~3.5–10MB
~93K SLoC