Skip to content

String Operations ​

Redis String is the simplest data type, capable of storing strings, integers, or floating-point numbers.

Set_ - Set Key-Value ​

Basic Setting ​

vb
oRedis.Set_ "name", "Zhang San"
oRedis.Set_ "age", "25"
oRedis.Set_ "city", "Beijing"

Set with Expiration ​

vb
' Set expiration time (seconds)
oRedis.Set_ "session", "abc123", , 3600  ' Expire in 1 hour

' Set expiration time (milliseconds)
oRedis.Set_ "token", "xyz789", , 60000  ' Expire in 1 minute

Get_ - Get Key Value ​

vb
Dim sValue As String
sValue = oRedis.Get_("name")
Debug.Print sValue  ' Output: Zhang San

Incr - Increment ​

Increment the numeric value in the key by 1:

vb
' Initialize counter
oRedis.Set_ "counter", "10"

' Increment
Dim lValue As Long
lValue = oRedis.Incr("counter")
Debug.Print lValue  ' Output: 11

' Increment again
lValue = oRedis.Incr("counter")
Debug.Print lValue  ' Output: 12

Use Cases:

  • Counters
  • ID generation
  • Access statistics

Decr - Decrement ​

Decrement the numeric value in the key by 1:

vb
' Decrement
lValue = oRedis.Decr("counter")
Debug.Print lValue  ' Output: 10

MGet - Batch Get ​

Get values of multiple keys simultaneously:

vb
' Get multiple keys at once
Dim vValues As Variant
vValues = oRedis.MGet("name", "age", "city")

If IsArray(vValues) Then
    Dim i As Long
    For i = 0 To UBound(vValues)
        Debug.Print vValues(i)
    Next
End If

Return Value Explanation:

  • Returns corresponding value if key exists
  • Returns empty string if key does not exist

MSet - Batch Set ​

Batch set multiple key-value pairs:

vb
' Batch set key-value pairs
oRedis.MSet "name", "Li Si", "age", "25", "city", "Beijing"

Use Cases ​

1. User Session Management ​

vb
Sub UserSession()
    Dim oRedis As New cRedisClient

    If Not oRedis.Connect() Then Exit Sub

    Dim sUserId As String
    sUserId = "user:12345"

    ' Create session
    oRedis.Set_ sUserId & ":name", "Zhang San"
    oRedis.Set_ sUserId & ":email", "zhangsan@example.com"
    oRedis.Set_ sUserId & ":login_time", CStr(Now())

    ' Set session expiration time to 30 minutes
    oRedis.Expire sUserId & ":name", 1800
    oRedis.Expire sUserId & ":email", 1800
    oRedis.Expire sUserId & ":login_time", 1800

    ' Get session information
    Dim vSession As Variant
    vSession = oRedis.MGet(sUserId & ":name", sUserId & ":email")
    If IsArray(vSession) Then
        Debug.Print "User: " & vSession(0)
        Debug.Print "Email: " & vSession(1)
        Debug.Print "Remaining time: " & oRedis.TTL(sUserId & ":name") & " seconds"
    End If

    oRedis.DisConnect
End Sub

2. Counter ​

vb
Sub CounterExample()
    Dim oRedis As New cRedisClient

    If Not oRedis.Connect() Then Exit Sub

    ' Page visit count
    Dim sPage As String
    sPage = "page:/home"

    ' Increment visit count
    Dim lCount As Long
    lCount = oRedis.Incr(sPage)
    Debug.Print "Page visit count: " & lCount

    ' Article like count
    oRedis.Incr "article:1001:likes"
    oRedis.Incr "article:1001:likes"
    oRedis.Incr "article:1001:likes"

    Debug.Print "Article likes: " & oRedis.Get_("article:1001:likes")

    oRedis.DisConnect
End Sub

3. Distributed ID Generation ​

