Skip to content

Set Operations ​

Redis Set is an unordered collection of unique strings, suitable for storing unique values.

SAdd - Add Set Members ​

Add Single Member ​

vb
oRedis.SAdd "myset", "apple"

Add Multiple Members ​

vb
oRedis.SAdd "myset", "banana", "orange", "grape"

Note: Sets automatically deduplicate, adding duplicate elements will only keep one.

SMembers - Get All Set Members ​

vb
' Get all members
Dim vMembers As Variant
vMembers = oRedis.SMembers("myset")

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

SCard - Get Set Member Count ​

vb
Dim lCount As Long
lCount = oRedis.SCard("myset")
Debug.Print "Set member count: " & lCount

SIsMember - Check if Member Exists ​

vb
If oRedis.SIsMember("myset", "apple") Then
    Debug.Print "Member exists"
Else
    Debug.Print "Member does not exist"
End If

SRem - Delete Set Members ​

vb
' Delete single member
oRedis.SRem "myset", "apple"

' Delete multiple members
oRedis.SRem "myset", "banana", "orange"

Use Cases ​

1. Article Tags ​

vb
Sub ArticleTags()
    Dim oRedis As New cRedisClient

    If Not oRedis.Connect() Then Exit Sub

    Dim sArticleId As String
    sArticleId = "post:1:tags"

    ' Add tags
    oRedis.SAdd sArticleId, "Technology", "Programming", "Redis", "VB6", "Database"

    ' Check tag count
    Debug.Print "Article tags count: " & oRedis.SCard(sArticleId)

    ' Check if specific tag exists
    If oRedis.SIsMember(sArticleId, "Redis") Then
        Debug.Print "This article contains Redis tag"
    End If

    ' Get all tags
    Dim vTags As Variant
    vTags = oRedis.SMembers(sArticleId)

    Debug.Print vbCrLf & "All tags:"
    Dim i As Long
    If IsArray(vTags) Then
        For i = 0 To UBound(vTags)
            Debug.Print "  - " & vTags(i)
        Next
    End If

    ' Delete specific tag
    oRedis.SRem sArticleId, "VB6"
    Debug.Print vbCrLf & "After deleting tag, remaining tags count: " & oRedis.SCard(sArticleId)

    oRedis.DisConnect
End Sub

2. User Following ​

vb
Sub UserFollowers()
    Dim oRedis As New cRedisClient

    If Not oRedis.Connect() Then Exit Sub

    Dim sUserId As String
    sUserId = "user:123"

    ' Users this user follows
    oRedis.SAdd sUserId & ":following", "user:456", "user:789", "user:101"

    ' Users following this user
    oRedis.SAdd sUserId & ":followers", "user:200", "user:300", "user:400"

    ' Check if following specific user
    If oRedis.SIsMember(sUserId & ":following", "user:456") Then
        Debug.Print "Already following user 456"
    End If

    ' Get following list
    Dim vFollowing As Variant
    vFollowing = oRedis.SMembers(sUserId & ":following")

    Debug.Print vbCrLf & "Following list:"
    Dim i As Long
    If IsArray(vFollowing) Then
        For i = 0 To UBound(vFollowing)
            Debug.Print "  " & vFollowing(i)
        Next
    End If

    ' Get follower count
    Debug.Print vbCrLf & "Follower count: " & oRedis.SCard(sUserId & ":followers")

    ' Unfollow
    oRedis.SRem sUserId & ":following", "user:789"
    Debug.Print "After unfollowing, following count: " & oRedis.SCard(sUserId & ":following")

    oRedis.DisConnect
End Sub

3. Online Users ​

vb
Sub OnlineUsers()
    Dim oRedis As New cRedisClient

    If Not oRedis.Connect() Then Exit Sub

    Dim sOnlineSet As String
    sOnlineSet = "users:online"

    ' Users come online
    oRedis.SAdd sOnlineSet, "user:1001", "user:1002", "user:1003", "user:1004"

    ' Get online user count
    Debug.Print "Online users count: " & oRedis.SCard(sOnlineSet)

    ' Check if specific user is online
    If oRedis.SIsMember(sOnlineSet, "user:1001") Then
        Debug.Print "User 1001 is online"
    End If

    ' Get all online users
    Dim vUsers As Variant
    vUsers = oRedis.SMembers(sOnlineSet)

    Debug.Print vbCrLf & "Online user list:"
    Dim i As Long
    If IsArray(vUsers) Then
        For i = 0 To UBound(vUsers)
            Debug.Print "  " & vUsers(i)
        Next
    End If

    ' User goes offline
    oRedis.SRem sOnlineSet, "user:1002"
    Debug.Print vbCrLf & "User 1002 offline, online users count: " & oRedis.SCard(sOnlineSet)

    oRedis.DisConnect
End Sub

4. IP Blacklist ​

