use async_trait::async_trait;
use redis::aio::ConnectionManager;
use wacore::store::traits::*;
use wacore::store::error::Result;
pub struct RedisStore {
client: ConnectionManager,
device_id: i32,
}
impl RedisStore {
pub async fn new(redis_url: &str) -> Result<Self> {
let client = redis::Client::open(redis_url)
.map_err(|e| StoreError::Connection(e.to_string()))?;
let conn = client.get_connection_manager().await
.map_err(|e| StoreError::Connection(e.to_string()))?;
Ok(Self {
client: conn,
device_id: 1,
})
}
}
#[async_trait]
impl SignalStore for RedisStore {
async fn put_identity(&self, address: &str, key: [u8; 32]) -> Result<()> {
let mut conn = self.client.clone();
let key_name = format!("identity:{}:{}", self.device_id, address);
redis::cmd("SET")
.arg(key_name)
.arg(&key[..])
.query_async(&mut conn)
.await
.map_err(|e| StoreError::Database(e.to_string()))?;
Ok(())
}
async fn load_identity(&self, address: &str) -> Result<Option<Vec<u8>>> {
let mut conn = self.client.clone();
let key_name = format!("identity:{}:{}", self.device_id, address);
let result: Option<Vec<u8>> = redis::cmd("GET")
.arg(key_name)
.query_async(&mut conn)
.await
.map_err(|e| StoreError::Database(e.to_string()))?;
Ok(result)
}
// Implement remaining SignalStore methods...
}
#[async_trait]
impl AppSyncStore for RedisStore {
// Implement all AppSyncStore methods...
}
#[async_trait]
impl ProtocolStore for RedisStore {
// Implement all ProtocolStore methods...
}
#[async_trait]
impl DeviceStore for RedisStore {
// Implement all DeviceStore methods...
}
// Backend is automatically implemented!