StringCommands

Trait StringCommands 

Source
pub trait StringCommands<'a>: Sized {
Show 26 methods // Provided methods fn append( self, key: impl Serialize, value: impl Serialize, ) -> PreparedCommand<'a, Self, usize> { ... } fn decr(self, key: impl Serialize) -> PreparedCommand<'a, Self, i64> { ... } fn decrby( self, key: impl Serialize, decrement: i64, ) -> PreparedCommand<'a, Self, i64> { ... } fn get<R: Response>( self, key: impl Serialize, ) -> PreparedCommand<'a, Self, R> { ... } fn getdel<R: Response>( self, key: impl Serialize, ) -> PreparedCommand<'a, Self, R> { ... } fn getex<R: Response>( self, key: impl Serialize, options: GetExOptions, ) -> PreparedCommand<'a, Self, R> { ... } fn getrange<R: Response>( self, key: impl Serialize, start: isize, end: isize, ) -> PreparedCommand<'a, Self, R> { ... } fn getset<R: Response>( self, key: impl Serialize, value: impl Serialize, ) -> PreparedCommand<'a, Self, R> { ... } fn incr(self, key: impl Serialize) -> PreparedCommand<'a, Self, i64> { ... } fn incrby( self, key: impl Serialize, increment: i64, ) -> PreparedCommand<'a, Self, i64> { ... } fn incrbyfloat( self, key: impl Serialize, increment: f64, ) -> PreparedCommand<'a, Self, f64> { ... } fn lcs<R: Response>( self, key1: impl Serialize, key2: impl Serialize, ) -> PreparedCommand<'a, Self, R> { ... } fn lcs_len( self, key1: impl Serialize, key2: impl Serialize, ) -> PreparedCommand<'a, Self, usize> { ... } fn lcs_idx( self, key1: impl Serialize, key2: impl Serialize, min_match_len: Option<usize>, with_match_len: bool, ) -> PreparedCommand<'a, Self, LcsResult> { ... } fn mget<R: Response>( self, keys: impl Serialize, ) -> PreparedCommand<'a, Self, R> { ... } fn mset(self, items: impl Serialize) -> PreparedCommand<'a, Self, ()> { ... } fn msetnx(self, items: impl Serialize) -> PreparedCommand<'a, Self, bool> { ... } fn psetex( self, key: impl Serialize, milliseconds: u64, value: impl Serialize, ) -> PreparedCommand<'a, Self, ()> { ... } fn set( self, key: impl Serialize, value: impl Serialize, ) -> PreparedCommand<'a, Self, ()> { ... } fn set_with_options<'b>( self, key: impl Serialize, value: impl Serialize, condition: impl Into<Option<SetCondition<'b>>>, expiration: impl Into<Option<SetExpiration>>, ) -> PreparedCommand<'a, Self, bool> { ... } fn set_get_with_options<'b, R: Response>( self, key: impl Serialize, value: impl Serialize, condition: impl Into<Option<SetCondition<'b>>>, expiration: impl Into<Option<SetExpiration>>, ) -> PreparedCommand<'a, Self, R> { ... } fn setex( self, key: impl Serialize, seconds: u64, value: impl Serialize, ) -> PreparedCommand<'a, Self, ()> { ... } fn setnx( self, key: impl Serialize, value: impl Serialize, ) -> PreparedCommand<'a, Self, bool> { ... } fn setrange( self, key: impl Serialize, offset: usize, value: impl Serialize, ) -> PreparedCommand<'a, Self, usize> { ... } fn strlen(self, key: impl Serialize) -> PreparedCommand<'a, Self, usize> { ... } fn substr<R: Response>( self, key: impl Serialize, start: isize, end: isize, ) -> PreparedCommand<'a, Self, R> { ... }
}
Expand description

A group of Redis commands related to Strings

§See Also

Redis Generic Commands

Provided Methods§

Source

fn append( self, key: impl Serialize, value: impl Serialize, ) -> PreparedCommand<'a, Self, usize>

If key already exists and is a string, this command appends the value at the end of the string. If key does not exist it is created and set as an empty string, so APPEND will be similar to SET in this special case.

§Return

the length of the string after the append operation.

§See Also

https://redis.io/commands/append/

Source

fn decr(self, key: impl Serialize) -> PreparedCommand<'a, Self, i64>

Decrements the number stored at key by one.

If the key does not exist, it is set to 0 before performing the operation. An error is returned if the key contains a value of the wrong type or contains a string that can not be represented as integer. This operation is limited to 64 bit signed integers.