vb
Sub IpBlacklist()
    Dim oRedis As New cRedisClient

    If Not oRedis.Connect() Then Exit Sub

    Dim sBlacklist As String
    sBlacklist = "security:blacklist"

    ' Add IP to blacklist
    oRedis.SAdd sBlacklist, "192.168.1.100"
    oRedis.SAdd sBlacklist, "192.168.1.101"
    oRedis.SAdd sBlacklist, "192.168.1.102"

    ' Check if IP is in blacklist
    Dim sCheckIp As String
    sCheckIp = "192.168.1.100"

    If oRedis.SIsMember(sBlacklist, sCheckIp) Then
        Debug.Print "IP " & sCheckIp & " is in blacklist, access denied"
    Else
        Debug.Print "IP " & sCheckIp & " can access"
    End If

    ' Get all IPs in blacklist
    Dim vIps As Variant
    vIps = oRedis.SMembers(sBlacklist)

    Debug.Print vbCrLf & "IPs in blacklist:"
    Dim i As Long
    If IsArray(vIps) Then
        For i = 0 To UBound(vIps)
            Debug.Print "  " & vIps(i)
        Next
    End If

    ' Remove IP from blacklist
    oRedis.SRem sBlacklist, "192.168.1.101"
    Debug.Print vbCrLf & "Removed 192.168.1.101 from blacklist"

    oRedis.DisConnect
End Sub

5. Unique Visitor Statistics ​

vb
Sub UniqueVisitors()
    Dim oRedis As New cRedisClient

    If Not oRedis.Connect() Then Exit Sub

    Dim sDate As String
    sDate = "2024-01-01"
    Dim sVisitorKey As String
    sVisitorKey = "stats:visitors:" & sDate

    ' Record visitor IDs
    oRedis.SAdd sVisitorKey, "visitor:001"
    oRedis.SAdd sVisitorKey, "visitor:002"
    oRedis.SAdd sVisitorKey, "visitor:003"

    ' Same visitor visits again (will not count again)
    oRedis.SAdd sVisitorKey, "visitor:001"
    oRedis.SAdd sVisitorKey, "visitor:002"

    ' Get unique visitor count for the day
    Debug.Print sDate & " unique visitors: " & oRedis.SCard(sVisitorKey)

    ' Get all visitor IDs
    Dim vVisitors As Variant
    vVisitors = oRedis.SMembers(sVisitorKey)

    Debug.Print vbCrLf & "Visitor list:"
    Dim i As Long
    If IsArray(vVisitors) Then
        For i = 0 To UBound(vVisitors)
            Debug.Print "  " & vVisitors(i)
        Next
    End If

    oRedis.DisConnect
End Sub

6. Bookmarks ​

vb
Sub Bookmarks()
    Dim oRedis As New cRedisClient

    If Not oRedis.Connect() Then Exit Sub

    Dim sUserId As String
    sUserId = "user:123"
    Dim sBookmarksKey As String
    sBookmarksKey = sUserId & ":bookmarks"

    ' Add bookmarks
    oRedis.SAdd sBookmarksKey, "article:1"
    oRedis.SAdd sBookmarksKey, "article:2"
    oRedis.SAdd sBookmarksKey, "article:3"

    ' Check if article is bookmarked
    If oRedis.SIsMember(sBookmarksKey, "article:1") Then
        Debug.Print "Article 1 is bookmarked"
    End If

    ' Get bookmark count
    Debug.Print "Bookmark count: " & oRedis.SCard(sBookmarksKey)

    ' Get all bookmarks
    Dim vBookmarks As Variant
    vBookmarks = oRedis.SMembers(sBookmarksKey)

    Debug.Print vbCrLf & "Bookmark list:"
    Dim i As Long
    If IsArray(vBookmarks) Then
        For i = 0 To UBound(vBookmarks)
            Debug.Print "  " & vBookmarks(i)
        Next
    End If

    ' Remove bookmark
    oRedis.SRem sBookmarksKey, "article:2"
    Debug.Print vbCrLf & "After removing bookmark, remaining count: " & oRedis.SCard(sBookmarksKey)

    oRedis.DisConnect
End Sub

Complete Example ​

vb
Sub Example_SetOperations()
    Dim oRedis As New cRedisClient

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

    ' Use Set to store tags
    oRedis.SAdd "post:1:tags", "Technology", "Programming", "Redis", "VB6"

    Debug.Print "Article tags count: " & oRedis.SCard("post:1:tags")

    Dim vTags As Variant
    vTags = oRedis.SMembers("post:1:tags")

    Debug.Print "All tags:"
    Dim i As Long
    For i = 0 To UBound(vTags)
        Debug.Print "  - " & vTags(i)
    Next

    ' Check if tag exists
    If oRedis.SIsMember("post:1:tags", "Redis") Then
        Debug.Print vbCrLf & "Contains Redis tag"
    End If

    ' Delete tag
    oRedis.SRem "post:1:tags", "VB6"
    Debug.Print "After deleting tag, remaining tags count: " & oRedis.SCard("post:1:tags")

    ' Add again (will not duplicate)
    oRedis.SAdd "post:1:tags", "Programming"
    Debug.Print "After adding 'Programming' again, tags count: " & oRedis.SCard("post:1:tags")

    oRedis.DisConnect
End Sub

Set vs List Selection ​

ScenarioRecommended TypeReason
Need unique valuesSetAutomatic deduplication
Allow duplicatesListCan store duplicate elements
Member checkSetO(1) time complexity
Store in orderListMaintain insertion order
Deduplication neededSetSets naturally support dedup
Stack/QueueListSupports LPush/LPop/RPush/RPop

Set Features ​

  1. Unordered: Members have no order
  2. Unique: Duplicate elements not allowed
  3. Fast lookup: Very fast to check member existence
  4. Set operations: Supports intersection, union, difference operations (requires extended implementation)

VB6 and LOGO copyright of Microsoft Corporation