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
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
//! An in-memory implementationof a session store.
//!
//! This module provides [MemoryStore], an implementation of [Store]
//! to be used for testing and development. It is not optimized for production
//! and thus you should use another store to use it in the real world.

use std::{
	collections::HashMap,
	time::{
		Duration,
		Instant,
	},
};

use rocket::tokio::sync::{
	Mutex,
	RwLock,
};

use crate::{
	SessionResult,
	Store,
};

/// An in memory implementation of a session store using hashmaps.
/// Do note that this implementation is just for testing purposes,
/// and should not be used in any real world application.
pub struct MemoryStore<T> {
	map: RwLock<HashMap<String, Mutex<MemoryStoreFrame<T>>>>,
}

struct MemoryStoreFrame<T> {
	value: T,
	expiry: Instant,
}

impl<T> Default for MemoryStore<T> {
	fn default() -> Self {
		Self::new()
	}
}

impl<T> MemoryStore<T> {
	/// Create a new in-memory store
	pub fn new() -> Self {
		Self {
			map: RwLock::default(),
		}
	}
}

#[rocket::async_trait]
impl<T> Store for MemoryStore<T>
where
	T: Send + Sync + Clone,
{
	type Value = T;

	async fn get(&self, id: &str) -> SessionResult<Option<Self::Value>> {
		let lock = self.map.read().await;
		if let Some(frame) = lock.get(id) {
			let frame_lock = frame.lock().await;
			if frame_lock
				.expiry
				.checked_duration_since(Instant::now())
				.is_some()
			{
				return Ok(Some(frame_lock.value.clone()));
			};
		};
		Ok(None)
	}

	async fn set(&self, id: &str, value: Self::Value, expiry: Duration) -> SessionResult<()> {
		let mut lock = self.map.write().await;
		let frame = MemoryStoreFrame {
			value,
			expiry: Instant::now() + expiry,
		};
		lock.insert(id.into(), Mutex::new(frame));

		Ok(())
	}

	async fn touch(&self, id: &str, duration: Duration) -> SessionResult<()> {
		let lock = self.map.read().await;
		if let Some(frame) = lock.get(id) {
			let mut frame_lock = frame.lock().await;
			frame_lock.expiry = Instant::now() + duration;
		};
		Ok(())
	}

	async fn remove(&self, id: &str) -> SessionResult<()> {
		let mut lock = self.map.write().await;
		lock.remove(id);

		Ok(())
	}
}