use parking_lot::Mutex;
use std::collections::HashMap;
use std::sync::Arc;
pub struct ClientSessionMemoryCache {
cache: Mutex<HashMap<Vec<u8>, Vec<u8>>>,
max_entries: usize,
}
impl ClientSessionMemoryCache {
pub fn new(size: usize) -> Arc<ClientSessionMemoryCache> {
debug_assert!(size > 0);
Arc::new(ClientSessionMemoryCache {
cache: Mutex::new(HashMap::new()),
max_entries: size,
})
}
fn limit_size(&self) {
let mut cache = self.cache.lock();
while cache.len() > self.max_entries {
let k = cache.keys().next().unwrap().clone();
let _ = cache.remove(&k);
}
}
}
impl rustls::StoresClientSessions for ClientSessionMemoryCache {
fn put(&self, key: Vec<u8>, value: Vec<u8>) -> bool {
let _ = self.cache.lock().insert(key, value);
self.limit_size();
true
}
fn get(&self, key: &[u8]) -> Option<Vec<u8>> {
self.cache.lock().get(key).cloned()
}
}
pub struct ServerSessionMemoryCache {
cache: Mutex<HashMap<Vec<u8>, Vec<u8>>>,
max_entries: usize,
}
impl ServerSessionMemoryCache {
pub fn new(size: usize) -> Arc<ServerSessionMemoryCache> {
debug_assert!(size > 0);
Arc::new(ServerSessionMemoryCache {
cache: Mutex::new(HashMap::new()),
max_entries: size,
})
}
fn limit_size(&self) {
let mut cache = self.cache.lock();
while cache.len() > self.max_entries {
let k = cache.keys().next().unwrap().clone();
let _ = cache.remove(&k);
}
}
}
impl rustls::StoresServerSessions for ServerSessionMemoryCache {
fn put(&self, key: Vec<u8>, value: Vec<u8>) -> bool {
let _ = self.cache.lock().insert(key, value);
self.limit_size();
true
}
fn get(&self, key: &[u8]) -> Option<Vec<u8>> {
self.cache.lock().get(key).cloned()
}
fn take(&self, key: &[u8]) -> Option<Vec<u8>> {
self.cache.lock().remove(key)
}
}