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
use super::Driver;
use serde::{de::DeserializeOwned, Serialize};
use std::{convert::Infallible, time::Duration};

#[allow(clippy::module_name_repetitions)]
/// A driver that does nothing.
pub struct NullDriver;

impl Driver for NullDriver {
	type Config = ();
	type Error = Infallible;

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

	async fn get<T: DeserializeOwned>(&self, _key: &str) -> Result<Option<T>, Self::Error> {
		Ok(None)
	}

	async fn has(&self, _key: &str) -> Result<bool, Self::Error> {
		Ok(false)
	}

	async fn put<T: Serialize + Sync>(
		&mut self,
		_: &str,
		_: &T,
		_: Option<Duration>,
	) -> Result<(), Self::Error> {
		Ok(())
	}

	async fn forget(&mut self, _: &str) -> Result<(), Self::Error> {
		Ok(())
	}

	async fn flush(&mut self) -> Result<(), Self::Error> {
		Ok(())
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::Cache;

	#[tokio::test]
	async fn test_null_driver() {
		let mut cache = Cache::<NullDriver>::new(()).await.unwrap();

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

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

		assert_eq!(cache.get::<String>("foo").await.unwrap(), None);
		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());
	}
}