df-cache 0.1.0

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

pub enum Mode {
    Redis(Redis),
}

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);
            }
        }
    }
    /// 设置列表缓存
    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);
            }
        }
    }
    /// 设置消息队列
    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);
            }
        }
    }
    /// 获取缓存
    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);
                    }
                }
            }
        }
    }
    /// 获取列表缓存
    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
            }
        }
    }
    /// 获取列表缓存
    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);
                println!("{:?}",data);
                JsonValue::from(data)
                // 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
            }
        }
    }
    /// 删除缓存
    pub fn del(&mut self, db: i8, key: &str) -> bool {
        match self {
            Mode::Redis(conn) => {
                return conn.db(db).del(key);
            }
        }
    }
}