use std::collections::BTreeMap;
use std::ops::{Deref, DerefMut, Range};
use std::time::{Duration, Instant};
use async_lock::{RwLockReadGuard, RwLockWriteGuard};
use rand::prelude::*;
#[derive(Debug)]
pub(crate) struct CacheEntry<V> {
value: V,
expiration: CacheExpiration,
}
impl<V> CacheEntry<V> {
pub fn new(value: V, expiration: CacheExpiration) -> Self {
Self { value, expiration }
}
pub fn expiration(&self) -> &CacheExpiration {
&self.expiration
}
pub(crate) fn expiration_mut(&mut self) -> &mut CacheExpiration {
&mut self.expiration
}
pub fn value(&self) -> &V {
&self.value
}
pub fn value_mut(&mut self) -> &mut V {
&mut self.value
}
pub fn into_inner(self) -> V {
self.value
}
}
#[derive(Debug)]
pub struct CacheExpiration {
instant: Option<Instant>,
}
impl CacheExpiration {
pub fn new<I>(instant: I) -> Self
where
I: Into<Instant>,
{
Self {
instant: Some(instant.into()),
}
}
pub fn none() -> Self {
Self { instant: None }
}
pub fn instant(&self) -> &Option<Instant> {
&self.instant
}
pub fn is_expired(&self) -> bool {
self.instant()
.map(|expiration| expiration < Instant::now())
.unwrap_or(false)
}
pub fn remaining(&self) -> Option<Duration> {
self.instant
.map(|i| i.saturating_duration_since(Instant::now()))
}
}
impl From<Instant> for CacheExpiration {
fn from(instant: Instant) -> Self {
Self::new(instant)
}
}
impl From<u64> for CacheExpiration {
fn from(millis: u64) -> Self {
Duration::from_millis(millis).into()
}
}
impl From<Duration> for CacheExpiration {
fn from(duration: Duration) -> Self {
Instant::now().checked_add(duration).unwrap().into()
}
}
impl From<Range<u64>> for CacheExpiration {
fn from(range: Range<u64>) -> Self {
rand::rng().random_range(range).into()
}
}
#[derive(Debug)]
pub struct CacheReadGuard<'a, K, V> {
pub(crate) entry: *const CacheEntry<V>,
pub(crate) _lock: RwLockReadGuard<'a, BTreeMap<K, CacheEntry<V>>>,
}
impl<K, V> CacheReadGuard<'_, K, V> {
pub fn expiration(&self) -> &CacheExpiration {
self.entry().expiration()
}
pub fn value(&self) -> &V {
self.entry().value()
}
fn entry(&self) -> &CacheEntry<V> {
unsafe { &*self.entry }
}
}
impl<K, V> Deref for CacheReadGuard<'_, K, V> {
type Target = V;
fn deref(&self) -> &Self::Target {
self.value()
}
}
unsafe impl<K, V> Send for CacheReadGuard<'_, K, V>
where
K: Sync,
V: Sync,
{
}
unsafe impl<K, V> Sync for CacheReadGuard<'_, K, V>
where
K: Sync,
V: Sync,
{
}
#[derive(Debug)]
pub struct CacheWriteGuard<'a, K, V> {
pub(crate) entry: *mut CacheEntry<V>,
pub(crate) _lock: RwLockWriteGuard<'a, BTreeMap<K, CacheEntry<V>>>,
}
impl<K, V> CacheWriteGuard<'_, K, V> {
pub fn expiration(&self) -> &CacheExpiration {
self.entry().expiration()
}
pub fn expiration_mut(&mut self) -> &mut CacheExpiration {
self.entry_mut().expiration_mut()
}
pub fn value(&self) -> &V {
self.entry().value()
}
pub fn value_mut(&mut self) -> &mut V {
self.entry_mut().value_mut()
}
fn entry(&self) -> &CacheEntry<V> {
unsafe { &*self.entry }
}
fn entry_mut(&mut self) -> &mut CacheEntry<V> {
unsafe { &mut *self.entry }
}
}
impl<K, V> Deref for CacheWriteGuard<'_, K, V> {
type Target = V;
fn deref(&self) -> &Self::Target {
self.value()
}
}
impl<K, V> DerefMut for CacheWriteGuard<'_, K, V> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.value_mut()
}
}
unsafe impl<K, V> Send for CacheWriteGuard<'_, K, V>
where
K: Send,
V: Send,
{
}
unsafe impl<K, V> Sync for CacheWriteGuard<'_, K, V>
where
K: Sync,
V: Sync,
{
}