derive-redis-json

License License Cargo Documentation

A derive to store and retrieve JSON values in redis encoded using serde.

Example

Cargo.toml:

toml [dependencies] derive-redis-json = "0.1.1"

main.rs:

```rust use std::sync::Arc;

use anyhow::Result; use deadpoolredis::{redis::cmd, Pool as RedisPool}; use deriveredis_json::RedisJsonValue; use serde::{Deserialize, Serialize};

[derive(Serialize, Deserialize, RedisJsonValue)]

pub struct User { pub id: u64, pub name: String, }

pub async fn adduser( redispool: Arc, user: User, ) -> Result { let mut conn = redispool.get().await?; let res: usize = cmd("SADD") .arg("Users") .arg(&user) .queryasync(&mut conn) .await?;

Ok(res) }

pub async fn getusers( redispool: Arc, ) -> Result> { let mut conn = redispool.get().await?; let res: Vec = cmd("SMEMBERS").arg("Users").queryasync(&mut conn).await?;

Ok(res) } ```