Skip to main content

HotkeysCommands

Trait HotkeysCommands 

Source
pub trait HotkeysCommands: ConnectionLike {
    // Provided methods
    fn hotkeys_start(&mut self, opts: HotkeysOptions) -> RedisResult<()>
       where Self: Sized { ... }
    fn hotkeys_get(&mut self) -> RedisResult<Option<HotkeysResponse>>
       where Self: Sized { ... }
    fn hotkeys_stop(&mut self) -> RedisResult<bool>
       where Self: Sized { ... }
    fn hotkeys_reset(&mut self) -> RedisResult<()>
       where Self: Sized { ... }
}
Expand description

HOTKEYS commands for tracking hot keys on standalone connections (Redis 8.6.0+).

HOTKEYS is a stateful, node-local command requiring session affinity. It is ONLY implemented for standalone connection types (Connection, MultiplexedConnection, ConnectionManager) and NOT for cluster clients (ClusterConnection) to prevent session affinity issues.

§Example (Standalone)

use redis::{HotkeysCommands, HotkeysOptions};

let client = redis::Client::open("redis://127.0.0.1/")?;
let mut con = client.get_connection()?;

// Start tracking hot keys by CPU time percentage for 60 seconds
let opts = HotkeysOptions::new_with_cpu()
    .with_duration_secs(60);
con.hotkeys_start(opts)?;

// ... perform operations ...

// Get hot keys metrics. `None` means no tracking session state is available
// (e.g. reset or never started).
if let Some(response) = con.hotkeys_get()? {
    if let Some(cpu_keys) = response.by_cpu_time_us.as_ref() {
        for entry in cpu_keys {
            println!("Key: {}, CPU time: {}", entry.key, entry.value);
        }
    }
}

// Stop tracking
con.hotkeys_stop()?;

§Using HOTKEYS in Cluster Mode

For cluster connections, use route_command with explicit node routing. See the documentation on HotkeysOptions for a complete example.

Provided Methods§

Source

fn hotkeys_start(&mut self, opts: HotkeysOptions) -> RedisResult<()>
where Self: Sized,

Start tracking hot keys with the given options.

HOTKEYS START METRICS count [CPU] [NET] [COUNT k] [DURATION seconds] [SAMPLE ratio] [SLOTS count slot [slot ...]]

Redis Docs

Source

fn hotkeys_get(&mut self) -> RedisResult<Option<HotkeysResponse>>
where Self: Sized,

Get the current hot keys metrics.

Returns Some(response) when a tracking session state is available (when there is an active tracking session or when the tracking session has been stopped but not reset) and None when there is no session state to report (Redis replies Nil in that case).

HOTKEYS GET

Redis Docs

Source

fn hotkeys_stop(&mut self) -> RedisResult<bool>
where Self: Sized,

Stop tracking hot keys.

Returns true if a tracking session was running and has been stopped, or false if no session was active (Redis replies Nil in that case).

HOTKEYS STOP

Redis Docs

Source

fn hotkeys_reset(&mut self) -> RedisResult<()>
where Self: Sized,

Reset the hot keys tracking state.

Only valid when no tracking session is currently running. Calling RESET while a session is active returns a server error.

HOTKEYS RESET

Redis Docs

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§