use std::collections::{HashMap, VecDeque};
use std::time::{Duration, Instant};
pub struct JwtCache {
map: HashMap<String, Instant>, order: VecDeque<String>, remote_verification_interval: Duration,
capacity: usize,
}
impl JwtCache {
pub fn new(remote_verification_interval: Duration, capacity: usize) -> Self {
Self {
map: HashMap::with_capacity(capacity),
order: VecDeque::with_capacity(capacity),
remote_verification_interval,
capacity,
}
}
pub fn is_recent(&self, key: &str) -> bool {
self.map
.get(key)
.is_some_and(|t| t.elapsed() <= self.remote_verification_interval)
}
pub fn record(&mut self, key: String) {
self.remove_expired();
if self.map.contains_key(&key) {
self.map.insert(key.clone(), Instant::now());
} else {
if self.map.len() >= self.capacity {
if let Some(oldest) = self.order.pop_front() {
self.map.remove(&oldest);
}
}
self.map.insert(key.clone(), Instant::now());
self.order.push_back(key);
}
}
pub fn remove_expired(&mut self) {
let now = Instant::now();
let mut expired = Vec::new();
for key in &self.order {
if let Some(&last) = self.map.get(key).as_ref() {
if now.duration_since(last.to_owned()) > self.remote_verification_interval {
expired.push(key.clone());
}
}
}
for key in expired {
self.map.remove(&key);
self.order.retain(|k| *k != key);
}
}
}