Skip to content

Hash Operations

Redis Hash is a collection of key-value pairs, suitable for storing objects. Each Hash can contain multiple field-value pairs.

HSet - Set Hash Field

Set Single Field

vb
oRedis.HSet "user:1", "name", "Zhang San"
oRedis.HSet "user:1", "age", "25"
oRedis.HSet "user:1", "email", "zhangsan@example.com"

Batch Set Fields

vb
oRedis.HSet "user:1", "name", "Li Si"
oRedis.HSet "user:1", "age", "30"
oRedis.HSet "user:1", "city", "Shanghai"
oRedis.HSet "user:1", "email", "lisi@example.com"

HGet - Get Hash Field

vb
Dim sValue As String
sValue = oRedis.HGet("user:1", "name")
Debug.Print sValue  ' Output: Zhang San

HMGet - Batch Get Hash Fields

vb
' Batch get fields
Dim vValues As Variant
vValues = oRedis.HMGet("user:1", "name", "age", "email")

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

HGetAll - Get All Hash Fields

Get all fields and values in a Hash, returns Scripting.Dictionary object:

vb
' Get all fields and values
Dim oDict As Scripting.Dictionary
Set oDict = oRedis.HGetAll("user:1")

' Traverse dictionary
Dim vKey As Variant
For Each vKey In oDict.Keys
    Debug.Print vKey & ": " & oDict(vKey)
Next

Return Value Type: Scripting.Dictionary

Note: Need to add reference to "Microsoft Scripting Runtime" before use.

HDel - Delete Hash Field

vb
' Delete single field
oRedis.HDel "user:1", "email"

' Delete multiple fields
oRedis.HDel "user:1", "age", "email"

HExists - Check if Hash Field Exists

vb
If oRedis.HExists("user:1", "name") Then
    Debug.Print "Field exists"
Else
    Debug.Print "Field does not exist"
End If

Use Cases

1. User Information Storage

vb
Sub StoreUserInfo()
    Dim oRedis As New cRedisClient

    If Not oRedis.Connect() Then Exit Sub

    Dim lUserId As Long
    lUserId = 1001

    ' Store user information
    oRedis.HSet "user:" & lUserId, "name", "Zhang San"
    oRedis.HSet "user:" & lUserId, "age", "25"
    oRedis.HSet "user:" & lUserId, "email", "zhangsan@example.com"
    oRedis.HSet "user:" & lUserId, "phone", "13800138000"
    oRedis.HSet "user:" & lUserId, "city", "Beijing"

    ' Get user name
    Debug.Print "User name: " & oRedis.HGet("user:" & lUserId, "name")

    ' Get user email
    Debug.Print "User email: " & oRedis.HGet("user:" & lUserId, "email")

    ' Get all user information
    Dim oDict As Scripting.Dictionary
    Set oDict = oRedis.HGetAll("user:" & lUserId)

    Debug.Print vbCrLf & "User details:"
    Dim vKey As Variant
    For Each vKey In oDict.Keys
        Debug.Print "  " & vKey & ": " & oDict(vKey)
    Next

    oRedis.DisConnect
End Sub

2. Shopping Cart

vb
Sub ShoppingCart()
    Dim oRedis As New cRedisClient

    If Not oRedis.Connect() Then Exit Sub

    Dim sUserId As String
    sUserId = "cart:user123"

    ' Add products to cart
    oRedis.HSet sUserId, "product:1001", "2"  ' Product ID: Quantity
    oRedis.HSet sUserId, "product:1002", "1"
    oRedis.HSet sUserId, "product:1003", "3"

    ' Get quantity of a product in cart
    Debug.Print "Quantity of product 1001: " & oRedis.HGet(sUserId, "product:1001")

    ' Update product quantity
    oRedis.HSet sUserId, "product:1001", "5"

    ' Get all products in cart
    Dim oCart As Scripting.Dictionary
    Set oCart = oRedis.HGetAll(sUserId)

    Debug.Print vbCrLf & "Cart contents:"
    Dim vProductId As Variant
    Dim lTotal As Long
    lTotal = 0

    For Each vProductId In oCart.Keys
        Dim lCount As Long
        lCount = CLng(oCart(vProductId))
        Debug.Print "  " & vProductId & ": " & lCount & " items"
        lTotal = lTotal + lCount
    Next

    Debug.Print "Total items: " & lTotal

    ' Remove product from cart
    oRedis.HDel sUserId, "product:1002"

    oRedis.DisConnect
End Sub

3. Article Metadata