vb
Function GenerateId(ByVal sPrefix As String) As String
    Static oRedis As cRedisClient

    If oRedis Is Nothing Then
        Set oRedis = New cRedisClient
        If Not oRedis.Connect() Then
            Debug.Print "Connection failed"
            Exit Function
        End If
    End If

    Dim lId As Long
    lId = oRedis.Incr("id:" & sPrefix)

    GenerateId = sPrefix & ":" & lId
End Function

Sub TestIdGeneration()
    Debug.Print GenerateId("order")    ' Output: order:1
    Debug.Print GenerateId("order")    ' Output: order:2
    Debug.Print GenerateId("user")     ' Output: user:1
    Debug.Print GenerateId("order")    ' Output: order:3
End Sub

4. Rate Limiter ​

vb
Function RateLimit(ByVal sUserId As String, ByVal lLimit As Long) As Boolean
    Static oRedis As cRedisClient

    If oRedis Is Nothing Then
        Set oRedis = New cRedisClient
        If Not oRedis.Connect() Then
            Exit Function
        End If
    End If

    Dim sKey As String
    sKey = "ratelimit:" & sUserId

    Dim lCount As Long
    lCount = oRedis.Incr(sKey)

    ' Set expiration time on first request (1 minute)
    If lCount = 1 Then
        oRedis.Expire sKey, 60
    End If

    ' Check if limit exceeded
    If lCount > lLimit Then
        Debug.Print "Limit exceeded, current requests: " & lCount
        RateLimit = False
    Else
        RateLimit = True
    End If
End Function

Sub TestRateLimit()
    Dim i As Long
    For i = 1 To 12
        Debug.Print "Request " & i & ": " & IIf(RateLimit("user123", 10), "Allowed", "Denied")
    Next i
End Sub

5. Cache Management ​

vb
Function GetCachedData(ByVal sKey As String, ByVal sDefault As String) As String
    Static oRedis As cRedisClient

    If oRedis Is Nothing Then
        Set oRedis = New cRedisClient
        oRedis.Connect
    End If

    Dim sValue As String
    sValue = oRedis.Get_(sKey)

    If sValue = "" Then
        ' Cache miss, return default value
        GetCachedData = sDefault

        ' Store default value in cache with 1 hour expiration
        oRedis.Set_ sKey, sDefault, , 3600
    Else
        GetCachedData = sValue
    End If
End Function

Sub TestCache()
    Debug.Print "First fetch: " & GetCachedData("config:appname", "MyApp")
    Debug.Print "Second fetch: " & GetCachedData("config:appname", "MyApp")
End Sub

Complete Example ​

vb
Sub Example_StringOperations()
    Dim oRedis As New cRedisClient

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

    ' Set values
    oRedis.Set_ "name", "Zhang San"
    oRedis.Set_ "age", "25"

    ' Get values
    Debug.Print "Name: " & oRedis.Get_("name")
    Debug.Print "Age: " & oRedis.Get_("age")

    ' Set expiration time
    oRedis.Expire "name", 300

    ' Check remaining time
    Debug.Print "name remaining TTL: " & oRedis.TTL("name") & " seconds"

    ' Counter example
    oRedis.Set_ "counter", "10"
    Debug.Print "Initial value: " & oRedis.Get_("counter")
    Debug.Print "After increment: " & oRedis.Incr("counter")
    Debug.Print "Increment again: " & oRedis.Incr("counter")
    Debug.Print "After decrement: " & oRedis.Decr("counter")

    ' Batch operations
    oRedis.MSet "user:1", "Zhang San", "user:2", "Li Si", "user:3", "Wang Wu"
    Dim vUsers As Variant
    vUsers = oRedis.MGet("user:1", "user:2", "user:3")

    Debug.Print vbCrLf & "Batch fetch results:"
    Dim i As Long
    If IsArray(vUsers) Then
        For i = 0 To UBound(vUsers)
            Debug.Print "  User" & (i + 1) & ": " & vUsers(i)
        Next
    End If

    oRedis.DisConnect
End Sub

VB6 and LOGO copyright of Microsoft Corporation