§Return

the value of key after the decrement

§See Also

https://redis.io/commands/decr/

Source

fn decrby( self, key: impl Serialize, decrement: i64, ) -> PreparedCommand<'a, Self, i64>

Decrements the number stored at key by one.

If the key does not exist, it is set to 0 before performing the operation. An error is returned if the key contains a value of the wrong type or contains a string that can not be represented as integer. This operation is limited to 64 bit signed integers.

§Return

the value of key after the decrement

§See Also

https://redis.io/commands/decrby/

Source

fn get<R: Response>(self, key: impl Serialize) -> PreparedCommand<'a, Self, R>

Get the value of key.

Get the value of key. If the key does not exist the special value nil is returned. An error is returned if the value stored at key is not a string, because GET only handles string values.

§Return

the value of key, or nil when key does not exist.

§Example
use rustis::{
    client::{Client, ClientPreparedCommand},
    commands::{FlushingMode, ServerCommands, StringCommands},
    resp::{cmd},
    Result
};

#[cfg_attr(feature = "tokio-runtime", tokio::main)]
#[cfg_attr(feature = "async-std-runtime", async_std::main)]
async fn main() -> Result<()> {
    let client = Client::connect("127.0.0.1:6379").await?;
    client.flushall(FlushingMode::Sync).await?;

    // return value can be an Option<String>...
    let value: Option<String> = client.get("key").await?;
    assert_eq!(None, value);

    // ... or it can be directly a String.
    // In this cas a `nil` value will result in an empty String
    let value: String = client.get("key").await?;
    assert_eq!("", value);

    client.set("key", "value").await?;
    let value: String = client.get("key").await?;
    assert_eq!("value", value);

    Ok(())
}
§See Also

https://redis.io/commands/get/

Source

fn getdel<R: Response>( self, key: impl Serialize, ) -> PreparedCommand<'a, Self, R>

Get the value of key and delete the key.

This command is similar to GET, except for the fact that it also deletes the key on success (if and only if the key’s value type is a string).

§Return

the value of key, nil when key does not exist, or an error if the key’s value type isn’t a string.

§See Also

https://redis.io/commands/getdel/

Source

fn getex<R: Response>( self, key: impl Serialize, options: GetExOptions, ) -> PreparedCommand<'a, Self, R>

Get the value of key and optionally set its expiration. GETEX is similar to GET, but is a write command with additional options.

Decrements the number stored at key by decrement. If the key does not exist, it is set to 0 before performing the operation. An error is returned if the key contains a value of the wrong type or contains a string that can not be represented as integer. This operation is limited to 64 bit signed integers.

§Return

the value of key, or nil when key does not exist.

§Example
use rustis::{
    client::{Client, ClientPreparedCommand},
    commands::{FlushingMode, GetExOptions, GenericCommands, ServerCommands, StringCommands},
    resp::cmd,
    Result,
};

#[cfg_attr(feature = "tokio-runtime", tokio::main)]
#[cfg_attr(feature = "async-std-runtime", async_std::main)]
async fn main() -> Result<()> {
    let client = Client::connect("127.0.0.1:6379").await?;
    client.flushall(FlushingMode::Sync).await?;

    client.set("key", "value").await?;
    let value: String = client.getex("key", GetExOptions::Ex(60)).await?;
    assert_eq!("value", value);

    let ttl = client.ttl("key").await?;
    assert!(59 <= ttl && ttl <= 60);

    Ok(())
}
§See Also

https://redis.io/commands/getex/

Source

fn getrange<R: Response>( self, key: impl Serialize, start: isize, end: isize, ) -> PreparedCommand<'a, Self, R>

Returns the substring of the string value stored at key, determined by the offsets start and end (both are inclusive).

Negative offsets can be used in order to provide an offset starting from the end of the string. So -1 means the last character, -2 the penultimate and so forth.

The function handles out of range requests by limiting the resulting range to the actual length of the string.

§Example
use rustis::{
    client::Client,
    commands::{FlushingMode, ServerCommands, StringCommands},
    Result,
};

#[cfg_attr(feature = "tokio-runtime", tokio::main)]
#[cfg_attr(feature = "async-std-runtime", async_std::main)]
async fn main() -> Result<()> {
    let client = Client::connect("127.0.0.1:6379").await?;
    client.flushall(FlushingMode::Sync).await?;
    client.set("mykey", "This is a string").await?;

    let value: String = client.getrange("mykey", 0, 3).await?;
    assert_eq!("This", value);
    let value: String = client.getrange("mykey", -3, -1).await?;
    assert_eq!("ing", value);
    let value: String = client.getrange("mykey", 0, -1).await?;
    assert_eq!("This is a string", value);
    let value: String = client.getrange("mykey", 10, 100).await?;
    assert_eq!("string", value);
    Ok(())
}
§See Also