vb
Sub ArticleMetadata()
    Dim oRedis As New cRedisClient

    If Not oRedis.Connect() Then Exit Sub

    Dim sArticleId As String
    sArticleId = "article:12345"

    ' Store article metadata
    oRedis.HSet sArticleId, "title", "Redis Tutorial"
    oRedis.HSet sArticleId, "author", "Zhang San"
    oRedis.HSet sArticleId, "views", "1000"
    oRedis.HSet sArticleId, "likes", "50"
    oRedis.HSet sArticleId, "comments", "20"
    oRedis.HSet sArticleId, "created_at", "2024-01-01"

    ' Get article title
    Debug.Print "Article title: " & oRedis.HGet(sArticleId, "title")

    ' Get author and view count
    Dim vInfo As Variant
    vInfo = oRedis.HMGet(sArticleId, "author", "views")
    If IsArray(vInfo) Then
        Debug.Print "Author: " & vInfo(0)
        Debug.Print "Views: " & vInfo(1)
    End If

    ' Increment view count
    Dim lViews As Long
    lViews = CLng(oRedis.HGet(sArticleId, "views"))
    oRedis.HSet sArticleId, "views", CStr(lViews + 1)
    Debug.Print "Updated views: " & oRedis.HGet(sArticleId, "views")

    ' Check if article has comments
    If oRedis.HExists(sArticleId, "comments") Then
        Debug.Print "Comments: " & oRedis.HGet(sArticleId, "comments")
    End If

    oRedis.DisConnect
End Sub

4. Configuration Management

vb
Sub ConfigManager()
    Dim oRedis As New cRedisClient

    If Not oRedis.Connect() Then Exit Sub

    Dim sConfigKey As String
    sConfigKey = "config:app"

    ' Load configuration
    oRedis.HSet sConfigKey, "appname", "MyApp"
    oRedis.HSet sConfigKey, "version", "1.0.0"
    oRedis.HSet sConfigKey, "debug", "false"
    oRedis.HSet sConfigKey, "timeout", "30"
    oRedis.HSet sConfigKey, "max_users", "1000"

    ' Get single configuration
    Debug.Print "Application name: " & oRedis.HGet(sConfigKey, "appname")

    ' Batch get configuration
    Dim vConfigs As Variant
    vConfigs = oRedis.HMGet(sConfigKey, "debug", "timeout")

    If IsArray(vConfigs) Then
        Debug.Print "Debug mode: " & vConfigs(0)
        Debug.Print "Timeout: " & vConfigs(1) & " seconds"
    End If

    ' Update configuration
    oRedis.HSet sConfigKey, "debug", "true"
    oRedis.HSet sConfigKey, "version", "1.1.0"

    ' Get all configuration
    Dim oAllConfig As Scripting.Dictionary
    Set oAllConfig = oRedis.HGetAll(sConfigKey)

    Debug.Print vbCrLf & "All configuration:"
    Dim vKey As Variant
    For Each vKey In oAllConfig.Keys
        Debug.Print "  " & vKey & " = " & oAllConfig(vKey)
    Next

    oRedis.DisConnect
End Sub

5. Online User Status

vb
Sub OnlineUserStatus()
    Dim oRedis As New cRedisClient

    If Not oRedis.Connect() Then Exit Sub

    Dim sUserId As String
    sUserId = "user:123"

    ' Update user online status
    oRedis.HSet sUserId, "status", "online"
    oRedis.HSet sUserId, "last_seen", CStr(Now())
    oRedis.HSet sUserId, "ip", "192.168.1.100"

    ' Check if user is online
    Dim sStatus As String
    sStatus = oRedis.HGet(sUserId, "status")
    Debug.Print "User status: " & sStatus

    ' Get user last active time
    Debug.Print "Last active: " & oRedis.HGet(sUserId, "last_seen")

    ' User offline
    oRedis.HSet sUserId, "status", "offline"

    oRedis.DisConnect
End Sub

Complete Example

vb
Sub Example_HashOperations()
    Dim oRedis As New cRedisClient

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

    ' Set user information
    oRedis.HSet "user:1001", "name", "Li Si"
    oRedis.HSet "user:1001", "age", "30"
    oRedis.HSet "user:1001", "city", "Shanghai"
    oRedis.HSet "user:1001", "email", "lisi@example.com"

    ' Get single field
    Debug.Print "User name: " & oRedis.HGet("user:1001", "name")

    ' Get all fields
    Dim oDict As Scripting.Dictionary
    Set oDict = oRedis.HGetAll("user:1001")

    Debug.Print vbCrLf & "User details:"
    Dim vKey As Variant
    For Each vKey In oDict.Keys
        Debug.Print "  " & vKey & ": " & oDict(vKey)
    Next

    ' Check if field exists
    If oRedis.HExists("user:1001", "name") Then
        Debug.Print vbCrLf & "Name field exists"
    End If

    ' Delete field
    oRedis.HDel "user:1001", "email"
    Debug.Print "Deleted email field"

    oRedis.DisConnect
End Sub

Hash vs String Selection

ScenarioRecommended TypeReason
Store single valueStringSimple and efficient
Store object fieldsHashMore memory efficient, supports field operations
Frequently update single fieldHashOnly update modified fields
Need to batch get partial fieldsHashUse HMGet for better efficiency
Simple cacheStringSimple implementation

VB6 and LOGO copyright of Microsoft Corporation