Custom Commands
Redlite extends Redis with custom commands that leverage SQLite’s capabilities.
VACUUM
Section titled “VACUUM”Delete expired keys and run SQLite VACUUM to reclaim disk space.
127.0.0.1:6379> VACUUMOKWhy VACUUM?
Section titled “Why VACUUM?”Redlite uses lazy expiration - expired keys are only deleted when accessed. This is efficient but can leave expired data on disk. VACUUM:
- Scans all databases for expired keys and deletes them
- Runs SQLite
VACUUMto reclaim disk space
Library Mode
Section titled “Library Mode”let deleted_count = db.vacuum()?;println!("Deleted {} expired keys", deleted_count);AUTOVACUUM
Section titled “AUTOVACUUM”Configure automatic background cleanup of expired keys.
# Check status127.0.0.1:6379> AUTOVACUUM1) "enabled"2) "true"3) "interval_ms"4) "60000"
# Enable (default)127.0.0.1:6379> AUTOVACUUM ONOK
# Disable127.0.0.1:6379> AUTOVACUUM OFFOK
# Set interval (minimum 1000ms)127.0.0.1:6379> AUTOVACUUM INTERVAL 30000OKHow It Works
Section titled “How It Works”When enabled, autovacuum runs periodically during read operations:
- Triggered on GET, HGET, SMEMBERS, ZRANGE, LRANGE, EXISTS
- Uses compare-and-exchange to ensure only one connection runs cleanup
- Default interval: 60 seconds
Library Mode
Section titled “Library Mode”// Check if enabledlet enabled = db.autovacuum_enabled();
// Enable/disabledb.set_autovacuum(true);db.set_autovacuum(false);
// Set interval (milliseconds)db.set_autovacuum_interval(30_000); // 30 secondsKEYINFO
Section titled “KEYINFO”Get detailed metadata about a key.
127.0.0.1:6379> SET mykey "hello" EX 3600OK127.0.0.1:6379> KEYINFO mykey1) "type"2) "string"3) "ttl"4) (integer) 35995005) "created_at"6) (integer) 17040672000007) "updated_at"8) (integer) 1704067200000Fields
Section titled “Fields”| Field | Description |
|---|---|
type | Key type: string, hash, list, set, zset, stream |
ttl | Time-to-live in milliseconds (nil if no expiration) |
created_at | Creation timestamp (Unix milliseconds) |
updated_at | Last update timestamp (Unix milliseconds) |
Library Mode
Section titled “Library Mode”if let Some(info) = db.keyinfo("mykey")? { println!("Type: {:?}", info.key_type); println!("TTL: {:?}ms", info.ttl); println!("Created: {}", info.created_at); println!("Updated: {}", info.updated_at);}HISTORY
Section titled “HISTORY”Track and query historical data with time-travel queries. See History Tracking for full documentation.
Enable/Disable
Section titled “Enable/Disable”# Enable at different levelsHISTORY ENABLE GLOBAL [RETENTION {TIME ms|COUNT n}]HISTORY ENABLE DATABASE 0 [RETENTION {TIME ms|COUNT n}]HISTORY ENABLE KEY mykey [RETENTION {TIME ms|COUNT n}]
# DisableHISTORY DISABLE GLOBALHISTORY DISABLE DATABASE 0HISTORY DISABLE KEY mykeyQuery History
Section titled “Query History”# Get history entriesHISTORY GET mykey [LIMIT n] [SINCE timestamp] [UNTIL timestamp]
# Time-travel queryHISTORY GETAT mykey 1704067200000Manage History
Section titled “Manage History”# List keys with historyHISTORY LIST [PATTERN pattern]
# Get statisticsHISTORY STATS [KEY key]
# Clean upHISTORY CLEAR key [BEFORE timestamp]HISTORY PRUNE BEFORE timestampExample
Section titled “Example”# Enable history for a key127.0.0.1:6379> HISTORY ENABLE KEY user:1 RETENTION COUNT 100OK
# Make changes127.0.0.1:6379> SET user:1 "version1"OK127.0.0.1:6379> SET user:1 "version2"OK
# Query history127.0.0.1:6379> HISTORY GET user:11) 1) "version" 2) (integer) 1 3) "operation" 4) "SET" 5) "timestamp" 6) (integer) 1704067200000 7) "type" 8) "string" 9) "data" 10) "version1"2) ...
# Time-travel127.0.0.1:6379> HISTORY GETAT user:1 1704067200000"version1"Library Mode
Section titled “Library Mode”use redlite::{Db, RetentionType};
// Enable historydb.history_enable_key("user:1", Some(RetentionType::Count(100)))?;
// Query historylet entries = db.history_get("user:1", Some(10), None, None)?;
// Time-travel querylet snapshot = db.history_get_at("user:1", 1704067200000)?;
// Statisticslet stats = db.history_stats("user:1")?;
// Cleanupdb.history_clear_key("user:1", Some(1704067200000))?;db.history_prune(1704067200000)?;Why Custom Commands?
Section titled “Why Custom Commands?”SQLite provides capabilities that Redis doesn’t have natively:
- VACUUM - SQLite manages disk space explicitly; Redis doesn’t have this concept
- AUTOVACUUM - Configurable background cleanup leveraging SQLite’s efficiency
- KEYINFO - SQLite stores rich metadata; Redis only tracks basic info
- HISTORY - SQLite’s ACID properties make versioning reliable and efficient