Keys
Commands for managing keys, expiration, and key metadata.
Commands
Section titled “Commands”| Command | Syntax | Description |
|---|---|---|
| DEL | DEL key [key ...] | Delete one or more keys |
| EXISTS | EXISTS key [key ...] | Check if keys exist |
| TYPE | TYPE key | Get key type |
| EXPIRE | EXPIRE key seconds | Set TTL in seconds |
| EXPIREAT | EXPIREAT key timestamp | Set expiration as Unix timestamp |
| PEXPIRE | PEXPIRE key milliseconds | Set TTL in milliseconds |
| PEXPIREAT | PEXPIREAT key timestamp | Set expiration as Unix milliseconds |
| TTL | TTL key | Get remaining TTL in seconds |
| PTTL | PTTL key | Get remaining TTL in milliseconds |
| PERSIST | PERSIST key | Remove expiration |
| KEYS | KEYS pattern | Find keys by glob pattern |
| SCAN | SCAN cursor [MATCH pattern] [COUNT count] | Iterate keys |
| DBSIZE | DBSIZE | Get key count in current database |
| FLUSHDB | FLUSHDB | Delete all keys in current database |
Examples
Section titled “Examples”Delete Keys
Section titled “Delete Keys”127.0.0.1:6379> SET key1 "value1"OK127.0.0.1:6379> SET key2 "value2"OK127.0.0.1:6379> DEL key1 key2(integer) 2Check Existence
Section titled “Check Existence”127.0.0.1:6379> SET mykey "Hello"OK127.0.0.1:6379> EXISTS mykey(integer) 1127.0.0.1:6379> EXISTS nosuchkey(integer) 0127.0.0.1:6379> EXISTS key1 key2 key3(integer) 2 # Returns count of existing keysExpiration
Section titled “Expiration”# Set expiration127.0.0.1:6379> SET mykey "Hello"OK127.0.0.1:6379> EXPIRE mykey 60(integer) 1
# Check remaining TTL127.0.0.1:6379> TTL mykey(integer) 58
# Remove expiration127.0.0.1:6379> PERSIST mykey(integer) 1127.0.0.1:6379> TTL mykey(integer) -1 # No expirationFind Keys
Section titled “Find Keys”# Find all keys matching pattern127.0.0.1:6379> KEYS user:*1) "user:1"2) "user:2"3) "user:100"
# Find keys with single character wildcard127.0.0.1:6379> KEYS user:?1) "user:1"2) "user:2"Scan Keys (Recommended for Large Datasets)
Section titled “Scan Keys (Recommended for Large Datasets)”127.0.0.1:6379> SCAN 0 MATCH user:* COUNT 101) "15" # Next cursor2) 1) "user:1" 2) "user:2" 3) "user:3"
# Continue with returned cursor127.0.0.1:6379> SCAN 15 MATCH user:* COUNT 101) "0" # Cursor 0 = iteration complete2) 1) "user:4" 2) "user:5"Key Type
Section titled “Key Type”127.0.0.1:6379> SET mystring "hello"OK127.0.0.1:6379> LPUSH mylist "world"(integer) 1127.0.0.1:6379> TYPE mystringstring127.0.0.1:6379> TYPE mylistlistLibrary Mode (Rust)
Section titled “Library Mode (Rust)”use redlite::Db;use std::time::Duration;
let db = Db::open("mydata.db")?;
// Delete keysdb.del(&["key1", "key2"])?;
// Check existencelet exists = db.exists(&["mykey"])?; // Returns count
// Set expirationdb.expire("mykey", Duration::from_secs(60))?;
// Get TTLlet ttl = db.ttl("mykey")?; // Returns Option<i64>
// Find keyslet keys = db.keys("user:*")?;
// Get key typelet key_type = db.key_type("mykey")?; // Returns KeyType enum