use crate::{DbError, RedisClient, Result};
use serde::{de::DeserializeOwned, Serialize};
pub trait RedisCodec {
fn encode<T: Serialize>(&self, value: &T) -> Result<Vec<u8>>;
fn decode<T: DeserializeOwned>(&self, bytes: &[u8]) -> Result<T>;
}
#[derive(Debug, Default, Clone, Copy)]
pub struct JsonCodec;
impl RedisCodec for JsonCodec {
fn encode<T: Serialize>(&self, value: &T) -> Result<Vec<u8>> {
serde_json::to_vec(value)
.map_err(|error| DbError::SerializationError(format!("Redis JSON 序列化失败: {error}")))
}
fn decode<T: DeserializeOwned>(&self, bytes: &[u8]) -> Result<T> {
serde_json::from_slice(bytes).map_err(|error| {
DbError::DeserializationError(format!("Redis JSON 反序列化失败: {error}"))
})
}
}
impl RedisClient {
pub async fn set_typed<C, T>(
&self,
codec: &C,
key: impl AsRef<[u8]>,
value: &T,
ttl_seconds: Option<i64>,
) -> Result<()>
where
C: RedisCodec,
T: Serialize,
{
let bytes = codec.encode(value)?;
let key = self.key_for(key);
match ttl_seconds {
Some(ttl) => self.setex_bytes(key, ttl, bytes).await,
None => self.set_bytes(key, bytes).await,
}
}
pub async fn get_typed<C, T>(&self, codec: &C, key: impl AsRef<[u8]>) -> Result<Option<T>>
where
C: RedisCodec,
T: DeserializeOwned,
{
match self.get_bytes(self.key_for(key)).await? {
Some(bytes) => Ok(Some(codec.decode(&bytes)?)),
None => Ok(None),
}
}
pub async fn set_json<T: Serialize>(
&self,
key: impl AsRef<[u8]>,
value: &T,
ttl_seconds: Option<i64>,
) -> Result<()> {
self.set_typed(&JsonCodec, key, value, ttl_seconds).await
}
pub async fn get_json<T: DeserializeOwned>(&self, key: impl AsRef<[u8]>) -> Result<Option<T>> {
self.get_typed(&JsonCodec, key).await
}
pub async fn hset_json<T: Serialize>(
&self,
key: impl AsRef<[u8]>,
field: impl AsRef<[u8]>,
value: &T,
) -> Result<bool> {
let bytes = JsonCodec.encode(value)?;
self.hset_bytes(self.key_for(key), field, bytes).await
}
pub async fn hget_json<T: DeserializeOwned>(
&self,
key: impl AsRef<[u8]>,
field: impl AsRef<[u8]>,
) -> Result<Option<T>> {
match self.hget_bytes(self.key_for(key), field).await? {
Some(bytes) => Ok(Some(JsonCodec.decode(&bytes)?)),
None => Ok(None),
}
}
}
#[cfg(test)]
#[allow(clippy::expect_used, clippy::unwrap_used)]
mod tests {
use super::*;
#[derive(Debug, PartialEq, serde::Serialize, serde::Deserialize)]
struct Payload {
id: u64,
name: String,
}
#[test]
fn json_codec_round_trips() {
let codec = JsonCodec;
let payload = Payload {
id: 7,
name: "redis".to_string(),
};
let bytes = codec.encode(&payload).expect("编码应成功");
let decoded: Payload = codec.decode(&bytes).expect("解码应成功");
assert_eq!(decoded, payload);
}
#[test]
fn json_codec_reports_corrupt_payload() {
let error = JsonCodec
.decode::<Payload>(b"not-json")
.expect_err("损坏 JSON 必须失败");
assert!(matches!(error, DbError::DeserializationError(_)));
}
}