use async_trait::async_trait;
use moka::future::Cache;
use time::OffsetDateTime;
use tower_sessions_core::{
session::{Id, Record},
session_store, SessionStore,
};
#[derive(Debug, Clone)]
pub struct MokaStore {
cache: Cache<Id, (Record, OffsetDateTime)>,
}
impl MokaStore {
pub fn new(max_capacity: Option<u64>) -> Self {
let cache_builder = match max_capacity {
Some(capacity) => Cache::builder().max_capacity(capacity),
None => Cache::builder(),
};
Self {
cache: cache_builder.build(),
}
}
}
#[async_trait]
impl SessionStore for MokaStore {
async fn save(&self, record: &Record) -> session_store::Result<()> {
self.cache
.insert(record.id, (record.clone(), record.expiry_date))
.await;
Ok(())
}
async fn load(&self, session_id: &Id) -> session_store::Result<Option<Record>> {
Ok(self
.cache
.get(session_id)
.await
.filter(|(_, expiry_date)| is_active(*expiry_date))
.map(|(session, _)| session))
}
async fn delete(&self, session_id: &Id) -> session_store::Result<()> {
self.cache.invalidate(session_id).await;
Ok(())
}
}
fn is_active(expiry_date: OffsetDateTime) -> bool {
expiry_date > OffsetDateTime::now_utc()
}