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

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
If Not oRedis.Connected Then
    Debug.Print "Not connected to Redis server"
    ' Try 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. 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.DisConnect
        Set m_oRedis = Nothing
    End If
End Sub

VB6 and LOGO copyright of Microsoft Corporation