You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.1 KiB
48 lines
1.1 KiB
use crate::models::JobMeta; |
|
use lazy_static::lazy_static; |
|
use std::{ |
|
collections::HashMap, |
|
ops::Deref, |
|
sync::{RwLock, RwLockReadGuard}, |
|
}; |
|
use uuid::Uuid; |
|
|
|
type Cache = HashMap<Uuid, JobMeta>; |
|
|
|
lazy_static! { |
|
static ref JOB_CACHE: RwLock<Cache> = RwLock::new(HashMap::new()); |
|
} |
|
|
|
pub struct JobCache; |
|
|
|
impl JobCache { |
|
pub fn insert(job_meta: JobMeta) { |
|
JOB_CACHE.write().unwrap().insert(job_meta.id, job_meta); |
|
} |
|
|
|
pub fn contains(uid: &Uuid) -> bool { |
|
JOB_CACHE.read().unwrap().contains_key(uid) |
|
} |
|
|
|
pub fn get(uid: &Uuid) -> Option<JobCacheHolder> { |
|
if !Self::contains(uid) { |
|
return None; |
|
} |
|
let lock = JOB_CACHE.read().unwrap(); |
|
Some(JobCacheHolder(lock, uid)) |
|
} |
|
|
|
pub fn remove(uid: &Uuid) { |
|
JOB_CACHE.write().unwrap().remove(uid); |
|
} |
|
} |
|
|
|
pub struct JobCacheHolder<'jm>(pub RwLockReadGuard<'jm, Cache>, pub &'jm Uuid); |
|
|
|
impl<'jm> Deref for JobCacheHolder<'jm> { |
|
type Target = JobMeta; |
|
|
|
fn deref(&self) -> &Self::Target { |
|
self.0.get(self.1).unwrap() |
|
} |
|
}
|
|
|