forked from
microcosm.blue/microcosm-rs
Constellation, Spacedust, Slingshot, UFOs: atproto crates and services for microcosm
1use crate::CachedRecord;
2use foyer::{DirectFsDeviceOptions, Engine, HybridCache, HybridCacheBuilder};
3use std::path::Path;
4
5pub async fn firehose_cache(
6 cache_dir: impl AsRef<Path>,
7 memory_mb: usize,
8 disk_gb: usize,
9) -> Result<HybridCache<String, CachedRecord>, String> {
10 let cache = HybridCacheBuilder::new()
11 .with_name("firehose")
12 .memory(memory_mb * 2_usize.pow(20))
13 .with_weighter(|k: &String, v| k.len() + std::mem::size_of_val(v))
14 .storage(Engine::large())
15 .with_device_options(
16 DirectFsDeviceOptions::new(cache_dir)
17 .with_capacity(disk_gb * 2_usize.pow(30))
18 .with_file_size(16 * 2_usize.pow(20)), // note: this does limit the max cached item size, warning jumbo records
19 )
20 .build()
21 .await
22 .map_err(|e| format!("foyer setup error: {e:?}"))?;
23 Ok(cache)
24}