List Operations
Redis List is a simple string list, ordered by insertion order. Supports inserting and popping elements from both ends of the list.
LPush - Insert from Left
Insert Single Value
vb
oRedis.LPush "mylist", "item1"Insert Multiple Values
vb
oRedis.LPush "mylist", "item2", "item3"Note: Elements inserted using LPush appear at the beginning of the list.
RPush - Insert from Right
vb
' Insert from right
oRedis.RPush "mylist", "item4"Note: Elements inserted using RPush appear at the end of the list.
LPop - Pop from Left
vb
Dim sValue As String
sValue = oRedis.LPop("mylist")
Debug.Print sValue ' Output: leftmost elementRPop - Pop from Right
vb
sValue = oRedis.RPop("mylist")
Debug.Print sValue ' Output: rightmost elementLLen - Get List Length
vb
Dim lLen As Long
lLen = oRedis.lLen("mylist")
Debug.Print "List length: " & lLenLRange - Get Elements in Range
Get All Elements
vb
Dim vItems As Variant
vItems = oRedis.LRange("mylist", 0, -1)
If IsArray(vItems) Then
Dim i As Long
For i = 0 To UBound(vItems)
Debug.Print vItems(i)
Next
End IfGet First 10 Elements
vb
vItems = oRedis.LRange("mylist", 0, 9)Get Specified Range Elements
vb
' Get elements from 6th to 10th (index starts from 0)
vItems = oRedis.LRange("mylist", 5, 9)Use Cases
1. Task Queue
vb
Sub TaskQueue()
Dim oRedis As New cRedisClient
If Not oRedis.Connect() Then Exit Sub
Dim sQueue As String
sQueue = "tasks:queue"
' Add tasks to queue
oRedis.RPush sQueue, "Send email to user1"
oRedis.RPush sQueue, "Generate report"
oRedis.RPush sQueue, "Backup database"
oRedis.RPush sQueue, "Clear cache"
' View queue length
Debug.Print "Pending tasks: " & oRedis.lLen(sQueue)
' View all tasks
Dim vTasks As Variant
vTasks = oRedis.LRange(sQueue, 0, -1)
Debug.Print vbCrLf & "All tasks:"
Dim i As Long
For i = 0 To UBound(vTasks)
Debug.Print " " & (i + 1) & ". " & vTasks(i)
Next
' Process tasks (pop from left)
Debug.Print vbCrLf & "Processing tasks..."
While oRedis.lLen(sQueue) > 0
Dim sTask As String
sTask = oRedis.LPop(sQueue)
Debug.Print "Processing: " & sTask
Debug.Print "Remaining: " & oRedis.lLen(sQueue)
Wend
oRedis.DisConnect
End Sub2. Message Queue
vb
Sub MessageQueue()
Dim oRedis As New cRedisClient
If Not oRedis.Connect() Then Exit Sub
Dim sQueue As String
sQueue = "messages:inbox"
' Send messages (producer)
oRedis.RPush sQueue, "Message1: Hello"
oRedis.RPush sQueue, "Message2: Redis is great"
oRedis.RPush sQueue, "Message3: Learning"
' Receive messages (consumer)
Debug.Print "New messages:"
Dim lCount As Long
lCount = oRedis.lLen(sQueue)
For i = 1 To lCount
Dim sMsg As String
sMsg = oRedis.LPop(sQueue)
Debug.Print " " & sMsg
Next
oRedis.DisConnect
End Sub3. History Records
vb
Sub SearchHistory()
Dim oRedis As New cRedisClient
If Not oRedis.Connect() Then Exit Sub
Dim sUserId As String
sUserId = "user:123:history"
' Add search records
oRedis.LPush sUserId, "Redis tutorial"
oRedis.LPush sUserId, "VB6 programming"
oRedis.LPush sUserId, "Database design"
' Limit history records (keep only recent 10)
While oRedis.lLen(sUserId) > 10
oRedis.RPop sUserId
Wend
' Get recent search records
Dim vHistory As Variant
vHistory = oRedis.LRange(sUserId, 0, 4) ' Recent 5
Debug.Print "Recent search records:"
Dim i As Long
If IsArray(vHistory) Then
For i = 0 To UBound(vHistory)
Debug.Print " " & (i + 1) & ". " & vHistory(i)
Next
End If
oRedis.DisConnect
End Sub4. Recent Activity
vb
Sub RecentActivity()
Dim oRedis As New cRedisClient
If Not oRedis.Connect() Then Exit Sub
Dim sKey As String
sKey = "activity:recent"
' Add activities
oRedis.LPush sKey, "User Zhang San published new article"
oRedis.LPush sKey, "User Li Si commented on article"
oRedis.LPush sKey, "User Wang Wu liked post"
oRedis.LPush sKey, "User Zhao Liu followed new user"
' Keep only latest 20 activities
While oRedis.lLen(sKey) > 20
oRedis.RPop sKey
Wend
' Show recent activities
Dim vActivities As Variant
vActivities = oRedis.LRange(sKey, 0, 9)
Debug.Print "Recent activities:"
Dim i As Long
If IsArray(vActivities) Then
For i = 0 To UBound(vActivities)
Debug.Print " " & (i + 1) & ". " & vActivities(i)
Next
End If
oRedis.DisConnect
End Sub5. Stack (LIFO)
vb
Sub StackExample()
Dim oRedis As New cRedisClient
If Not oRedis.Connect() Then Exit Sub
Dim sStack As String
sStack = "stack:operations"
' Push operations
Debug.Print "Push operations:"
oRedis.LPush sStack, "Element A"
Debug.Print " Pushed: Element A"
oRedis.LPush sStack, "Element B"
Debug.Print " Pushed: Element B"
oRedis.LPush sStack, "Element C"
Debug.Print " Pushed: Element C"
Debug.Print "Stack depth: " & oRedis.lLen(sStack)
' Pop operations
Debug.Print vbCrLf & "Pop operations:"
While oRedis.lLen(sStack) > 0
Dim sItem As String
sItem = oRedis.LPop(sStack)
Debug.Print " Popped: " & sItem
Debug.Print " Remaining: " & oRedis.lLen(sStack)
Wend
oRedis.DisConnect
End Sub6. Queue (FIFO)
vb
Sub QueueExample()
Dim oRedis As New cRedisClient
If Not oRedis.Connect() Then Exit Sub
Dim sQueue As String
sQueue = "queue:customers"
' Enqueue operations
Debug.Print "Enqueue operations:"
oRedis.RPush sQueue, "Customer A"
Debug.Print " Enqueued: Customer A"
oRedis.RPush sQueue, "Customer B"
Debug.Print " Enqueued: Customer B"
oRedis.RPush sQueue, "Customer C"
Debug.Print " Enqueued: Customer C"
Debug.Print "Queue length: " & oRedis.lLen(sQueue)
' Dequeue operations
Debug.Print vbCrLf & "Dequeue operations:"
While oRedis.lLen(sQueue) > 0
Dim sCustomer As String
sCustomer = oRedis.LPop(sQueue)
Debug.Print " Serving: " & sCustomer
Debug.Print " Waiting: " & oRedis.lLen(sQueue)
Wend
oRedis.DisConnect
End SubComplete Example
vb
Sub Example_ListOperations()
Dim oRedis As New cRedisClient
If Not oRedis.Connect() Then
Debug.Print "Connection failed: " & oRedis.LastError
Exit Sub
End If
' Clear existing list
oRedis.Del "tasks"
' Add tasks
oRedis.RPush "tasks", "Task1"
oRedis.RPush "tasks", "Task2"
oRedis.RPush "tasks", "Task3"
oRedis.RPush "tasks", "Task4"
oRedis.RPush "tasks", "Task5"
' Get list length
Debug.Print "Task list length: " & oRedis.lLen("tasks")
' Get all tasks
Dim vTasks As Variant
vTasks = oRedis.LRange("tasks", 0, -1)
Debug.Print vbCrLf & "All tasks:"
Dim i As Long
For i = 0 To UBound(vTasks)
Debug.Print " " & (i + 1) & ". " & vTasks(i)
Next
' Process first task
Debug.Print vbCrLf & "Processing task: " & oRedis.LPop("tasks")
Debug.Print "Remaining tasks: " & oRedis.lLen("tasks")
' Add new task to front
oRedis.LPush "tasks", "Urgent task"
' View updated list
vTasks = oRedis.LRange("tasks", 0, -1)
Debug.Print vbCrLf & "Updated task list:"
If IsArray(vTasks) Then
For i = 0 To UBound(vTasks)
Debug.Print " " & (i + 1) & ". " & vTasks(i)
Next
End If
oRedis.DisConnect
End SubList Operations Summary
| Operation | Method | Description |
|---|---|---|
| Left insert | LPush | Insert from list beginning |
| Right insert | RPush | Insert from list end |
| Left pop | LPop | Pop from list beginning |
| Right pop | RPop | Pop from list end |
| Get length | LLen | Get number of list elements |
| Get range | LRange | Get elements in specified range |
Usage Recommendations
- Task Queue: Use
RPush+LPopfor FIFO - Stack: Use
LPush+LPopfor LIFO - Limit Size: Use
RPush+RPoporLPush+RPopto control list length - History Records: Use
LPushto add new records,LRANGEto get recent records