#cache #batching #facebook #async-await #load #batcher #requests

dataloader

Rust implementation of Facebook's DataLoader using async-await

23 releases (breaking)

0.17.0 Oct 25, 2023
0.16.0 Sep 6, 2022
0.15.0 Mar 22, 2022
0.14.0 Jan 12, 2021
0.5.0 Feb 3, 2017

#191 in Asynchronous

Download history 337/week @ 2023-12-21 239/week @ 2023-12-28 918/week @ 2024-01-04 722/week @ 2024-01-11 658/week @ 2024-01-18 859/week @ 2024-01-25 950/week @ 2024-02-01 854/week @ 2024-02-08 492/week @ 2024-02-15 721/week @ 2024-02-22 1180/week @ 2024-02-29 1373/week @ 2024-03-07 1731/week @ 2024-03-14 1259/week @ 2024-03-21 992/week @ 2024-03-28 1662/week @ 2024-04-04

6,007 downloads per month

MIT/Apache

24KB
507 lines

Dataloader

Rust Crates.io

Rust implementation of Facebook's DataLoader using async-await.

Documentation

Features

  • Batching load requests with caching
  • Batching load requests without caching

Usage

Switching runtime, by using cargo features

  • runtime-async-std (default), to use the async-std runtime
    • dataloader = "0.17"
  • runtime-tokio to use the Tokio runtime
    • dataloader = { version = "0.17", default-features = false, features = ["runtime-tokio"]}

Add to your Cargo.toml:

[dependencies]
dataloader = "0.17"
futures = "0.3"

Example:

use dataloader::cached::Loader;
use dataloader::BatchFn;
use futures::executor::block_on;
use futures::future::ready;
use std::collections::HashMap;
use std::thread;

struct MyLoadFn;

impl BatchFn<usize, usize> for MyLoadFn {
    async fn load(&mut self, keys: &[usize]) -> HashMap<usize, usize> {
        println!("BatchFn load keys {:?}", keys);
        let ret = keys.iter()
            .map(|v| (v.clone(), v.clone()))
            .collect::<HashMap<_, _>>();
        ready(ret).await
    }
}

fn main() {
    let mut i = 0;
    while i < 2 {
        let a = MyLoadFn;
        let loader = Loader::new(a).with_max_batch_size(4);

        let l1 = loader.clone();
        let h1 = thread::spawn(move || {
            let r1 = l1.load(1);
            let r2 = l1.load(2);
            let r3 = l1.load(3);

            let r4 = l1.load_many(vec![2, 3, 4, 5, 6, 7, 8]);
            let f = futures::future::join4(r1, r2, r3, r4);
            println!("{:?}", block_on(f));
        });

        let l2 = loader.clone();
        let h2 = thread::spawn(move || {
            let r1 = l2.load(1);
            let r2 = l2.load(2);
            let r3 = l2.load(3);
            let r4 = l2.load(4);
            let f = futures::future::join4(r1, r2, r3, r4);
            println!("{:?}", block_on(f));
        });

        h1.join().unwrap();
        h2.join().unwrap();
        i += 1;
    }
}

LICENSE

This project is licensed under either of

at your option.

Dependencies

~0–12MB
~114K SLoC