Trait redis::JsonCommands

source ·
pub trait JsonCommands: ConnectionLike + Sized {
Show 18 methods // Provided methods fn json_arr_append<'a, K: ToRedisArgs, P: ToRedisArgs, V: Serialize, RV: FromRedisValue>( &mut self, key: K, path: P, value: &'a V ) -> RedisResult<RV> { ... } fn json_arr_index<'a, K: ToRedisArgs, P: ToRedisArgs, V: Serialize, RV: FromRedisValue>( &mut self, key: K, path: P, value: &'a V ) -> RedisResult<RV> { ... } fn json_arr_index_ss<'a, K: ToRedisArgs, P: ToRedisArgs, V: Serialize, RV: FromRedisValue>( &mut self, key: K, path: P, value: &'a V, start: &'a isize, stop: &'a isize ) -> RedisResult<RV> { ... } fn json_arr_insert<'a, K: ToRedisArgs, P: ToRedisArgs, V: Serialize, RV: FromRedisValue>( &mut self, key: K, path: P, index: i64, value: &'a V ) -> RedisResult<RV> { ... } fn json_arr_len<'a, K: ToRedisArgs, P: ToRedisArgs, RV: FromRedisValue>( &mut self, key: K, path: P ) -> RedisResult<RV> { ... } fn json_arr_pop<'a, K: ToRedisArgs, P: ToRedisArgs, RV: FromRedisValue>( &mut self, key: K, path: P, index: i64 ) -> RedisResult<RV> { ... } fn json_arr_trim<'a, K: ToRedisArgs, P: ToRedisArgs, RV: FromRedisValue>( &mut self, key: K, path: P, start: i64, stop: i64 ) -> RedisResult<RV> { ... } fn json_clear<'a, K: ToRedisArgs, P: ToRedisArgs, RV: FromRedisValue>( &mut self, key: K, path: P ) -> RedisResult<RV> { ... } fn json_del<'a, K: ToRedisArgs, P: ToRedisArgs, RV: FromRedisValue>( &mut self, key: K, path: P ) -> RedisResult<RV> { ... } fn json_get<'a, K: ToRedisArgs, P: ToRedisArgs, RV: FromRedisValue>( &mut self, key: K, path: P ) -> RedisResult<RV> { ... } fn json_num_incr_by<'a, K: ToRedisArgs, P: ToRedisArgs, RV: FromRedisValue>( &mut self, key: K, path: P, value: i64 ) -> RedisResult<RV> { ... } fn json_obj_keys<'a, K: ToRedisArgs, P: ToRedisArgs, RV: FromRedisValue>( &mut self, key: K, path: P ) -> RedisResult<RV> { ... } fn json_obj_len<'a, K: ToRedisArgs, P: ToRedisArgs, RV: FromRedisValue>( &mut self, key: K, path: P ) -> RedisResult<RV> { ... } fn json_set<'a, K: ToRedisArgs, P: ToRedisArgs, V: Serialize, RV: FromRedisValue>( &mut self, key: K, path: P, value: &'a V ) -> RedisResult<RV> { ... } fn json_str_append<'a, K: ToRedisArgs, P: ToRedisArgs, V: ToRedisArgs, RV: FromRedisValue>( &mut self, key: K, path: P, value: V ) -> RedisResult<RV> { ... } fn json_str_len<'a, K: ToRedisArgs, P: ToRedisArgs, RV: FromRedisValue>( &mut self, key: K, path: P ) -> RedisResult<RV> { ... } fn json_toggle<'a, K: ToRedisArgs, P: ToRedisArgs, RV: FromRedisValue>( &mut self, key: K, path: P ) -> RedisResult<RV> { ... } fn json_type<'a, K: ToRedisArgs, P: ToRedisArgs, RV: FromRedisValue>( &mut self, key: K, path: P ) -> RedisResult<RV> { ... }
}
Available on crate feature json only.
Expand description

Implements RedisJSON commands for connection like objects. This allows you to send commands straight to a connection or client. It is also implemented for redis results of clients which makes for very convenient access in some basic cases.

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

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

Will become this:

