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
138
139
140
141
142
use std::time::Duration;

use async_trait::async_trait;

use crate::Result;

#[async_trait]
pub trait MemoryDB: Send + Sync {
    async fn set(&self, key: &str, value: &str) -> Result<()>;

    async fn get(&self, key: &str) -> Result<Option<String>>;

    async fn get_del(&self, key: &str) -> Result<Option<String>>;

    async fn get_ex(&self, key: &str, ttl: &Duration) -> Result<Option<String>>;

    async fn set_ex(&self, key: &str, value: &str, ttl: &Duration) -> Result<()>;

    async fn del(&self, key: &str) -> Result<bool>;

    async fn expire(&self, key: &str, ttl: i64) -> Result<bool>;

    async fn flush(&self) -> Result<()>;
}

#[cfg(test)]
mod tests {
    use std::time::Duration;
    use tokio::time::sleep;

    use super::*;
    use crate::memorydb::default::DefaultBackend;

    #[cfg(feature = "redis")]
    async fn setup_redis() -> impl MemoryDB {
        crate::memorydb::redis::RedisBackend::new("redis://127.0.0.1:6379/0")
            .await
            .unwrap()
    }

    fn setup_default() -> impl MemoryDB {
        DefaultBackend::new()
    }

    #[tokio::test]
    async fn test_normal() {
        test_normal_fn("default", setup_default()).await;
        #[cfg(feature = "redis")]
        test_normal_fn("redis", setup_redis().await).await;
    }

    async fn test_normal_fn(name: &str, r: impl MemoryDB) {
        let key = "_actix_cloud_key1";
        let value1 = "value1";
        let value2 = "value2";

        println!("Backend: {}", name);

        let _ = r.del(key).await;

        assert_eq!(r.get(key).await.unwrap(), None);

        r.set(key, value1).await.unwrap();
        assert_eq!(r.get(key).await.unwrap().unwrap(), value1);
        r.set(key, value2).await.unwrap();
        assert_eq!(r.get(key).await.unwrap().unwrap(), value2);

        assert_eq!(r.del(key).await.unwrap(), true);
        assert_eq!(r.del(key).await.unwrap(), false);
        assert_eq!(r.get(key).await.unwrap(), None);

        r.set("_actix_cloud_key1-1-1", value1).await.unwrap();
        r.set("_actix_cloud_key1-1-2", value1).await.unwrap();
        r.set("_actix_cloud_key1-2-1", value1).await.unwrap();
    }

    #[tokio::test]
    async fn test_ex() {
        test_ex_fn("default", setup_default()).await;
        #[cfg(feature = "redis")]
        test_ex_fn("redis", setup_redis().await).await;
    }

    async fn test_ex_fn(name: &str, r: impl MemoryDB) {
        let key = "_actix_cloud_key2";
        let value = "value";

        println!("Backend: {}", name);

        let _ = r.del(key).await;

        r.set(key, value).await.unwrap();
        assert_eq!(r.get_del(key).await.unwrap().unwrap(), value);
        assert_eq!(r.get(key).await.unwrap(), None);

        r.set_ex(key, value, &Duration::from_secs(2)).await.unwrap();
        assert_eq!(r.get(key).await.unwrap().unwrap(), value);
        sleep(Duration::from_secs(1)).await;
        assert_eq!(
            r.get_ex(key, &Duration::from_secs(2))
                .await
                .unwrap()
                .unwrap(),
            value
        );
        sleep(Duration::from_secs(1)).await;
        assert_eq!(r.get(key).await.unwrap().unwrap(), value);
        sleep(Duration::from_secs(2)).await;
        assert_eq!(r.get(key).await.unwrap(), None);
    }

    #[tokio::test]
    async fn test_expire() {
        test_expire_fn("default", setup_default()).await;
        #[cfg(feature = "redis")]
        test_expire_fn("redis", setup_redis().await).await;
    }

    async fn test_expire_fn(name: &str, r: impl MemoryDB) {
        let key = "_actix_cloud_key3";
        let value = "value";

        println!("Backend: {}", name);

        let _ = r.del(key).await;

        r.set(key, value).await.unwrap();
        assert_eq!(r.get(key).await.unwrap().unwrap(), value);
        assert_eq!(r.expire(key, 1).await.unwrap(), true);
        sleep(Duration::from_secs(2)).await;
        assert_eq!(r.get(key).await.unwrap(), None);
        assert_eq!(r.expire(key, 1).await.unwrap(), false);

        r.set_ex(key, value, &Duration::from_secs(1)).await.unwrap();
        assert_eq!(r.expire(key, 3).await.unwrap(), true);
        sleep(Duration::from_secs(2)).await;
        assert_eq!(r.get(key).await.unwrap().unwrap(), value);
        assert_eq!(r.expire(key, -1).await.unwrap(), true);
        assert_eq!(r.get(key).await.unwrap(), None);
        assert_eq!(r.expire(key, 0).await.unwrap(), false);
    }
}