https://redis.io/commands/getrange/

Source

fn getset<R: Response>( self, key: impl Serialize, value: impl Serialize, ) -> PreparedCommand<'a, Self, R>

Atomically sets key to value and returns the old value stored at key. Returns an error when key exists but does not hold a string value. Any previous time to live associated with the key is discarded on successful SET operation.

§Return

the old value stored at key, or nil when key did not exist.

§See Also

https://redis.io/commands/getset/

Source

fn incr(self, key: impl Serialize) -> PreparedCommand<'a, Self, i64>

Increments the number stored at key by one.

If the key does not exist, it is set to 0 before performing the operation. An error is returned if the key contains a value of the wrong type or contains a string that can not be represented as integer. This operation is limited to 64 bit signed integers.

Note: this is a string operation because Redis does not have a dedicated integer type. The string stored at the key is interpreted as a base-10 64 bit signed integer to execute the operation.

Redis stores integers in their integer representation, so for string values that actually hold an integer, there is no overhead for storing the string representation of the integer.

§Return

the value of key after the increment

§See Also

https://redis.io/commands/incr/

Source

fn incrby( self, key: impl Serialize, increment: i64, ) -> PreparedCommand<'a, Self, i64>

Increments the number stored at key by increment.

If the key does not exist, it is set to 0 before performing the operation. An error is returned if the key contains a value of the wrong type or contains a string that can not be represented as integer. This operation is limited to 64 bit signed integers.

See incr for extra information on increment/decrement operations.

§Return

the value of key after the increment

§See Also

https://redis.io/commands/incrby/

Source

fn incrbyfloat( self, key: impl Serialize, increment: f64, ) -> PreparedCommand<'a, Self, f64>

Increment the string representing a floating point number stored at key by the specified increment. By using a negative increment value, the result is that the value stored at the key is decremented (by the obvious properties of addition). If the key does not exist, it is set to 0 before performing the operation. An error is returned if one of the following conditions occur:

  • The key contains a value of the wrong type (not a string).

  • The current key content or the specified increment are not parsable as a double precision floating point number.

If the command is successful the new incremented value is stored as the new value of the key (replacing the old one), and returned to the caller as a string.

Both the value already contained in the string key and the increment argument can be optionally provided in exponential notation, however the value computed after the increment is stored consistently in the same format, that is, an integer number followed (if needed) by a dot, and a variable number of digits representing the decimal part of the number. Trailing zeroes are always removed.

The precision of the output is fixed at 17 digits after the decimal point regardless of the actual internal precision of the computation.

§Return

the value of key after the increment

§See Also

https://redis.io/commands/incrbyfloat/

Source

fn lcs<R: Response>( self, key1: impl Serialize, key2: impl Serialize, ) -> PreparedCommand<'a, Self, R>

The LCS command implements the longest common subsequence algorithm

§Return

The string representing the longest common substring.

§See Also

https://redis.io/commands/lcs/

Source

fn lcs_len( self, key1: impl Serialize, key2: impl Serialize, ) -> PreparedCommand<'a, Self, usize>

The LCS command implements the longest common subsequence algorithm

§Return

The length of the longest common substring.

§See Also

https://redis.io/commands/lcs/

Source

fn lcs_idx( self, key1: impl Serialize, key2: impl Serialize, min_match_len: Option<usize>, with_match_len: bool, ) -> PreparedCommand<'a, Self, LcsResult>

The LCS command implements the longest common subsequence algorithm

§Return

An array with the LCS length and all the ranges in both the strings, start and end offset for each string, where there are matches. When with_match_len is given each match will also have the length of the match

§See Also

https://redis.io/commands/lcs/

Source

fn mget<R: Response>(self, keys: impl Serialize) -> PreparedCommand<'a, Self, R>

Returns the values of all specified keys.

For every key that does not hold a string value or does not exist, the special value nil is returned. Because of this, the operation never fails.

§Return

Array reply: list of values at the specified keys.

§See Also

https://redis.io/commands/mget/

Source

fn mset(self, items: impl Serialize) -> PreparedCommand<'a, Self, ()>

Sets the given keys to their respective values.

§Return

always OK since MSET can’t fail.

§See Also

https://redis.io/commands/mset/

Source

