aboutsummaryrefslogtreecommitdiff
path: root/cache/src/backends/mod.rs
blob: 5872255e27044cf130456ddf9c7ad2fcdd65a29c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
    This file is part of jellything (https://codeberg.org/metamuffin/jellything)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2025 metamuffin <metamuffin.org>
*/
pub mod filesystem;
pub mod rocksdb;

use crate::{
    CONF,
    backends::{filesystem::Filesystem, rocksdb::Rocksdb},
};
use anyhow::{Result, bail};

pub(crate) trait CacheStorage: Send + Sync + 'static {
    fn store(&self, key: String, value: &[u8]) -> Result<()>;
    fn read(&self, key: &str) -> Result<Option<Vec<u8>>>;
}

pub fn init_backend() -> Result<Box<dyn CacheStorage>> {
    Ok(match CONF.driver.as_str() {
        "filesystem" => Box::new(Filesystem::new(&CONF)),
        "rocksdb" => Box::new(Rocksdb::new(&CONF)?),
        _ => bail!("unknown driver"),
    })
}