Basic Commands
Auth - Authentication
Authenticate with Redis server using password:
vb
' Authenticate with password
If oRedis.Auth("mypassword") Then
Debug.Print "Authentication successful"
Else
Debug.Print "Authentication failed: " & oRedis.LastError
End IfSelectDb - Select Database
Redis supports multiple databases (default 0-15):
vb
' Switch to database 1
If oRedis.SelectDb(1) Then
Debug.Print "Switched to database 1"
End If
' Or set property directly
oRedis.DbIndex = 2 ' Switch to database 2Example: Using Multiple Databases
vb
' Store user data in database 0
oRedis.SelectDb 0
oRedis.Set_ "user:1", "Zhang San"
oRedis.Set_ "user:2", "Li Si"
' Store configuration data in database 1
oRedis.SelectDb 1
oRedis.Set_ "config:appname", "MyApp"
oRedis.Set_ "config:version", "1.0.0"
' Switch back to database 0
oRedis.SelectDb 0Ping - Test Connection
Test connection with Redis server:
vb
Dim sResult As String
sResult = oRedis.Ping()
Debug.Print sResult ' Output: PONGUsage:
- Check if connection is still active
- Monitor server response time
- Heartbeat detection
Info - Get Server Information
Get All Information
vb
Dim sInfo As String
sInfo = oRedis.Info()
Debug.Print sInfoGet Specific Section Information
vb
' Get server information
sInfo = oRedis.Info("server")
Debug.Print sInfo
' Get memory information
sInfo = oRedis.Info("memory")
' Get persistence information
sInfo = oRedis.Info("persistence")
' Get statistics information
sInfo = oRedis.Info("stats")
' Get replication information
sInfo = oRedis.Info("replication")FlushDb - Clear Current Database
Clear all keys in the current database:
vb
If oRedis.FlushDb() Then
Debug.Print "Database cleared"
Else
Debug.Print "Clear failed: " & oRedis.LastError
End IfWarning: This operation is irreversible, use with caution!
Basic Key Operations
Del - Delete Keys
vb
' Delete single key
oRedis.Del "name"
' Delete multiple keys
oRedis.Del "key1", "key2", "key3"Exists - Check if Key Exists
vb
' Check single key
If oRedis.Exists("name") > 0 Then
Debug.Print "Key exists"
End If
' Check multiple keys
Dim lCount As Long
lCount = oRedis.Exists("key1", "key2", "key3")
Debug.Print lCount & " keys exist"Keys - Find Keys
vb
' Find all keys
Dim vKeys As Variant
vKeys = oRedis.Keys("*")
' Find keys starting with "user:"
vKeys = oRedis.Keys("user:*")
' Find keys ending with "session"
vKeys = oRedis.Keys("*session")
' Output all keys
Dim i As Long
If IsArray(vKeys) Then
For i = 0 To UBound(vKeys)
Debug.Print vKeys(i)
Next
End IfNote: Use KEYS command with caution in production environments as it scans the entire database and may affect performance.
Expiration Management
Expire - Set Expiration Time
vb
' Set key expiration time (seconds)
oRedis.Expire "name", 300 ' Expire in 5 minutes
' Set session expiration time to 1 hour
oRedis.Expire "session:123", 3600TTL - Get Remaining Time to Live
vb
Dim lTTL As Long
lTTL = oRedis.TTL("name")
If lTTL = -1 Then
Debug.Print "Key never expires"
ElseIf lTTL = -2 Then
Debug.Print "Key does not exist"
Else
Debug.Print "Remaining time: " & lTTL & " seconds"
End IfTTL Return Value Explanation:
-2: Key does not exist-1: Key exists but has no expiration time>= 0: Remaining time to live for the key (seconds)
Example: Complete Key Management
vb
Sub Example_KeyManagement()
Dim oRedis As New cRedisClient
If Not oRedis.Connect() Then
Debug.Print "Connection failed: " & oRedis.LastError
Exit Sub
End If
' Set key values
oRedis.Set_ "user:1001", "Zhang San"
oRedis.Set_ "user:1002", "Li Si"
oRedis.Set_ "config:timeout", "30"
' Set expiration times
oRedis.Expire "user:1001", 3600 ' 1 hour
oRedis.Expire "user:1002", 7200 ' 2 hours
' Find all user keys
Dim vKeys As Variant
vKeys = oRedis.Keys("user:*")
Debug.Print "User keys count: " & (UBound(vKeys) + 1)
' Check if key exists
If oRedis.Exists("user:1001") > 0 Then
Debug.Print "User 1001 exists, remaining time: " & oRedis.TTL("user:1001") & " seconds"
End If
' Delete config key
oRedis.Del "config:timeout"
' Disconnect
oRedis.DisConnect
End Sub