redis_om/hash_model/
async.rs

1use super::cmds;
2use crate::{RedisModel, RedisSearchModel};
3use redis::aio::ConnectionLike;
4use redis::{FromRedisValue, RedisResult, ToRedisArgs};
5
6/// Hash Object Model
7#[async_trait::async_trait]
8pub trait HashModel: RedisModel + RedisSearchModel + ToRedisArgs + FromRedisValue {
9    /// Get Redis key to be used in storing HashModel object.
10    /// This should by default that HashModel name in lowercase.
11    fn redis_prefix() -> &'static str {
12        <Self as RedisModel>::_prefix_key()
13    }
14
15    /// Redis search schema
16    fn redissearch_schema() -> &'static str {
17        <Self as RedisSearchModel>::_REDIS_SEARCH_SCHEMA
18    }
19
20    /// Save Self into redis database
21    async fn save<C>(&mut self, conn: &mut C) -> RedisResult<()>
22    where
23        C: ConnectionLike + Send,
24    {
25        self._ensure_pk();
26        let key = self._get_redis_key();
27        let cmd = cmds::save(key, self)?;
28        cmd.query_async(conn).await
29    }
30
31    /// Get a list of all primary keys for current type
32    async fn all_pks<C: ConnectionLike + Send>(
33        conn: &mut C,
34    ) -> RedisResult<redis::AsyncIter<'_, String>> {
35        cmds::all_pks::<Self>()?.iter_async(conn).await
36    }
37
38    /// Get a list of all primary keys for current type
39    async fn get<C, S>(pk: S, conn: &mut C) -> RedisResult<Self>
40    where
41        S: AsRef<str> + Send,
42        C: ConnectionLike + Send,
43    {
44        cmds::get::<Self>(pk)?.query_async(conn).await
45    }
46
47    /// Delete by given pk
48    async fn delete<S, C>(pk: S, conn: &mut C) -> RedisResult<()>
49    where
50        S: AsRef<str> + Send,
51        C: ConnectionLike + Send,
52    {
53        cmds::delete::<Self>(pk)?.query_async(conn).await
54    }
55
56    /// Expire Self at given duration
57    async fn expire<C>(&self, secs: usize, conn: &mut C) -> RedisResult<()>
58    where
59        C: ConnectionLike + Send,
60    {
61        self._expire_cmd(secs)?.query_async(conn).await
62    }
63}