JsonAsyncCommands in redis - Rust
Description: Implements RedisJSON commands over asynchronous connections. This allows you to send commands straight to a connection or client.

Skip to main content

## JsonAsyncCommands

# Trait JsonAsyncCommands

Source

    pub trait JsonAsyncCommands:
        ConnectionLike
        + Send
        + Sized {
    Show 20 methods    // Provided methods
        fn json_arr_append<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, V: Serialize + Send + Sync + 'a, RV>(
            &'a mut self,
            key: K,
            path: P,
            value: &'a V,
        ) -> RedisFuture<'a, RV>
           where RV: FromRedisValue { ... }
        fn json_arr_index<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, V: Serialize + Send + Sync + 'a, RV>(
            &'a mut self,
            key: K,
            path: P,
            value: &'a V,
        ) -> RedisFuture<'a, RV>
           where RV: FromRedisValue { ... }
        fn json_arr_index_ss<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, V: Serialize + Send + Sync + 'a, RV>(
            &'a mut self,
            key: K,
            path: P,
            value: &'a V,
            start: &'a isize,
            stop: &'a isize,
        ) -> RedisFuture<'a, RV>
           where RV: FromRedisValue { ... }
        fn json_arr_insert<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, V: Serialize + Send + Sync + 'a, RV>(
            &'a mut self,
            key: K,
            path: P,
            index: i64,
            value: &'a V,
        ) -> RedisFuture<'a, RV>
           where RV: FromRedisValue { ... }
        fn json_arr_len<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, RV>(
            &'a mut self,
            key: K,
            path: P,
        ) -> RedisFuture<'a, RV>
           where RV: FromRedisValue { ... }
        fn json_arr_pop<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, RV>(
            &'a mut self,
            key: K,
            path: P,
            index: i64,
        ) -> RedisFuture<'a, RV>
           where RV: FromRedisValue { ... }
        fn json_arr_trim<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, RV>(
            &'a mut self,
            key: K,
            path: P,
            start: i64,
            stop: i64,
        ) -> RedisFuture<'a, RV>
           where RV: FromRedisValue { ... }
        fn json_clear<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, RV>(
            &'a mut self,
            key: K,
            path: P,
        ) -> RedisFuture<'a, RV>
           where RV: FromRedisValue { ... }
        fn json_del<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, RV>(
            &'a mut self,
            key: K,
            path: P,
        ) -> RedisFuture<'a, RV>
           where RV: FromRedisValue { ... }
        fn json_get<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToRedisArgs + Send + Sync + 'a, RV>(
            &'a mut self,
            key: K,
            path: P,
        ) -> RedisFuture<'a, RV>
           where RV: FromRedisValue { ... }
        fn json_mget<'a, K: ToRedisArgs + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, RV>(
            &'a mut self,
            key: K,
            path: P,
        ) -> RedisFuture<'a, RV>
           where RV: FromRedisValue { ... }
        fn json_num_incr_by<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, RV>(
            &'a mut self,
            key: K,
            path: P,
            value: i64,
        ) -> RedisFuture<'a, RV>
           where RV: FromRedisValue { ... }
        fn json_obj_keys<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, RV>(
            &'a mut self,
            key: K,
            path: P,
        ) -> RedisFuture<'a, RV>
           where RV: FromRedisValue { ... }
        fn json_obj_len<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, RV>(
            &'a mut self,
            key: K,
            path: P,
        ) -> RedisFuture<'a, RV>
           where RV: FromRedisValue { ... }
        fn json_set<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, V: Serialize + Send + Sync + 'a, RV>(
            &'a mut self,
            key: K,
            path: P,
            value: &'a V,
        ) -> RedisFuture<'a, RV>
           where RV: FromRedisValue { ... }
        fn json_mset<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, V: Serialize + Send + Sync + 'a, RV>(
            &'a mut self,
            key_path_values: &'a [(K, P, V)],
        ) -> RedisFuture<'a, RV>
           where RV: FromRedisValue { ... }
        fn json_str_append<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, V: ToSingleRedisArg + Send + Sync + 'a, RV>(
            &'a mut self,
            key: K,
            path: P,
            value: V,
        ) -> RedisFuture<'a, RV>
           where RV: FromRedisValue { ... }
        fn json_str_len<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, RV>(
            &'a mut self,
            key: K,
            path: P,
        ) -> RedisFuture<'a, RV>
           where RV: FromRedisValue { ... }
        fn json_toggle<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, RV>(
            &'a mut self,
            key: K,
            path: P,
        ) -> RedisFuture<'a, RV>
           where RV: FromRedisValue { ... }
        fn json_type<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, RV>(
            &'a mut self,
            key: K,
            path: P,
        ) -> RedisFuture<'a, RV>
           where RV: FromRedisValue { ... }
    }

