mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-11-01 19:42:27 +08:00
refactor(redis): add redis commands required for success rate based routing (#5610)
This commit is contained in:
@ -15,7 +15,7 @@ use common_utils::{
|
|||||||
};
|
};
|
||||||
use error_stack::{report, ResultExt};
|
use error_stack::{report, ResultExt};
|
||||||
use fred::{
|
use fred::{
|
||||||
interfaces::{HashesInterface, KeysInterface, SetsInterface, StreamsInterface},
|
interfaces::{HashesInterface, KeysInterface, ListInterface, SetsInterface, StreamsInterface},
|
||||||
prelude::RedisErrorKind,
|
prelude::RedisErrorKind,
|
||||||
types::{
|
types::{
|
||||||
Expiration, FromRedis, MultipleIDs, MultipleKeys, MultipleOrderedPairs, MultipleStrings,
|
Expiration, FromRedis, MultipleIDs, MultipleKeys, MultipleOrderedPairs, MultipleStrings,
|
||||||
@ -371,6 +371,19 @@ impl super::RedisConnectionPool {
|
|||||||
Ok(hsetnx)
|
Ok(hsetnx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[instrument(level = "DEBUG", skip(self))]
|
||||||
|
pub async fn increment_field_in_hash(
|
||||||
|
&self,
|
||||||
|
key: &str,
|
||||||
|
field: &str,
|
||||||
|
increment: i64,
|
||||||
|
) -> CustomResult<usize, errors::RedisError> {
|
||||||
|
self.pool
|
||||||
|
.hincrby(self.add_prefix(key), field, increment)
|
||||||
|
.await
|
||||||
|
.change_context(errors::RedisError::IncrementHashFieldFailed)
|
||||||
|
}
|
||||||
|
|
||||||
#[instrument(level = "DEBUG", skip(self))]
|
#[instrument(level = "DEBUG", skip(self))]
|
||||||
pub async fn hscan(
|
pub async fn hscan(
|
||||||
&self,
|
&self,
|
||||||
@ -630,6 +643,55 @@ impl super::RedisConnectionPool {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[instrument(level = "DEBUG", skip(self))]
|
||||||
|
pub async fn append_elements_to_list<V>(
|
||||||
|
&self,
|
||||||
|
key: &str,
|
||||||
|
elements: V,
|
||||||
|
) -> CustomResult<(), errors::RedisError>
|
||||||
|
where
|
||||||
|
V: TryInto<MultipleValues> + Debug + Send,
|
||||||
|
V::Error: Into<fred::error::RedisError> + Send,
|
||||||
|
{
|
||||||
|
self.pool
|
||||||
|
.rpush(self.add_prefix(key), elements)
|
||||||
|
.await
|
||||||
|
.change_context(errors::RedisError::AppendElementsToListFailed)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[instrument(level = "DEBUG", skip(self))]
|
||||||
|
pub async fn get_list_elements(
|
||||||
|
&self,
|
||||||
|
key: &str,
|
||||||
|
start: i64,
|
||||||
|
stop: i64,
|
||||||
|
) -> CustomResult<Vec<String>, errors::RedisError> {
|
||||||
|
self.pool
|
||||||
|
.lrange(self.add_prefix(key), start, stop)
|
||||||
|
.await
|
||||||
|
.change_context(errors::RedisError::GetListElementsFailed)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[instrument(level = "DEBUG", skip(self))]
|
||||||
|
pub async fn get_list_length(&self, key: &str) -> CustomResult<usize, errors::RedisError> {
|
||||||
|
self.pool
|
||||||
|
.llen(self.add_prefix(key))
|
||||||
|
.await
|
||||||
|
.change_context(errors::RedisError::GetListLengthFailed)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[instrument(level = "DEBUG", skip(self))]
|
||||||
|
pub async fn lpop_list_elements(
|
||||||
|
&self,
|
||||||
|
key: &str,
|
||||||
|
count: Option<usize>,
|
||||||
|
) -> CustomResult<Vec<String>, errors::RedisError> {
|
||||||
|
self.pool
|
||||||
|
.lpop(self.add_prefix(key), count)
|
||||||
|
.await
|
||||||
|
.change_context(errors::RedisError::PopListElementsFailed)
|
||||||
|
}
|
||||||
|
|
||||||
// Consumer Group API
|
// Consumer Group API
|
||||||
|
|
||||||
#[instrument(level = "DEBUG", skip(self))]
|
#[instrument(level = "DEBUG", skip(self))]
|
||||||
|
|||||||
@ -68,4 +68,14 @@ pub enum RedisError {
|
|||||||
OnMessageError,
|
OnMessageError,
|
||||||
#[error("Got an unknown result from redis")]
|
#[error("Got an unknown result from redis")]
|
||||||
UnknownResult,
|
UnknownResult,
|
||||||
|
#[error("Failed to append elements to list in Redis")]
|
||||||
|
AppendElementsToListFailed,
|
||||||
|
#[error("Failed to get list elements in Redis")]
|
||||||
|
GetListElementsFailed,
|
||||||
|
#[error("Failed to get length of list")]
|
||||||
|
GetListLengthFailed,
|
||||||
|
#[error("Failed to pop list elements in Redis")]
|
||||||
|
PopListElementsFailed,
|
||||||
|
#[error("Failed to increment hash field in Redis")]
|
||||||
|
IncrementHashFieldFailed,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user