2021-05-02 10:32:03 +00:00
|
|
|
use memcache;
|
|
|
|
|
|
|
|
use crate::note::Note;
|
|
|
|
|
|
|
|
lazy_static! {
|
2021-05-02 11:53:48 +00:00
|
|
|
static ref CLIENT: memcache::Client = memcache::connect(format!(
|
|
|
|
"memcache://{}?timeout=10&tcp_nodelay=true",
|
|
|
|
std::env::var("MEMCACHE").unwrap_or("127.0.0.1:11211".to_string())
|
|
|
|
))
|
|
|
|
.unwrap();
|
2021-05-02 10:32:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set(id: &String, note: &Note) {
|
|
|
|
let serialized = serde_json::to_string(¬e.clone()).unwrap();
|
|
|
|
CLIENT.set(id, serialized, 0).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(id: &String) -> Option<Note> {
|
|
|
|
let value: Option<String> = CLIENT.get(&id).unwrap();
|
|
|
|
match value {
|
|
|
|
None => return None,
|
|
|
|
Some(s) => {
|
|
|
|
let deserialize: Note = serde_json::from_str(&s).unwrap();
|
|
|
|
return Some(deserialize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn del(id: &String) {
|
|
|
|
CLIENT.delete(id).unwrap();
|
|
|
|
}
|