Cache guilds.

This commit is contained in:
Paul Makles
2020-08-03 15:23:32 +02:00
parent fa6ed75ed8
commit 6a1912c27b
7 changed files with 327 additions and 221 deletions

View File

@@ -1,9 +1,11 @@
use crate::database::get_collection;
use super::get_collection;
use serde::{Deserialize, Serialize};
use rocket::request::FromParam;
use std::sync::{Arc, Mutex};
use lru::LruCache;
use bson::{doc, from_bson};
use rocket::http::RawStr;
use lru::LruCache;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct LastMessage {
@@ -42,6 +44,100 @@ lazy_static! {
static ref CACHE: Arc<Mutex<LruCache<String, Channel>>> = Arc::new(Mutex::new(LruCache::new(4_000_000)));
}
pub fn fetch_channel(id: &str) -> Result<Option<Channel>, String> {
{
if let Ok(mut cache) = CACHE.lock() {
let existing = cache.get(&id.to_string());
if let Some(channel) = existing {
return Ok(Some((*channel).clone()));
}
} else {
return Err("Failed to lock cache.".to_string());
}
}
let col = get_collection("channels");
if let Ok(result) = col.find_one(doc! { "_id": id }, None) {
if let Some(doc) = result {
if let Ok(channel) = from_bson(bson::Bson::Document(doc)) as Result<Channel, _> {
let mut cache = CACHE.lock().unwrap();
cache.put(id.to_string(), channel.clone());
Ok(Some(channel))
} else {
Err("Failed to deserialize channel!".to_string())
}
} else {
Ok(None)
}
} else {
Err("Failed to fetch channel from database.".to_string())
}
}
pub fn fetch_channels(ids: &Vec<String>) -> Result<Option<Vec<Channel>>, String> {
let mut missing = vec![];
let mut channels = vec![];
{
if let Ok(mut cache) = CACHE.lock() {
for gid in ids {
let existing = cache.get(gid);
if let Some(channel) = existing {
channels.push((*channel).clone());
} else {
missing.push(gid);
}
}
} else {
return Err("Failed to lock cache.".to_string());
}
}
if missing.len() == 0 {
return Ok(Some(channels))
}
let col = get_collection("channels");
if let Ok(result) = col.find(doc! { "_id": { "$in": missing } }, None) {
for item in result {
let mut cache = CACHE.lock().unwrap();
if let Ok(doc) = item {
if let Ok(channel) = from_bson(bson::Bson::Document(doc)) as Result<Channel, _> {
cache.put(channel.id.clone(), channel.clone());
channels.push(channel);
} else {
return Err("Failed to deserialize channel!".to_string())
}
} else {
return Err("Failed to fetch channel.".to_string());
}
}
Ok(Some(channels))
} else {
Err("Failed to fetch channel from database.".to_string())
}
}
impl<'r> FromParam<'r> for Channel {
type Error = &'r RawStr;
fn from_param(param: &'r RawStr) -> Result<Self, Self::Error> {
if let Ok(result) = fetch_channel(param) {
if let Some(channel) = result {
Ok(channel)
} else {
Err(param)
}
} else {
Err(param)
}
}
}
/*pub fn test() {
use std::time::Instant;
@@ -75,28 +171,3 @@ lazy_static! {
println!("It took {} seconds, roughly {}ms per entry.", now.elapsed().as_secs_f64(), now.elapsed().as_millis() as f64 / 1_000_000.0);
}*/
pub fn fetch_channel(id: &str) -> Channel {
{
let mut cache = CACHE.lock().unwrap();
let existing = cache.get(&id.to_string());
if let Some(channel) = existing {
return (*channel).clone();
}
}
let col = get_collection("channels");
let result = col.find_one(doc! { "_id": id }, None).unwrap();
if let Some(doc) = result {
let channel: Channel = from_bson(bson::Bson::Document(doc)).expect("Failed to unwrap channel.");
let mut cache = CACHE.lock().unwrap();
cache.put(id.to_string(), channel.clone());
return channel;
}
panic!("Channel does not exist!");
}