use crate::models::JobMeta; use lazy_static::lazy_static; use std::{ collections::HashMap, ops::Deref, sync::{RwLock, RwLockReadGuard}, }; use uuid::Uuid; type Cache = HashMap; lazy_static! { static ref JOB_CACHE: RwLock = 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 { 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() } }