use std::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: usize) -> 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");
let entity_id = entity_id.to_string();
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");
let entity_id = entity_id.to_string();
cache.put(entity_id, results);
}
pub fn remove(&self, entity_id: &str) {
let mut cache = self
.cache
.lock()
.expect("Entity mutations cache lock is poisoned");
let entity_id = entity_id.to_string();
cache.pop(&entity_id);
}
}