use std::{num::NonZeroUsize, sync::Mutex};
use lru::LruCache;
use super::EntityMutationResults;
use crate::entity::EntityId;
pub struct EntityMutationsCache {
cache: Mutex<LruCache<EntityId, EntityMutationResults>>,
}
impl EntityMutationsCache {
pub fn new(size: NonZeroUsize) -> EntityMutationsCache {
EntityMutationsCache {
cache: Mutex::new(LruCache::new(size)),
}
}
pub fn get(&self, entity_id: &str) -> Option<EntityMutationResults> {
let mut cache = self
.cache
.lock()
.expect("Entity mutations cache lock is poisoned");
cache.get(entity_id).cloned()
}
pub fn put(&self, entity_id: &str, results: EntityMutationResults) {
let mut cache = self
.cache
.lock()
.expect("Entity mutations cache lock is poisoned");
cache.put(entity_id.to_string(), results);
}
pub fn remove(&self, entity_id: &str) {
let mut cache = self
.cache
.lock()
.expect("Entity mutations cache lock is poisoned");
cache.pop(entity_id);
}
}