aboutsummaryrefslogtreecommitdiffstats
path: root/crates/sh-util/src/cache/cluster.rs
diff options
context:
space:
mode:
authorrtkay123 <dev@kanjala.com>2026-04-05 15:17:55 +0200
committerrtkay123 <dev@kanjala.com>2026-04-05 15:17:55 +0200
commit3f708c5fffed105b27965f8e844a26de6bdf9662 (patch)
treefbed157ae7fc15a26a86fba5e0b8b9c5107ee07f /crates/sh-util/src/cache/cluster.rs
parente86366c6d68b9d3d2af4ac4afb5cf7d5a8400dde (diff)
downloadsellershut-3f708c5fffed105b27965f8e844a26de6bdf9662.tar.bz2
sellershut-3f708c5fffed105b27965f8e844a26de6bdf9662.zip
feat(cli): cache
Diffstat (limited to 'crates/sh-util/src/cache/cluster.rs')
-rw-r--r--crates/sh-util/src/cache/cluster.rs56
1 files changed, 56 insertions, 0 deletions
diff --git a/crates/sh-util/src/cache/cluster.rs b/crates/sh-util/src/cache/cluster.rs
new file mode 100644
index 0000000..de13629
--- /dev/null
+++ b/crates/sh-util/src/cache/cluster.rs
@@ -0,0 +1,56 @@
+use redis::{
+ ErrorKind, FromRedisValue, IntoConnectionInfo, RedisError,
+ cluster::{ClusterClient, ClusterClientBuilder},
+ cluster_routing::{MultipleNodeRoutingInfo, ResponsePolicy, RoutingInfo},
+};
+
+/// ConnectionManager that implements `bb8::ManageConnection` and supports
+/// asynchronous clustered connections via `redis_cluster_async::Connection`
+#[derive(Clone)]
+pub struct RedisClusterConnectionManager {
+ client: ClusterClient,
+}
+
+impl RedisClusterConnectionManager {
+ pub fn new<T: IntoConnectionInfo>(
+ info: T,
+ ) -> Result<RedisClusterConnectionManager, RedisError> {
+ Ok(RedisClusterConnectionManager {
+ client: ClusterClientBuilder::new(vec![info]).retries(0).build()?,
+ })
+ }
+}
+
+impl bb8::ManageConnection for RedisClusterConnectionManager {
+ type Connection = redis::cluster_async::ClusterConnection;
+ type Error = RedisError;
+
+ async fn connect(&self) -> Result<Self::Connection, Self::Error> {
+ self.client.get_async_connection().await
+ }
+
+ async fn is_valid(&self, conn: &mut Self::Connection) -> Result<(), Self::Error> {
+ let pong = conn
+ .route_command(
+ redis::cmd("PING"),
+ RoutingInfo::MultiNode((
+ MultipleNodeRoutingInfo::AllMasters,
+ Some(ResponsePolicy::OneSucceeded),
+ )),
+ )
+ .await
+ .and_then(|v| Ok(String::from_redis_value(v)?))?;
+ match pong.as_str() {
+ "PONG" => Ok(()),
+ _ => Err((
+ ErrorKind::Server(redis::ServerErrorKind::ResponseError),
+ "ping request",
+ )
+ .into()),
+ }
+ }
+
+ fn has_broken(&self, _: &mut Self::Connection) -> bool {
+ false
+ }
+}