redis_om/hash_model/
async.rs1use super::cmds;
2use crate::{RedisModel, RedisSearchModel};
3use redis::aio::ConnectionLike;
4use redis::{FromRedisValue, RedisResult, ToRedisArgs};
5
6#[async_trait::async_trait]
8pub trait HashModel: RedisModel + RedisSearchModel + ToRedisArgs + FromRedisValue {
9 fn redis_prefix() -> &'static str {
12 <Self as RedisModel>::_prefix_key()
13 }
14
15 fn redissearch_schema() -> &'static str {
17 <Self as RedisSearchModel>::_REDIS_SEARCH_SCHEMA
18 }
19
20 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 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 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 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 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}