Skip to content

Connection Management ​

Basic Connection ​

Connect to Local Redis Server ​

vb
Dim oRedis As New cRedisClient

' Connect to local Redis server
If oRedis.Connect() Then
    Debug.Print "Connected successfully!"
Else
    Debug.Print "Connection failed: " & oRedis.LastError
End If

Connect to Specified Server ​

vb
Dim oRedis As New cRedisClient

' Connect to specified address and port
If oRedis.Connect("192.168.1.100", 6379) Then
    Debug.Print "Connected successfully!"
End If

Connect with Password Authentication ​

vb
Dim oRedis As New cRedisClient

' Connect and use password authentication
If oRedis.Connect("127.0.0.1", 6379, "mypassword") Then
    Debug.Print "Connected successfully!"
End If

Connect with ACL Username and Password (Redis 6.0+) ​

vb
Dim oRedis As New cRedisClient

' Use ACL authentication (requires Redis 6.0+ with ACL user configured)
If oRedis.Connect("127.0.0.1", 6379, "mypassword", "myusername") Then
    Debug.Print "ACL authentication connected successfully!"
End If

Set Timeout ​

vb
Dim oRedis As New cRedisClient

' Set timeout to 10 seconds
oRedis.Timeout = 10
If oRedis.Connect() Then
    Debug.Print "Connected successfully!"
End If

Disconnect ​

vb
oRedis.DisConnect

Connection Status Check ​

Check connection status before executing operations:

vb
' Check internal connection flag
If Not oRedis.Connected Then
    Debug.Print "Not connected to Redis server"
End If

' Check actual Socket connection status (more accurate, syncs internal flag)
If Not oRedis.IsConnected Then
    Debug.Print "Socket disconnected, attempting to reconnect..."
    If Not oRedis.Connect() Then
        Debug.Print "Reconnection failed: " & oRedis.LastError
        Exit Sub
    End If
End If

Event Handling ​

OnDisconnected - Disconnection Event ​

vb
' Declare WithEvents variable in class module
Private WithEvents m_oRedis As cRedisClient

Private Sub m_oRedis_OnDisconnected()
    Debug.Print "Redis connection lost"
    ' Implement reconnection logic here
End Sub

OnError - Error Event ​

vb
Private Sub m_oRedis_OnError(ByVal ErrorMsg As String)
    Debug.Print "Redis error: " & ErrorMsg
    ' Implement error handling logic here
End Sub

Connection Best Practices ​

1. Using Pattern ​

vb
Sub DoRedisWork()
    Dim oRedis As New cRedisClient

    If Not oRedis.Connect() Then
        Debug.Print "Connection failed"
        Exit Sub
    End If

    ' Execute operations
    oRedis.Set_ "key", "value"

    ' Ensure disconnection
    oRedis.DisConnect
    Set oRedis = Nothing
End Sub

2. Error Handling ​

vb
Sub SafeRedisOperation()
    Dim oRedis As New cRedisClient

    On Error GoTo ErrorHandler

    If Not oRedis.Connect() Then
        Debug.Print "Connection failed: " & oRedis.LastError
        Exit Sub
    End If

    ' Execute operations
    oRedis.Set_ "key", "value"

    ' Check for errors
    If oRedis.LastError <> "" Then
        Debug.Print "Operation error: " & oRedis.LastError
    End If

    Exit Sub

ErrorHandler:
    Debug.Print "Error occurred: " & Err.Description
    If oRedis.Connected Then
        oRedis.DisConnect
    End If
End Sub

3. Heartbeat Keep-alive ​

vb
Dim oRedis As New cRedisClient

' Connect to Redis
If oRedis.Connect("127.0.0.1", 6379, "mypassword") Then
    ' Set heartbeat interval (seconds), default 30 seconds
    oRedis.HeartbeatInterval = 30
    
    ' Start heartbeat (periodically sends PING to keep connection active)
    oRedis.StartHeartbeat
    
    ' Execute business operations...
    oRedis.Set_ "key", "value"
    
    ' Stop heartbeat and disconnect
    oRedis.StopHeartbeat
    oRedis.DisConnect
End If

4. Connection Pool Example ​

vb
' Simple connection manager example
Private m_oRedis As cRedisClient

Function GetRedisConnection() As cRedisClient
    If m_oRedis Is Nothing Then
        Set m_oRedis = New cRedisClient
        If Not m_oRedis.Connect() Then
            Debug.Print "Failed to create connection: " & m_oRedis.LastError
            Set GetRedisConnection = Nothing
            Exit Function
        End If
    End If

    ' Check if connection is still valid
    If Not m_oRedis.Connected Then
        If Not m_oRedis.Connect() Then
            Debug.Print "Reconnection failed: " & m_oRedis.LastError
            Set GetRedisConnection = Nothing
            Exit Function
        End If
    End If

    Set GetRedisConnection = m_oRedis
End Function

Sub CloseRedisConnection()
    If Not m_oRedis Is Nothing Then
        m_oRedis.StopHeartbeat  ' Stop heartbeat first
        m_oRedis.DisConnect
        Set m_oRedis = Nothing
    End If
End Sub

Automatic Reconnection ​

Since v1.x, auto-reconnect is enabled by default. When connection drops unexpectedly:

  • Connected / IsConnected returns False
  • Commands will automatically try to reconnect once
  • If reconnection succeeds, command continues; if fails, error is raised
  • Combined with heartbeat mechanism to detect dead connections promptly
vb
' Disable auto-reconnect (not recommended)
oRedis.AutoReconnect = False

' Enable auto-reconnect (default)
oRedis.AutoReconnect = True

VB6 and LOGO copyright of Microsoft Corporation