Skip to content

Strings

String commands for storing and manipulating text and binary data.

CommandSyntaxDescription
GETGET keyGet value of key
SETSET key value [EX s] [PX ms] [NX|XX]Set value with optional expiration
MGETMGET key [key ...]Get values of multiple keys
MSETMSET key value [key value ...]Set multiple key-value pairs
INCRINCR keyIncrement integer value by 1
INCRBYINCRBY key incrementIncrement by integer amount
INCRBYFLOATINCRBYFLOAT key incrementIncrement by float amount
DECRDECR keyDecrement integer value by 1
DECRBYDECRBY key decrementDecrement by integer amount
APPENDAPPEND key valueAppend to existing value
STRLENSTRLEN keyGet string length
GETRANGEGETRANGE key start endGet substring
SETRANGESETRANGE key offset valueOverwrite part of string
SETNXSETNX key valueSet only if not exists
SETEXSETEX key seconds valueSet with expiration in seconds
PSETEXPSETEX key milliseconds valueSet with expiration in milliseconds
Terminal window
127.0.0.1:6379> SET name "Alice"
OK
127.0.0.1:6379> GET name
"Alice"
Terminal window
# Expires in 60 seconds
127.0.0.1:6379> SET session:abc "user_data" EX 60
OK
# Expires in 5000 milliseconds
127.0.0.1:6379> SET temp "quick" PX 5000
OK
Terminal window
# Only set if key doesn't exist (NX)
127.0.0.1:6379> SET lock:resource "owner_id" NX
OK
127.0.0.1:6379> SET lock:resource "other_owner" NX
(nil) # Key already exists
# Only set if key exists (XX)
127.0.0.1:6379> SET counter "10" XX
OK # Updates existing key
Terminal window
127.0.0.1:6379> SET views 0
OK
127.0.0.1:6379> INCR views
(integer) 1
127.0.0.1:6379> INCRBY views 10
(integer) 11
127.0.0.1:6379> DECR views
(integer) 10
Terminal window
127.0.0.1:6379> MSET key1 "value1" key2 "value2" key3 "value3"
OK
127.0.0.1:6379> MGET key1 key2 key3
1) "value1"
2) "value2"
3) "value3"
use redlite::{Db, SetOptions};
use std::time::Duration;
let db = Db::open("mydata.db")?;
// Basic SET/GET
db.set("name", b"Alice", None)?;
let name = db.get("name")?; // Some(b"Alice".to_vec())
// With TTL
db.set("session", b"data", Some(Duration::from_secs(60)))?;
// Conditional SET
db.set_opts("lock", b"owner", SetOptions::new().nx())?;
db.set_opts("counter", b"10", SetOptions::new().xx())?;
// Counters
db.incr("views")?;
db.incrby("views", 10)?;
db.decr("views")?;