1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use std::convert::Infallible;

use async_trait::async_trait;
use moka::future::Cache;

use crate::{
    session::{SessionId, SessionRecord},
    Session, SessionStore,
};

/// A session store that uses Moka, a fast and concurrent caching library.
#[derive(Debug, Clone)]
pub struct MokaStore {
    cache: Cache<SessionId, SessionRecord>,
}

impl MokaStore {
    /// Create a new Moka store with the provided maximum capacity.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tower_sessions::{MemoryStore, MokaStore};
    /// let session_store = MokaStore::new(Some(2_000));
    /// ```
    pub fn new(max_capacity: Option<u64>) -> Self {
        // it would be useful to expose more of the CacheBuilder options to the user,
        // but for now this is the most important one
        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 {
    type Error = Infallible;

    async fn save(&self, session_record: &SessionRecord) -> Result<(), Self::Error> {
        self.cache
            .insert(session_record.id(), session_record.clone())
            .await;
        Ok(())
    }

    async fn load(&self, session_id: &SessionId) -> Result<Option<Session>, Self::Error> {
        Ok(self.cache.get(session_id).await.map(Into::into))
    }

    async fn delete(&self, session_id: &SessionId) -> Result<(), Self::Error> {
        self.cache.invalidate(session_id).await;
        Ok(())
    }
}