cache_any/caches/
redis.rs

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::fmt::{Debug, Formatter};
use std::sync::Arc;
use redis::AsyncCommands;
use tokio::sync::RwLock;
use crate::{Cache, Cacheable};

/// Cache using redis.
/// 
/// It uses [`redis::Client`] to connect to redis.
/// Feature `redis` must be enabled.
/// 
/// A custom map should be specified. It will be used as the map of the redis key.
/// 
/// [`RedisCache`] implements [`Cache`]. See [`Cache`] for more details.
/// 
/// ## Example
/// 
/// ```rust,ignore
/// let client = redis::Client::open("redis://127.0.0.1:6379/").unwrap();
/// 
/// // `aaa` is the hash map name
/// let map = "aaa";
/// let cache = RedisCache::new(client, map).await.unwrap(); 
/// 
/// cache.set("a", 1).await.unwrap();
/// assert_eq!(cache.get::<u8>("a").await.unwrap().unwrap(), 1);
/// 
/// // Redis Data ('aaa' is a redis hash map):
/// // aaa: a -> Encoded(1)
/// ```
#[derive(Debug, Clone)]
pub struct RedisCache {
    inner: Arc<RwLock<Inner>>,
}

impl RedisCache {
    pub async fn new<S: ToString>(client: redis::Client, map: S) -> anyhow::Result<Self> {
        let conn = client.get_multiplexed_async_connection().await?;

        Ok(Self {
            inner: Arc::new(RwLock::new(Inner {
                map: Arc::new(map.to_string()),
                client,
                conn,
            }))
        })
    }
}

#[async_trait::async_trait]
impl Cache for RedisCache {
    async fn get<T: Cacheable + Send + Sync>(&self, key: &str) -> anyhow::Result<Option<T>> {
        let val: Option<String> = {
            let mut inner = self.inner.write().await;
            let map = inner.map.clone();
            inner.conn.hget(map, key).await?
        };

        val.map(|val| T::from_hex(&val))
            .transpose()
    }

    async fn set<T: Cacheable + Send + Sync>(&self, key: &str, value: T) -> anyhow::Result<()> {
        let val = value.to_hex();
        let mut inner = self.inner.write().await;
        let map = inner.map.clone();
        inner.conn.hset(map, key, val).await?;

        Ok(())
    }

    async fn delete(&self, key: &str) -> anyhow::Result<()> {
        let mut inner = self.inner.write().await;
        let map = inner.map.clone();
        inner.conn.hdel(map, key).await?;

        Ok(())
    }

    async fn len(&self) -> anyhow::Result<usize> {
        let mut inner = self.inner.write().await;
        let map = inner.map.clone();
        let len: u64 = inner.conn.hlen(map).await?;

        Ok(len as usize)
    }
}

struct Inner {
    map: Arc<String>,
    client: redis::Client,
    conn: redis::aio::MultiplexedConnection,
}

impl Debug for Inner {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RedisCache.Inner")
            .field("client", &self.client)
            .field("map", &self.map)
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use redis::Client;
    use super::*;

    #[tokio::test]
    async fn test_redis_cache() -> anyhow::Result<()> {
        let client = Client::open("redis://127.0.0.1:6379/")?;
        let cache = RedisCache::new(client, "aaa").await?;

        assert_eq!(cache.get::<u8>("none").await.unwrap(), None);
        cache.set("a", String::from("aaaaaa")).await?;
        assert_eq!(cache.get::<String>("a").await.unwrap().unwrap(), String::from("aaaaaa"));
        cache.set("a", String::from("aaaaaaa")).await?;
        assert_eq!(cache.get::<String>("a").await.unwrap().unwrap(), String::from("aaaaaaa"));

        cache.set("b", String::from("bbbbbb")).await?;
        assert_eq!(cache.get::<String>("b").await.unwrap().unwrap(), String::from("bbbbbb"));
        cache.set("c", 1).await?;
        assert_eq!(cache.get::<usize>("c").await.unwrap().unwrap(), 1);

        let cloned = cache.clone();
        assert_eq!(cloned.get::<u8>("none").await.unwrap(), None);
        cloned.set("a", String::from("aaaaaa")).await?;
        assert_eq!(cloned.get::<String>("a").await.unwrap().unwrap(), String::from("aaaaaa"));
        cloned.set("a", String::from("aaaaaaa")).await?;
        assert_eq!(cloned.get::<String>("a").await.unwrap().unwrap(), String::from("aaaaaaa"));

        cloned.set("b", String::from("bbbbbb")).await?;
        assert_eq!(cloned.get::<String>("b").await.unwrap().unwrap(), String::from("bbbbbb"));
        cloned.set("c", 1).await?;
        assert_eq!(cloned.get::<usize>("c").await.unwrap().unwrap(), 1);

        println!("{:?}", cache);
        println!("{:?}", cloned);
        println!("size = {}", cloned.len().await?);
        Ok(())
    }
}