use crate::error::{PricingError, Result};
use crate::pricing::PriceModel;
use std::collections::HashMap;
use std::path::Path;
use std::sync::{Arc, Mutex};
pub type BlueprintId = u64;
#[derive(Clone)]
pub struct PriceCache {
cache: Arc<Mutex<HashMap<BlueprintId, PriceModel>>>,
}
impl PriceCache {
pub fn new<P: AsRef<Path>>(_path: P) -> Result<Self> {
Ok(PriceCache {
cache: Arc::new(Mutex::new(HashMap::new())),
})
}
pub fn store_price(&self, blueprint_id: BlueprintId, price_model: &PriceModel) -> Result<()> {
let mut cache = self
.cache
.lock()
.map_err(|e| PricingError::Cache(format!("Lock error: {e}")))?;
cache.insert(blueprint_id, price_model.clone());
Ok(())
}
pub fn get_price(&self, blueprint_id: BlueprintId) -> Result<Option<PriceModel>> {
let cache = self
.cache
.lock()
.map_err(|e| PricingError::Cache(format!("Lock error: {e}")))?;
Ok(cache.get(&blueprint_id).cloned())
}
pub fn remove_price(&self, blueprint_id: BlueprintId) -> Result<Option<PriceModel>> {
let mut cache = self
.cache
.lock()
.map_err(|e| PricingError::Cache(format!("Lock error: {e}")))?;
Ok(cache.remove(&blueprint_id))
}
}