use crate::redis::connection::RedisPool;
use crate::storage::OffsetStore;
#[derive(Clone)]
pub struct RedisOffsetStore {
pool: RedisPool,
}
impl RedisOffsetStore {
pub fn new(pool: RedisPool) -> Self {
Self { pool }
}
fn hash_key(&self) -> String {
self.pool.key("offsets")
}
}
impl OffsetStore for RedisOffsetStore {
fn alloc(&self, topic: &str) -> i64 {
let key = self.hash_key();
self.pool.sync_cmd(|c| {
redis::cmd("HINCRBY")
.arg(&key)
.arg(topic)
.arg(1)
.query::<i64>(c)
})
}
fn head(&self, topic: &str) -> i64 {
let key = self.hash_key();
self.pool
.sync_cmd(|c| {
redis::cmd("HGET")
.arg(&key)
.arg(topic)
.query::<Option<i64>>(c)
})
.unwrap_or(0)
}
fn remove(&self, topic: &str) {
let key = self.hash_key();
let _: () = self
.pool
.sync_cmd(|c| redis::cmd("HDEL").arg(&key).arg(topic).query::<()>(c));
}
}