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
27
28
29
30
31
32
33
34
35
36
37
38
39
use std::collections::HashMap;

use tardis::{
    basic::{error::TardisError, result::TardisResult},
    cache::cache_client::TardisCacheClient,
};

static mut CACHE_CLIENTS: Option<HashMap<String, TardisCacheClient>> = None;

pub async fn init(name: &str, url: &str) -> TardisResult<()> {
    let cache = TardisCacheClient::init(url).await?;
    unsafe {
        if CACHE_CLIENTS.is_none() {
            CACHE_CLIENTS = Some(HashMap::new());
        }
        CACHE_CLIENTS.as_mut().expect("Unreachable code").insert(name.to_string(), cache);
    }
    Ok(())
}

pub async fn remove(name: &str) -> TardisResult<()> {
    unsafe {
        if CACHE_CLIENTS.is_none() {
            CACHE_CLIENTS = Some(HashMap::new());
        }
        CACHE_CLIENTS.as_mut().expect("Unreachable code").remove(name);
    }
    Ok(())
}

pub fn get(name: &str) -> TardisResult<&'static TardisCacheClient> {
    unsafe {
        if let Some(client) = CACHE_CLIENTS.as_ref().ok_or_else(|| TardisError::bad_request("[SG.server] Get client failed", ""))?.get(name) {
            Ok(client)
        } else {
            Err(TardisError::bad_request(&format!("[SG.server] Get client {name} failed"), ""))
        }
    }
}