Available on crate features `json` and `aio` only.
Expand description

Implements RedisJSON commands over asynchronous connections. This allows you to send commands straight to a connection or client.

This allows you to use nicer syntax for some common operations. For instance this code:

    use redis::JsonAsyncCommands;
    use serde_json::json;
    let client = redis::Client::open("redis://127.0.0.1/")?;
    let mut con = client.get_multiplexed_async_connection().await?;
    redis::cmd("JSON.SET").arg("my_key").arg("$").arg(&json!({"item": 42i32}).to_string()).exec_async(&mut con).await?;
    assert_eq!(redis::cmd("JSON.GET").arg("my_key").arg("$").query_async(&mut con).await, Ok(String::from(r#"[{"item":42}]"#)));

Will become this:

    use redis::JsonAsyncCommands;
    use serde_json::json;
    use redis::Commands;
    let client = redis::Client::open("redis://127.0.0.1/")?;
    let mut con = client.get_multiplexed_async_connection().await?;
    let _: () = con.json_set("my_key", "$", &json!({"item": 42i32}).to_string()).await?;
    assert_eq!(con.json_get("my_key", "$").await, Ok(String::from(r#"[{"item":42}]"#)));
    assert_eq!(con.json_get("my_key", "$.item").await, Ok(String::from(r#"[42]"#)));

With RedisJSON commands, you have to note that all results will be wrapped in square brackets (or empty brackets if not found). If you want to deserialize it with e.g. `serde_json` you have to use `Vec<T>` for your output type instead of `T`.

## Provided Methods§

Source

#### fn json_arr_append<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, V: Serialize + Send + Sync + 'a, RV>( &'a mut self, key: K, path: P, value: &'a V, ) -> RedisFuture<'a, RV>
where RV: FromRedisValue,

Append the JSON `value` to the array at `path` after the last element in it.

Source

#### fn json_arr_index<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, V: Serialize + Send + Sync + 'a, RV>( &'a mut self, key: K, path: P, value: &'a V, ) -> RedisFuture<'a, RV>
where RV: FromRedisValue,

Index array at `path`, returns first occurrence of `value`

Source

#### fn json_arr_index_ss<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, V: Serialize + Send + Sync + 'a, RV>( &'a mut self, key: K, path: P, value: &'a V, start: &'a isize, stop: &'a isize, ) -> RedisFuture<'a, RV>
where RV: FromRedisValue,

Same as `json_arr_index` except takes a `start` and a `stop` value, setting these to `0` will mean they make no effect on the query

The default values for `start` and `stop` are `0`, so pass those in if you want them to take no effect

Source

#### fn json_arr_insert<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, V: Serialize + Send + Sync + 'a, RV>( &'a mut self, key: K, path: P, index: i64, value: &'a V, ) -> RedisFuture<'a, RV>
where RV: FromRedisValue,

Inserts the JSON `value` in the array at `path` before the `index` (shifts to the right).

`index` must be within the array’s range.

Source

#### fn json_arr_len<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, RV>( &'a mut self, key: K, path: P, ) -> RedisFuture<'a, RV>
where RV: FromRedisValue,

Reports the length of the JSON Array at `path` in `key`.

Source

#### fn json_arr_pop<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, RV>( &'a mut self, key: K, path: P, index: i64, ) -> RedisFuture<'a, RV>
where RV: FromRedisValue,

Removes and returns an element from the `index` in the array.

`index` defaults to `-1` (the end of the array).

Source

#### fn json_arr_trim<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, RV>( &'a mut self, key: K, path: P, start: i64, stop: i64, ) -> RedisFuture<'a, RV>
where RV: FromRedisValue,

Trims an array so that it contains only the specified inclusive range of elements.

This command is extremely forgiving and using it with out-of-range indexes will not produce an error. There are a few differences between how RedisJSON v2.0 and legacy versions handle out-of-range indexes.

Source

#### fn json_clear<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, RV>( &'a mut self, key: K, path: P, ) -> RedisFuture<'a, RV>
where RV: FromRedisValue,

Clears container values (Arrays/Objects), and sets numeric values to 0.

Source

#### fn json_del<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, RV>( &'a mut self, key: K, path: P, ) -> RedisFuture<'a, RV>
where RV: FromRedisValue,

Deletes a value at `path`.

Source

#### fn json_get<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToRedisArgs + Send + Sync + 'a, RV>( &'a mut self, key: K, path: P, ) -> RedisFuture<'a, RV>
where RV: FromRedisValue,

Gets JSON Value at `path`.

With RedisJSON commands, you have to note that all results will be wrapped in square brackets (or empty brackets if not found). If you want to deserialize it with e.g. `serde_json` you have to use `Vec<T>` for your output type instead of `T`.

Source

#### fn json_mget<'a, K: ToRedisArgs + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, RV>( &'a mut self, key: K, path: P, ) -> RedisFuture<'a, RV>
where RV: FromRedisValue,

Gets JSON Values at `path`.

With RedisJSON commands, you have to note that all results will be wrapped in square brackets (or empty brackets if not found). If you want to deserialize it with e.g. `serde_json` you have to use `Vec<T>` for your output type instead of `T`.

Source

#### fn json_num_incr_by<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, RV>( &'a mut self, key: K, path: P, value: i64, ) -> RedisFuture<'a, RV>
where RV: FromRedisValue,

Increments the number value stored at `path` by `number`.

Source

#### fn json_obj_keys<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, RV>( &'a mut self, key: K, path: P, ) -> RedisFuture<'a, RV>
where RV: FromRedisValue,

Returns the keys in the object that’s referenced by `path`.

Source

#### fn json_obj_len<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, RV>( &'a mut self, key: K, path: P, ) -> RedisFuture<'a, RV>
where RV: FromRedisValue,

Reports the number of keys in the JSON Object at `path` in `key`.

Source

#### fn json_set<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, V: Serialize + Send + Sync + 'a, RV>( &'a mut self, key: K, path: P, value: &'a V, ) -> RedisFuture<'a, RV>
where RV: FromRedisValue,

Sets the JSON Value at `path` in `key`.

Source

#### fn json_mset<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, V: Serialize + Send + Sync + 'a, RV>( &'a mut self, key_path_values: &'a [(K, P, V)], ) -> RedisFuture<'a, RV>
where RV: FromRedisValue,

Sets the value at the path per key, for every given tuple.

Source

#### fn json_str_append<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, V: ToSingleRedisArg + Send + Sync + 'a, RV>( &'a mut self, key: K, path: P, value: V, ) -> RedisFuture<'a, RV>
where RV: FromRedisValue,

Appends the `json-string` values to the string at `path`.

Source

#### fn json_str_len<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, RV>( &'a mut self, key: K, path: P, ) -> RedisFuture<'a, RV>
where RV: FromRedisValue,

Reports the length of the JSON String at `path` in `key`.

Source

#### fn json_toggle<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, RV>( &'a mut self, key: K, path: P, ) -> RedisFuture<'a, RV>
where RV: FromRedisValue,

Toggle a `boolean` value stored at `path`.

Source

#### fn json_type<'a, K: ToSingleRedisArg + Send + Sync + 'a, P: ToSingleRedisArg + Send + Sync + 'a, RV>( &'a mut self, key: K, path: P, ) -> RedisFuture<'a, RV>
where RV: FromRedisValue,

Reports the type of JSON value at `path`.

## Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

## Implementors§

Source§

### impl<T> JsonAsyncCommands for T
where T: ConnectionLike + Send + Sized,