df-cache 0.1.6

This is an Cache
Documentation
use json::{array, JsonValue, object};
use log::info;
use crate::mode::redis::Redis;

#[derive(Clone)]
pub enum Mode {
    Redis(Redis),
    None,
}

impl Mode {
    /// 设置缓存
    pub fn set(&mut self, db: i8, key: &str, value: JsonValue, time: usize) -> bool {
        match self {
            Mode::Redis(conn) => {
                return conn.db(db).set(key, value, time);
            }
            _ => false
        }
    }
    /// 设置列表缓存
    pub fn set_list(&mut self, db: i8, key: &str, value: JsonValue) -> bool {
        match self {
            Mode::Redis(conn) => {
                return conn.db(db).set_list(key, value);
            }
            _ => false
        }
    }
    /// 设置消息队列
    pub fn set_message_queue(&mut self, db: i8, key: &str, value: JsonValue) -> bool {
        match self {
            Mode::Redis(conn) => {
                return conn.db(db).set_message_queue(key, value);
            }
            _ => false
        }
    }
    /// 获取缓存
    pub fn get(&mut self, db: i8, key: &str) -> JsonValue {
        match self {
            Mode::Redis(conn) => {
                let data = conn.db(db).get(key);
                let list = json::parse(data.as_str());
                match list {
                    Ok(e) => {
                        return e;
                    }
                    Err(_e) => {
                        return JsonValue::from(data);
                    }
                }
            }
            _ => object! {}
        }
    }
    /// 获取列表缓存
    pub fn get_list(&mut self, db: i8, key: &str) -> JsonValue {
        match self {
            Mode::Redis(conn) => {
                let data = conn.db(db).get_list(key);
                let mut row = array![];
                for item in data.iter() {
                    let list = json::parse(item.as_str());
                    match list {
                        Ok(e) => {
                            row.push(e).unwrap();
                        }
                        Err(_e) => {
                            row.push(item.clone()).unwrap();
                        }
                    }
                }
                row
            }
            _ => object! {}
        }
    }
    /// 获取列表缓存
    pub fn get_message_queue(&mut self, db: i8, key: &str) -> JsonValue {
        match self {
            Mode::Redis(conn) => {
                let data = conn.db(db).get_message_queue(key);
                info!("{:?}", data);
                JsonValue::from(data)
            }
            _ => object! {}
        }
    }
    /// 删除缓存
    pub fn del(&mut self, db: i8, key: &str) -> bool {
        match self {
            Mode::Redis(conn) => {
                return conn.db(db).del(key);
            }
            _ => false
        }
    }
    pub fn get_object(&mut self, db: i8, key: &str) -> JsonValue {
        match self {
            Mode::Redis(conn) => conn.db(db).get_object(key),
            _ => object! {}
        }
    }
    /// 设置哈希列表
    pub fn set_object(&mut self, db: i8, key: &str, field: &str, value: JsonValue) -> bool {
        match self {
            Mode::Redis(conn) => {
                return conn.db(db).set_object(key, field, value);
            }
            _ => false
        }
    }
}