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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use super::Driver;
use ensemble::{types::DateTime, Model};
use serde::{de::DeserializeOwned, Serialize};
use std::time::Duration;

#[derive(Debug, Model)]
#[ensemble(table = "cache")]
struct CacheEntry {
	#[model(primary)]
	pub key: String,
	pub value: String,
	pub expiration: Option<DateTime>,
}

#[allow(clippy::module_name_repetitions)]
/// A driver that stores cache entries in a database.
pub struct DatabaseDriver;

impl Driver for DatabaseDriver {
	type Config = ();
	type Error = Error;

	async fn new((): Self::Config) -> Result<Self, Self::Error> {
		Ok(Self)
	}

	async fn get<T: DeserializeOwned>(&self, key: &str) -> Result<Option<T>, Self::Error> {
		let Some(entry) = CacheEntry::query()
			.r#where("key", '=', key)
			.where_group(|query| {
				query
					.where_null("expiration")
					.or_where("expiration", '>', DateTime::now())
			})
			.first::<CacheEntry>()
			.await?
		else {
			return Ok(None);
		};

		Ok(Some(serde_json::from_str::<T>(&entry.value)?))
	}

	async fn has(&self, key: &str) -> Result<bool, Self::Error> {
		let count = CacheEntry::query()
			.r#where("key", '=', key)
			.where_null("expiration")
			.or_where("expiration", '>', DateTime::now())
			.count()
			.await?;

		Ok(count != 0)
	}

	async fn put<T: Serialize + Sync>(
		&mut self,
		key: &str,
		value: &T,
		duration: Option<Duration>,
	) -> Result<(), Self::Error> {
		let expiration = duration.map(|duration| DateTime::now() + duration);

		// TODO: This should be a single query.
		CacheEntry::query()
			.r#where("key", '=', key)
			.delete()
			.await?;

		CacheEntry::create(CacheEntry {
			expiration,
			key: key.to_string(),
			value: serde_json::to_string(value)?,
		})
		.await?;

		Ok(())
	}

	async fn forget(&mut self, key: &str) -> Result<(), Self::Error> {
		CacheEntry::query()
			.r#where("key", '=', key)
			.delete()
			.await?;

		Ok(())
	}

	async fn flush(&mut self) -> Result<(), Self::Error> {
		CacheEntry::query().delete().await?;

		Ok(())
	}
}

#[derive(Debug, thiserror::Error)]
pub enum Error {
	#[error(transparent)]
	Database(#[from] ensemble::Error),
	#[error(transparent)]
	Serialize(#[from] serde_json::Error),
}

#[cfg(test)]
mod tests {
	use std::env;

	use super::*;
	use crate::Cache;

	#[tokio::test]
	async fn test_database_driver() {
		ensemble::setup(&env::var("DATABASE_URL").expect("DATABASE_URL not set")).unwrap();

		let mut cache = Cache::<DatabaseDriver>::new(()).await.unwrap();

		assert_eq!(cache.get::<String>("foo").await.unwrap(), None);
		assert!(!cache.has("foo").await.unwrap());

		cache
			.put("foo", &"bar".to_string(), Duration::from_secs(10))
			.await
			.unwrap();

		assert_eq!(cache.get("foo").await.unwrap(), Some("bar".to_string()));
		assert!(cache.has("foo").await.unwrap());

		cache.forget("foo").await.unwrap();

		assert_eq!(cache.get::<String>("foo").await.unwrap(), None);
		assert!(!cache.has("foo").await.unwrap());

		cache
			.put("foo", &"bar".to_string(), Duration::from_secs(1))
			.await
			.unwrap();
	}
}