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
}
}
}