Skip to main content

glide/commands/
connection_management.rs

1// Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0
2//! Connection-management commands. Mirrors Python's connection command surface.
3
4use crate::ValkeyResult;
5use crate::cmd::Cmd;
6use crate::executor::CommandExecutor;
7use crate::value::FromValkeyValue;
8use crate::write::ToValkeyArgs;
9use async_trait::async_trait;
10use bytes::Bytes;
11
12/// Connection-management commands (`PING`, `ECHO`, `SELECT`, `CLIENT ...`).
13#[async_trait]
14pub trait ConnectionManagementCommands: CommandExecutor {
15    /// Echo a message (`ECHO`).
16    async fn echo<M: ToValkeyArgs + Send>(&self, message: M) -> ValkeyResult<Bytes> {
17        let mut cmd = Cmd::new();
18        cmd.arg("ECHO").arg(message);
19        Bytes::from_owned_valkey_value(self.execute_command(cmd, None).await?)
20    }
21
22    /// Select the logical database with the given index (`SELECT`).
23    async fn select(&self, index: i64) -> ValkeyResult<()> {
24        let mut cmd = Cmd::new();
25        cmd.arg("SELECT").arg(index);
26        <()>::from_owned_valkey_value(self.execute_command(cmd, None).await?)
27    }
28
29    /// Enable or disable eviction for the current connection (`CLIENT NO-EVICT`).
30    async fn client_no_evict(&self, on: bool) -> ValkeyResult<()> {
31        let mut cmd = Cmd::new();
32        cmd.arg("CLIENT")
33            .arg("NO-EVICT")
34            .arg(if on { "ON" } else { "OFF" });
35        <()>::from_owned_valkey_value(self.execute_command(cmd, None).await?)
36    }
37
38    /// Enable or disable access-time updates for the current connection
39    /// (`CLIENT NO-TOUCH`).
40    async fn client_no_touch(&self, on: bool) -> ValkeyResult<()> {
41        let mut cmd = Cmd::new();
42        cmd.arg("CLIENT")
43            .arg("NO-TOUCH")
44            .arg(if on { "ON" } else { "OFF" });
45        <()>::from_owned_valkey_value(self.execute_command(cmd, None).await?)
46    }
47
48    /// Reset the connection to its initial state (`RESET`).
49    async fn reset(&self) -> ValkeyResult<()> {
50        let mut cmd = Cmd::new();
51        cmd.arg("RESET");
52        <()>::from_owned_valkey_value(self.execute_command(cmd, None).await?)
53    }
54}
55
56impl<T: CommandExecutor + ?Sized> ConnectionManagementCommands for T {}