Skip to content

Keys

Commands for managing keys, expiration, and key metadata.

CommandSyntaxDescription
DELDEL key [key ...]Delete one or more keys
EXISTSEXISTS key [key ...]Check if keys exist
TYPETYPE keyGet key type
EXPIREEXPIRE key secondsSet TTL in seconds
EXPIREATEXPIREAT key timestampSet expiration as Unix timestamp
PEXPIREPEXPIRE key millisecondsSet TTL in milliseconds
PEXPIREATPEXPIREAT key timestampSet expiration as Unix milliseconds
TTLTTL keyGet remaining TTL in seconds
PTTLPTTL keyGet remaining TTL in milliseconds
PERSISTPERSIST keyRemove expiration
KEYSKEYS patternFind keys by glob pattern
SCANSCAN cursor [MATCH pattern] [COUNT count]Iterate keys
DBSIZEDBSIZEGet key count in current database
FLUSHDBFLUSHDBDelete all keys in current database
Terminal window
127.0.0.1:6379> SET key1 "value1"
OK
127.0.0.1:6379> SET key2 "value2"
OK
127.0.0.1:6379> DEL key1 key2
(integer) 2
Terminal window
127.0.0.1:6379> SET mykey "Hello"
OK
127.0.0.1:6379> EXISTS mykey
(integer) 1
127.0.0.1:6379> EXISTS nosuchkey
(integer) 0
127.0.0.1:6379> EXISTS key1 key2 key3
(integer) 2 # Returns count of existing keys
Terminal window
# Set expiration
127.0.0.1:6379> SET mykey "Hello"
OK
127.0.0.1:6379> EXPIRE mykey 60
(integer) 1
# Check remaining TTL
127.0.0.1:6379> TTL mykey
(integer) 58
# Remove expiration
127.0.0.1:6379> PERSIST mykey
(integer) 1
127.0.0.1:6379> TTL mykey
(integer) -1 # No expiration
Terminal window
# Find all keys matching pattern
127.0.0.1:6379> KEYS user:*
1) "user:1"
2) "user:2"
3) "user:100"
# Find keys with single character wildcard
127.0.0.1:6379> KEYS user:?
1) "user:1"
2) "user:2"
Section titled “Scan Keys (Recommended for Large Datasets)”
Terminal window
127.0.0.1:6379> SCAN 0 MATCH user:* COUNT 10
1) "15" # Next cursor
2) 1) "user:1"
2) "user:2"
3) "user:3"
# Continue with returned cursor
127.0.0.1:6379> SCAN 15 MATCH user:* COUNT 10
1) "0" # Cursor 0 = iteration complete
2) 1) "user:4"
2) "user:5"
Terminal window
127.0.0.1:6379> SET mystring "hello"
OK
127.0.0.1:6379> LPUSH mylist "world"
(integer) 1
127.0.0.1:6379> TYPE mystring
string
127.0.0.1:6379> TYPE mylist
list
use redlite::Db;
use std::time::Duration;
let db = Db::open("mydata.db")?;
// Delete keys
db.del(&["key1", "key2"])?;
// Check existence
let exists = db.exists(&["mykey"])?; // Returns count
// Set expiration
db.expire("mykey", Duration::from_secs(60))?;
// Get TTL
let ttl = db.ttl("mykey")?; // Returns Option<i64>
// Find keys
let keys = db.keys("user:*")?;
// Get key type
let key_type = db.key_type("mykey")?; // Returns KeyType enum