use crate::{Hash, Transaction};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
const MAX_CACHED: usize = 10_000;
#[cfg(feature = "ctv")]
pub struct PaymentTxCache {
cache: Mutex<HashMap<Hash, Transaction>>,
order: Mutex<Vec<Hash>>,
}
#[cfg(feature = "ctv")]
impl PaymentTxCache {
pub fn new() -> Self {
Self {
cache: Mutex::new(HashMap::new()),
order: Mutex::new(Vec::new()),
}
}
pub fn store(&self, tx_hash: Hash, tx: Transaction) {
let mut cache = self.cache.lock().unwrap();
let mut order = self.order.lock().unwrap();
if cache.len() >= MAX_CACHED && !cache.contains_key(&tx_hash) {
while let Some(old_hash) = order.first().copied() {
cache.remove(&old_hash);
order.remove(0);
if cache.len() < MAX_CACHED {
break;
}
}
}
if !cache.contains_key(&tx_hash) {
order.push(tx_hash);
}
cache.insert(tx_hash, tx);
}
pub fn get(&self, tx_hash: &Hash) -> Option<Transaction> {
let cache = self.cache.lock().unwrap();
cache.get(tx_hash).cloned()
}
}
#[cfg(feature = "ctv")]
impl Default for PaymentTxCache {
fn default() -> Self {
Self::new()
}
}
#[cfg(not(feature = "ctv"))]
pub struct PaymentTxCache;
#[cfg(not(feature = "ctv"))]
impl PaymentTxCache {
pub fn new() -> Self {
Self
}
pub fn store(&self, _tx_hash: Hash, _tx: Transaction) {}
pub fn get(&self, _tx_hash: &Hash) -> Option<Transaction> {
None
}
}
#[cfg(not(feature = "ctv"))]
impl Default for PaymentTxCache {
fn default() -> Self {
Self
}
}