fn msetnx(self, items: impl Serialize) -> PreparedCommand<'a, Self, bool>

Sets the given keys to their respective values. MSETNX will not perform any operation at all even if just a single key already exists.

Because of this semantic MSETNX can be used in order to set different keys representing different fields of a unique logic object in a way that ensures that either all the fields or none at all are set.

MSETNX is atomic, so all given keys are set at once. It is not possible for clients to see that some of the keys were updated while others are unchanged.

§Return

specifically:

  • 1 if the all the keys were set.
  • 0 if no key was set (at least one key already existed).
§See Also

https://redis.io/commands/msetnx/

Source

fn psetex( self, key: impl Serialize, milliseconds: u64, value: impl Serialize, ) -> PreparedCommand<'a, Self, ()>

Works exactly like setex with the sole difference that the expire time is specified in milliseconds instead of seconds.

If key already holds a value, it is overwritten, regardless of its type. Any previous time to live associated with the key is discarded on successful SET operation.

§See Also

https://redis.io/commands/psetex/

Source

fn set( self, key: impl Serialize, value: impl Serialize, ) -> PreparedCommand<'a, Self, ()>

Set key to hold the string value.

If key already holds a value, it is overwritten, regardless of its type. Any previous time to live associated with the key is discarded on successful SET operation.

§See Also

https://redis.io/commands/set/

Source

fn set_with_options<'b>( self, key: impl Serialize, value: impl Serialize, condition: impl Into<Option<SetCondition<'b>>>, expiration: impl Into<Option<SetExpiration>>, ) -> PreparedCommand<'a, Self, bool>

Set key to hold the string value.

§Return
  • true if SET was executed correctly.
  • false if the SET operation was not performed because the user specified the NX or XX option but the condition was not met.
§See Also

https://redis.io/commands/set/

Source

fn set_get_with_options<'b, R: Response>( self, key: impl Serialize, value: impl Serialize, condition: impl Into<Option<SetCondition<'b>>>, expiration: impl Into<Option<SetExpiration>>, ) -> PreparedCommand<'a, Self, R>

Set key to hold the string value wit GET option enforced

§See Also

https://redis.io/commands/set/

Source

fn setex( self, key: impl Serialize, seconds: u64, value: impl Serialize, ) -> PreparedCommand<'a, Self, ()>

Set key to hold the string value and set key to timeout after a given number of seconds.

§See Also

https://redis.io/commands/setex/

Source

fn setnx( self, key: impl Serialize, value: impl Serialize, ) -> PreparedCommand<'a, Self, bool>

Set key to hold string value if key does not exist.

In that case, it is equal to SET. When key already holds a value, no operation is performed. SETNX is short for “SET if Not eXists”.

§Return

specifically:

  • true - if the key was set
  • false - if the key was not set
§See Also

https://redis.io/commands/setnx/

Source

fn setrange( self, key: impl Serialize, offset: usize, value: impl Serialize, ) -> PreparedCommand<'a, Self, usize>

Overwrites part of the string stored at key, starting at the specified offset, for the entire length of value.

§Return

the length of the string after it was modified by the command.

§See Also

https://redis.io/commands/setrange/

Source

fn strlen(self, key: impl Serialize) -> PreparedCommand<'a, Self, usize>

Returns the length of the string value stored at key.

An error is returned when key holds a non-string value.

§Return

the length of the string at key, or 0 when key does not exist.

§See Also

https://redis.io/commands/strlen/

Source

fn substr<R: Response>( self, key: impl Serialize, start: isize, end: isize, ) -> PreparedCommand<'a, Self, R>

Returns the substring of the string value stored at key, determined by the offsets start and end (both are inclusive).

Negative offsets can be used in order to provide an offset starting from the end of the string. So -1 means the last character, -2 the penultimate and so forth.

The function handles out of range requests by limiting the resulting range to the actual length of the string.

§Example

client.set("mykey", "This is a string").await?;

let value: String = client.substr("mykey", 0, 3).await?;
assert_eq!("This", value);
let value: String = client.substr("mykey", -3, -1).await?;
assert_eq!("ing", value);
let value: String = client.substr("mykey", 0, -1).await?;
assert_eq!("This is a string", value);
let value: String = client.substr("mykey", 10, 100).await?;
assert_eq!("string", value);
§See Also

https://redis.io/commands/substr/

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<'a> StringCommands<'a> for &'a Client

Source§

impl<'a> StringCommands<'a> for &'a mut Pipeline<'_>

Source§

impl<'a> StringCommands<'a> for &'a mut Transaction