use std::any::Any;
use std::collections::HashMap;
use std::fmt;
use std::sync::{Arc, Mutex, MutexGuard, Weak};
use std::time::{Duration, Instant};
#[derive(Clone)]
pub struct QueryCache {
inner: Arc<Mutex<Inner>>,
}
pub(crate) struct Inner {
broadcast: Option<tokio::sync::mpsc::UnboundedSender<String>>,
capacity: usize,
entries: HashMap<String, Entry>,
generations: HashMap<String, u64>,
hits: u64,
misses: u64,
}
struct Entry {
value: Arc<dyn Any + Send + Sync>,
table: &'static str,
generation: u64,
expires: Instant,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct CacheStats {
pub hits: u64,
pub misses: u64,
pub entries: usize,
}
impl QueryCache {
pub fn new(capacity: usize) -> Self {
Self {
inner: Arc::new(Mutex::new(Inner {
broadcast: None,
capacity: capacity.max(1),
entries: HashMap::new(),
generations: HashMap::new(),
hits: 0,
misses: 0,
})),
}
}
fn lock(&self) -> MutexGuard<'_, Inner> {
self.inner.lock().unwrap_or_else(|e| e.into_inner())
}
pub fn invalidate(&self, table: &str) {
let mut inner = self.lock();
inner.invalidate(table);
if let Some(tx) = &inner.broadcast {
let _ = tx.send(table.to_owned());
}
}
pub fn clear(&self) {
let mut inner = self.lock();
if let Some(tx) = &inner.broadcast {
let _ = tx.send(String::new());
}
inner.clear();
}
pub(crate) fn downgrade(&self) -> Weak<Mutex<Inner>> {
Arc::downgrade(&self.inner)
}
pub(crate) fn set_broadcast(&self, tx: tokio::sync::mpsc::UnboundedSender<String>) {
self.lock().broadcast = Some(tx);
}
}
pub(crate) fn apply_remote(inner: &Mutex<Inner>, table: &str) {
let mut inner = inner.lock().unwrap_or_else(|e| e.into_inner());
if table.is_empty() {
inner.clear();
} else {
inner.invalidate(table);
}
}
impl Inner {
fn invalidate(&mut self, table: &str) {
*self.generations.entry(table.to_owned()).or_default() += 1;
self.entries.retain(|_, e| e.table != table);
}
fn clear(&mut self) {
let inner = self;
*inner.generations.entry(String::new()).or_default() += 1;
inner.entries.clear();
}
}
impl QueryCache {
pub fn stats(&self) -> CacheStats {
let inner = self.lock();
CacheStats {
hits: inner.hits,
misses: inner.misses,
entries: inner.entries.len(),
}
}
pub(crate) fn generation(&self, table: &str) -> u64 {
let inner = self.lock();
inner.generation(table)
}
pub(crate) fn get<T: Clone + 'static>(&self, key: &str) -> Option<T> {
let mut inner = self.lock();
let now = Instant::now();
let fresh = inner.entries.get(key).and_then(|e| {
(e.expires > now && e.generation == inner.generation(e.table))
.then(|| e.value.downcast_ref::<T>().cloned())
.flatten()
});
match fresh {
Some(value) => {
inner.hits += 1;
crate::metrics::cache(true);
Some(value)
}
None => {
inner.misses += 1;
crate::metrics::cache(false);
inner.entries.remove(key);
None
}
}
}
pub(crate) fn put<T: Send + Sync + 'static>(
&self,
key: String,
table: &'static str,
generation: u64,
ttl: Duration,
value: T,
) {
let mut inner = self.lock();
if generation != inner.generation(table) {
return; }
let now = Instant::now();
if inner.entries.len() >= inner.capacity && !inner.entries.contains_key(&key) {
inner.entries.retain(|_, e| e.expires > now);
if inner.entries.len() >= inner.capacity {
let oldest = inner
.entries
.iter()
.min_by_key(|(_, e)| e.expires)
.map(|(k, _)| k.clone());
if let Some(oldest) = oldest {
inner.entries.remove(&oldest);
}
}
}
inner.entries.insert(
key,
Entry {
value: Arc::new(value),
table,
generation,
expires: now + ttl,
},
);
}
}
impl Inner {
fn generation(&self, table: &str) -> u64 {
let global = self.generations.get("").copied().unwrap_or(0);
let table = self.generations.get(table).copied().unwrap_or(0);
global.wrapping_add(table)
}
}
impl fmt::Debug for QueryCache {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let inner = self.lock();
f.debug_struct("QueryCache")
.field("capacity", &inner.capacity)
.field("entries", &inner.entries.len())
.field("hits", &inner.hits)
.field("misses", &inner.misses)
.finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn get_put_invalidate() {
let cache = QueryCache::new(2);
let ttl = Duration::from_secs(60);
let g = cache.generation("users");
cache.put("a".into(), "users", g, ttl, vec![1, 2]);
assert_eq!(cache.get::<Vec<i32>>("a"), Some(vec![1, 2]));
assert_eq!(cache.get::<String>("a"), None, "type mismatch is a miss");
cache.put("a".into(), "users", g, ttl, vec![1, 2]);
cache.invalidate("users");
assert_eq!(cache.get::<Vec<i32>>("a"), None);
cache.put("b".into(), "users", g, ttl, 5);
assert_eq!(cache.get::<i32>("b"), None);
let g = cache.generation("users");
cache.put("c".into(), "users", g, Duration::ZERO, 5);
assert_eq!(cache.get::<i32>("c"), None, "expired");
let stats = cache.stats();
assert_eq!((stats.hits, stats.misses), (1, 4));
}
#[test]
fn capacity_and_clear() {
let cache = QueryCache::new(2);
let ttl = Duration::from_secs(60);
for (i, key) in ["a", "b", "c"].into_iter().enumerate() {
let g = cache.generation("t");
cache.put(key.into(), "t", g, ttl + Duration::from_secs(i as u64), i);
}
assert_eq!(cache.stats().entries, 2);
assert_eq!(cache.get::<usize>("a"), None, "oldest evicted");
let g = cache.generation("other");
cache.clear();
cache.put("d".into(), "other", g, ttl, 1);
assert_eq!(cache.get::<usize>("d"), None);
}
}