use redis::JsonCommands;
use serde_json::json;
let client = redis::Client::open("redis://127.0.0.1/")?;
let mut con = client.get_connection()?;
con.json_set("my_key", "$", &json!({"item": 42i32}).to_string())?;
assert_eq!(con.json_get("my_key", "$"), Ok(String::from(r#"[{"item":42}]"#)));
assert_eq!(con.json_get("my_key", "$.item"), 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: ToRedisArgs, P: ToRedisArgs, V: Serialize, RV: FromRedisValue>( &mut self, key: K, path: P, value: &'a V ) -> RedisResult<RV>

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

source

fn json_arr_index<'a, K: ToRedisArgs, P: ToRedisArgs, V: Serialize, RV: FromRedisValue>( &mut self, key: K, path: P, value: &'a V ) -> RedisResult<RV>

Index array at path, returns first occurance of value

source

fn json_arr_index_ss<'a, K: ToRedisArgs, P: ToRedisArgs, V: Serialize, RV: FromRedisValue>( &mut self, key: K, path: P, value: &'a V, start: &'a isize, stop: &'a isize ) -> RedisResult<RV>

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: ToRedisArgs, P: ToRedisArgs, V: Serialize, RV: FromRedisValue>( &mut self, key: K, path: P, index: i64, value: &'a V ) -> RedisResult<RV>

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

index must be withing the array’s range.

source

fn json_arr_len<'a, K: ToRedisArgs, P: ToRedisArgs, RV: FromRedisValue>( &mut self, key: K, path: P ) -> RedisResult<RV>

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

source

fn json_arr_pop<'a, K: ToRedisArgs, P: ToRedisArgs, RV: FromRedisValue>( &mut self, key: K, path: P, index: i64 ) -> RedisResult<RV>

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: ToRedisArgs, P: ToRedisArgs, RV: FromRedisValue>( &mut self, key: K, path: P, start: i64, stop: i64 ) -> RedisResult<RV>

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: ToRedisArgs, P: ToRedisArgs, RV: FromRedisValue>( &mut self, key: K, path: P ) -> RedisResult<RV>

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

source

fn json_del<'a, K: ToRedisArgs, P: ToRedisArgs, RV: FromRedisValue>( &mut self, key: K, path: P ) -> RedisResult<RV>

Deletes a value at path.

source

fn json_get<'a, K: ToRedisArgs, P: ToRedisArgs, RV: FromRedisValue>( &mut self, key: K, path: P ) -> RedisResult<RV>

Gets JSON Value(s) at path.

Runs JSON.GET if key is singular, JSON.MGET if there are multiple keys.

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: ToRedisArgs, P: ToRedisArgs, RV: FromRedisValue>( &mut self, key: K, path: P, value: i64 ) -> RedisResult<RV>

Increments the number value stored at path by number.

source

fn json_obj_keys<'a, K: ToRedisArgs, P: ToRedisArgs, RV: FromRedisValue>( &mut self, key: K, path: P ) -> RedisResult<RV>

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

source

fn json_obj_len<'a, K: ToRedisArgs, P: ToRedisArgs, RV: FromRedisValue>( &mut self, key: K, path: P ) -> RedisResult<RV>

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

source

fn json_set<'a, K: ToRedisArgs, P: ToRedisArgs, V: Serialize, RV: FromRedisValue>( &mut self, key: K, path: P, value: &'a V ) -> RedisResult<RV>

Sets the JSON Value at path in key.

source

fn json_str_append<'a, K: ToRedisArgs, P: ToRedisArgs, V: ToRedisArgs, RV: FromRedisValue>( &mut self, key: K, path: P, value: V ) -> RedisResult<RV>

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

source

fn json_str_len<'a, K: ToRedisArgs, P: ToRedisArgs, RV: FromRedisValue>( &mut self, key: K, path: P ) -> RedisResult<RV>

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

source

fn json_toggle<'a, K: ToRedisArgs, P: ToRedisArgs, RV: FromRedisValue>( &mut self, key: K, path: P ) -> RedisResult<RV>

Toggle a boolean value stored at path.

source

fn json_type<'a, K: ToRedisArgs, P: ToRedisArgs, RV: FromRedisValue>( &mut self, key: K, path: P ) -> RedisResult<RV>

Reports the type of JSON value at path.

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<T> JsonCommands for T
where T: ConnectionLike,