pub mod config;
pub mod metrics;
pub mod pipeline;
pub mod result;
pub(crate) mod runtime;
pub mod wire;
pub use config::{IdempotentCounterCfg, IdempotentTtlMode};
pub use metrics::{IdempotentCounterMetrics, IdempotentCounterSnapshot};
pub use pipeline::{IdempotentPipelineSession, IdempotentTicket};
pub use result::{IdempotentCounterError, IdempotentRejection};
use crate::client::RedisClient;
use crate::error::Result;
use runtime::{IdempotentCounterRuntime, Operation};
impl RedisClient {
fn idempotent_runtime(&self) -> Result<&IdempotentCounterRuntime> {
if let Some(runtime) = self.idempotent.get() {
return Ok(runtime);
}
let snapshot = self
.config()
.idempotent_counter
.clone()
.unwrap_or_default()
.snapshot(self.profile())?;
let _ = self.idempotent.set(IdempotentCounterRuntime::new(snapshot));
Ok(self.idempotent.get().expect("idempotent 运行时刚刚初始化"))
}
pub async fn incr_idempotent(&self, key: &str, nonce: &str) -> Result<i64> {
self.incr_by_idempotent(key, 1, nonce).await
}
pub async fn incr_by_idempotent(&self, key: &str, delta: i64, nonce: &str) -> Result<i64> {
self.idempotent_runtime()?
.execute_long(self, Operation::StringIncrement, key, b"", delta, nonce)
.await
}
pub async fn decr_idempotent(&self, key: &str, nonce: &str) -> Result<i64> {
self.decr_by_idempotent(key, 1, nonce).await
}
pub async fn decr_by_idempotent(&self, key: &str, delta: i64, nonce: &str) -> Result<i64> {
self.idempotent_runtime()?
.execute_long(self, Operation::StringDecrement, key, b"", delta, nonce)
.await
}
pub async fn h_incr_idempotent(&self, key: &str, field: &str, nonce: &str) -> Result<i64> {
self.h_incr_by_idempotent(key, field, 1, nonce).await
}
pub async fn h_incr_by_idempotent(
&self,
key: &str,
field: &str,
delta: i64,
nonce: &str,
) -> Result<i64> {
self.idempotent_runtime()?
.execute_long(
self,
Operation::HashIncrement,
key,
field.as_bytes(),
delta,
nonce,
)
.await
}
pub async fn h_decr_idempotent(&self, key: &str, field: &str, nonce: &str) -> Result<i64> {
self.h_decr_by_idempotent(key, field, 1, nonce).await
}
pub async fn h_decr_by_idempotent(
&self,
key: &str,
field: &str,
delta: i64,
nonce: &str,
) -> Result<i64> {
self.idempotent_runtime()?
.execute_long(
self,
Operation::HashDecrement,
key,
field.as_bytes(),
delta,
nonce,
)
.await
}
pub async fn z_incr_by_idempotent<V>(
&self,
key: &str,
member: V,
delta: f64,
nonce: &str,
) -> Result<f64>
where
V: redis::ToSingleRedisArg + Send + Sync,
{
let mut args = redis::ToRedisArgs::to_redis_args(&member);
let member = args.pop().ok_or_else(|| {
crate::error::NasaRedisError::IdempotentProtocol(
"ZSet member 未生成单个 Redis 参数".to_owned(),
)
})?;
if !args.is_empty() {
return Err(crate::error::NasaRedisError::IdempotentProtocol(
"ZSet member 生成了多个 Redis 参数".to_owned(),
));
}
self.idempotent_runtime()?
.execute_double(self, key, &member, delta, nonce)
.await
}
pub fn idempotent_counter_snapshot(&self) -> Option<IdempotentCounterSnapshot> {
self.idempotent
.get()
.map(|runtime| runtime.metrics().snapshot())
}
pub async fn prepare_idempotent_counter(&self) -> Result<()> {
self.idempotent_runtime()?.prepare(self).await
}
}