use std::collections::{BTreeMap, HashMap};
use std::hash::Hash;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use super::gate::{Slot, lock, with_now};
use crate::capability::HNil;
use crate::failure::Never;
use crate::failure::{Cause, Exit};
use crate::kernel::{Effect, ExitKind, Restore, suspend, sync, uninterruptible_mask};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct CacheStats {
pub hits: u64,
pub misses: u64,
pub size: usize,
}
type LookupFn<K, V, E, R> = Arc<dyn Fn(K) -> Effect<V, E, R> + Send + Sync>;
#[derive(Clone)]
enum Outcome<V, E> {
Done(Result<V, Cause<E>>),
Retry,
}
type Pending<V, E> = Arc<Slot<Outcome<V, E>>>;
enum Entry<V, E> {
Pending(Pending<V, E>),
Ready {
exit: Result<V, Cause<E>>,
at: Instant,
tick: u64,
},
}
struct CacheState<K, V, E> {
entries: HashMap<K, Entry<V, E>>,
lru: BTreeMap<u64, K>,
tick: u64,
hits: u64,
misses: u64,
}
impl<K: Clone + Eq + Hash, V, E> CacheState<K, V, E> {
fn next_tick(&mut self) -> u64 {
self.tick += 1;
self.tick
}
fn remove(&mut self, key: &K) {
if let Some(Entry::Ready { tick, .. }) = self.entries.remove(key) {
self.lru.remove(&tick);
}
}
fn touch(&mut self, key: &K) {
let new = self.next_tick();
if let Some(Entry::Ready { tick, .. }) = self.entries.get_mut(key) {
let old = std::mem::replace(tick, new);
self.lru.remove(&old);
self.lru.insert(new, key.clone());
}
}
fn insert_ready(&mut self, key: K, exit: Result<V, Cause<E>>, at: Instant, capacity: usize) {
self.remove(&key);
let tick = self.next_tick();
self.lru.insert(tick, key.clone());
self.entries.insert(key, Entry::Ready { exit, at, tick });
while capacity > 0 && self.lru.len() > capacity {
match self.lru.pop_first() {
Some((_, victim)) => {
self.entries.remove(&victim);
}
None => break,
}
}
}
}
enum Step<V, E> {
Hit(Result<V, Cause<E>>),
Wait(Pending<V, E>),
Lead(Pending<V, E>),
}
pub struct Cache<K, V, E = Never, R = HNil> {
state: Arc<Mutex<CacheState<K, V, E>>>,
lookup: LookupFn<K, V, E, R>,
capacity: usize,
ttl: Option<Duration>,
}
impl<K, V, E, R> Clone for Cache<K, V, E, R> {
fn clone(&self) -> Self {
Self {
state: Arc::clone(&self.state),
lookup: Arc::clone(&self.lookup),
capacity: self.capacity,
ttl: self.ttl,
}
}
}
impl<K, V, E, R> std::fmt::Debug for Cache<K, V, E, R> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let st = lock(&self.state);
f.debug_struct("Cache")
.field("capacity", &self.capacity)
.field("ttl", &self.ttl)
.field("entries", &st.entries.len())
.finish()
}
}
fn fresh(ttl: Option<Duration>, at: Instant, now: Instant) -> bool {
ttl.is_none_or(|ttl| now.saturating_duration_since(at) < ttl)
}
impl<K, V, E, R> Cache<K, V, E, R>
where
K: Clone + Eq + Hash + Send + 'static,
V: Clone + Send + 'static,
E: Clone + Send + 'static,
R: Send + Sync + 'static,
{
pub fn new(
capacity: usize,
ttl: Option<Duration>,
lookup: impl Fn(K) -> Effect<V, E, R> + Send + Sync + 'static,
) -> Self {
Self {
state: Arc::new(Mutex::new(CacheState {
entries: HashMap::new(),
lru: BTreeMap::new(),
tick: 0,
hits: 0,
misses: 0,
})),
lookup: Arc::new(lookup),
capacity,
ttl,
}
}
pub fn get(&self, key: K) -> Effect<V, E, R> {
let this = self.clone();
uninterruptible_mask(move |restore| with_now(move |now| this.step(key, now, restore)))
}
fn step(self, key: K, now: Instant, restore: Restore) -> Effect<V, E, R> {
match self.begin(&key, now) {
Step::Hit(r) => Effect::pure(r),
Step::Wait(slot) => restore.apply(slot.wait()).flat_map(move |o| match o {
Outcome::Done(r) => Effect::pure(r),
Outcome::Retry => restore.apply(self.get(key)),
}),
Step::Lead(slot) => {
let lookup = Arc::clone(&self.lookup);
let k = key.clone();
restore
.apply(suspend(move || lookup(k)))
.exit::<E>()
.flat_map(move |exit| with_now(move |now| self.finish(key, &slot, exit, now)))
}
}
}
fn begin(&self, key: &K, now: Instant) -> Step<V, E> {
let mut st = lock(&self.state);
let found = match st.entries.get(key) {
Some(Entry::Pending(slot)) => Some(Step::Wait(Arc::clone(slot))),
Some(Entry::Ready { exit, at, .. }) if fresh(self.ttl, *at, now) => {
Some(Step::Hit(exit.clone()))
}
Some(Entry::Ready { .. }) => {
st.remove(key);
None
}
None => None,
};
match found {
Some(Step::Hit(r)) => {
st.hits += 1;
st.touch(key);
Step::Hit(r)
}
Some(step) => {
st.misses += 1;
step
}
None => {
st.misses += 1;
let slot = Slot::new();
st.entries
.insert(key.clone(), Entry::Pending(Arc::clone(&slot)));
Step::Lead(slot)
}
}
}
fn finish(self, key: K, slot: &Pending<V, E>, exit: Exit<V, E>, now: Instant) -> Effect<V, E, R> {
let r = match exit {
Exit::Success(v) => Ok(v),
Exit::Failure(c) => Err(c),
};
let kind = ExitKind::of_result(&r);
{
let mut st = lock(&self.state);
let ours = matches!(st.entries.get(&key), Some(Entry::Pending(s)) if Arc::ptr_eq(s, slot));
if ours {
st.entries.remove(&key);
if matches!(kind, ExitKind::Success | ExitKind::Failure) {
st.insert_ready(key, r.clone(), now, self.capacity);
}
}
}
slot.complete(match kind {
ExitKind::Interrupt => Outcome::Retry,
_ => Outcome::Done(r.clone()),
});
Effect::pure(r)
}
pub fn get_if_present<E2: Send + 'static, R2: Send + Sync + 'static>(
&self,
key: &K,
) -> Effect<Option<V>, E2, R2> {
let this = self.clone();
let key = key.clone();
with_now(move |now| {
let st = lock(&this.state);
let v = match st.entries.get(&key) {
Some(Entry::Ready {
exit: Ok(v), at, ..
}) if fresh(this.ttl, *at, now) => Some(v.clone()),
_ => None,
};
Effect::pure(Ok(v))
})
}
pub fn contains<E2: Send + 'static, R2: Send + Sync + 'static>(
&self,
key: &K,
) -> Effect<bool, E2, R2> {
let this = self.clone();
let key = key.clone();
with_now(move |now| {
let st = lock(&this.state);
let present = match st.entries.get(&key) {
Some(Entry::Pending(_)) => true,
Some(Entry::Ready { at, .. }) => fresh(this.ttl, *at, now),
None => false,
};
Effect::pure(Ok(present))
})
}
pub fn set<E2: Send + 'static, R2: Send + Sync + 'static>(
&self,
key: K,
value: V,
) -> Effect<(), E2, R2> {
let this = self.clone();
with_now(move |now| {
lock(&this.state).insert_ready(key, Ok(value), now, this.capacity);
Effect::pure(Ok(()))
})
}
pub fn refresh(&self, key: K) -> Effect<(), E, R> {
let this = self.clone();
let lookup = Arc::clone(&self.lookup);
let k = key.clone();
suspend(move || lookup(k)).flat_map(move |v| this.set(key, v))
}
pub fn invalidate<E2: Send + 'static, R2: Send + Sync + 'static>(
&self,
key: &K,
) -> Effect<(), E2, R2> {
let state = Arc::clone(&self.state);
let key = key.clone();
sync(move || lock(&state).remove(&key))
}
pub fn invalidate_all<E2: Send + 'static, R2: Send + Sync + 'static>(
&self,
) -> Effect<(), E2, R2> {
let state = Arc::clone(&self.state);
sync(move || {
let mut st = lock(&state);
st.entries.clear();
st.lru.clear();
})
}
pub fn size<E2: Send + 'static, R2: Send + Sync + 'static>(&self) -> Effect<usize, E2, R2> {
let state = Arc::clone(&self.state);
sync(move || lock(&state).lru.len())
}
pub fn stats<E2: Send + 'static, R2: Send + Sync + 'static>(&self) -> Effect<CacheStats, E2, R2> {
let state = Arc::clone(&self.state);
sync(move || {
let st = lock(&state);
CacheStats {
hits: st.hits,
misses: st.misses,
size: st.lru.len(),
}
})
}
}