use std::sync::Arc;
use lru::LruCache;
use tox_crypto::*;
use futures::lock::Mutex;
#[derive(Clone)]
pub struct PrecomputedCache {
sk: SecretKey,
precomputed_keys: Arc<Mutex<LruCache<PublicKey, PrecomputedKey>>>,
}
impl PrecomputedCache {
pub fn new(sk: SecretKey, capacity: usize) -> PrecomputedCache {
PrecomputedCache {
sk,
precomputed_keys: Arc::new(Mutex::new(LruCache::new(capacity))),
}
}
pub async fn get(&self, pk: PublicKey) -> PrecomputedKey {
let mut keys = self.precomputed_keys.lock().await;
if let Some(precomputed_key) = keys.get(&pk) {
return precomputed_key.clone();
}
let precomputed_key = precompute(&pk, &self.sk);
keys.put(pk, precomputed_key.clone());
precomputed_key
}
}