String 操作
Redis String 是最简单的数据类型,可以存储字符串、整数或浮点数。
Set_ - 设置键值
基本设置
vb
oRedis.Set_ "name", "张三"
oRedis.Set_ "age", "25"
oRedis.Set_ "city", "北京"设置过期时间
vb
' 设置过期时间(秒)
oRedis.Set_ "session", "abc123", , 3600 ' 1小时后过期
' 设置过期时间(毫秒)
oRedis.Set_ "token", "xyz789", , 60000 ' 1分钟后过期Get_ - 获取键值
vb
Dim sValue As String
sValue = oRedis.Get_("name")
Debug.Print sValue ' 输出: 张三Incr - 自增
将键中的数值加 1:
vb
' 初始化计数器
oRedis.Set_ "counter", "10"
' 自增
Dim lValue As Long
lValue = oRedis.Incr("counter")
Debug.Print lValue ' 输出: 11
' 再次自增
lValue = oRedis.Incr("counter")
Debug.Print lValue ' 输出: 12用途:
- 计数器
- ID 生成
- 访问统计
Decr - 自减
将键中的数值减 1:
vb
' 自减
lValue = oRedis.Decr("counter")
Debug.Print lValue ' 输出: 10MGet - 批量获取
同时获取多个键的值:
vb
' 同时获取多个键
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返回值说明:
- 键存在时,返回对应的值
- 键不存在时,返回空字符串
MSet - 批量设置
批量设置多个键值对:
vb
' 批量设置键值对
oRedis.MSet "name", "李四", "age", "25", "city", "北京"应用场景
1. 用户会话管理
vb
Sub UserSession()
Dim oRedis As New cRedisClient
If Not oRedis.Connect() Then Exit Sub
Dim sUserId As String
sUserId = "user:12345"
' 创建会话
oRedis.Set_ sUserId & ":name", "张三"
oRedis.Set_ sUserId & ":email", "zhangsan@example.com"
oRedis.Set_ sUserId & ":login_time", CStr(Now())
' 设置会话过期时间为 30 分钟
oRedis.Expire sUserId & ":name", 1800
oRedis.Expire sUserId & ":email", 1800
oRedis.Expire sUserId & ":login_time", 1800
' 获取会话信息
Dim vSession As Variant
vSession = oRedis.MGet(sUserId & ":name", sUserId & ":email")
If IsArray(vSession) Then
Debug.Print "用户: " & vSession(0)
Debug.Print "邮箱: " & vSession(1)
Debug.Print "剩余时间: " & oRedis.TTL(sUserId & ":name") & " 秒"
End If
oRedis.DisConnect
End Sub2. 计数器
vb
Sub CounterExample()
Dim oRedis As New cRedisClient
If Not oRedis.Connect() Then Exit Sub
' 页面访问计数
Dim sPage As String
sPage = "page:/home"
' 增加访问次数
Dim lCount As Long
lCount = oRedis.Incr(sPage)
Debug.Print "页面访问次数: " & lCount
' 文章点赞计数
oRedis.Incr "article:1001:likes"
oRedis.Incr "article:1001:likes"
oRedis.Incr "article:1001:likes"
Debug.Print "文章点赞数: " & oRedis.Get_("article:1001:likes")
oRedis.DisConnect
End Sub3. 分布式 ID 生成
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 "连接失败"
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") ' 输出: order:1
Debug.Print GenerateId("order") ' 输出: order:2
Debug.Print GenerateId("user") ' 输出: user:1
Debug.Print GenerateId("order") ' 输出: order:3
End Sub4. 限流器
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)
' 第一次请求时设置过期时间(1分钟)
If lCount = 1 Then
oRedis.Expire sKey, 60
End If
' 检查是否超过限制
If lCount > lLimit Then
Debug.Print "超过限制,当前请求: " & lCount
RateLimit = False
Else
RateLimit = True
End If
End Function
Sub TestRateLimit()
Dim i As Long
For i = 1 To 12
Debug.Print "请求 " & i & ": " & IIf(RateLimit("user123", 10), "允许", "拒绝")
Next i
End Sub5. 缓存管理
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
' 缓存不存在,返回默认值
GetCachedData = sDefault
' 将默认值存入缓存,过期时间 1 小时
oRedis.Set_ sKey, sDefault, , 3600
Else
GetCachedData = sValue
End If
End Function
Sub TestCache()
Debug.Print "第一次获取: " & GetCachedData("config:appname", "MyApp")
Debug.Print "第二次获取: " & GetCachedData("config:appname", "MyApp")
End Sub完整示例
vb
Sub Example_StringOperations()
Dim oRedis As New cRedisClient
If Not oRedis.Connect() Then
Debug.Print "连接失败: " & oRedis.LastError
Exit Sub
End If
' 设置值
oRedis.Set_ "name", "张三"
oRedis.Set_ "age", "25"
' 获取值
Debug.Print "姓名: " & oRedis.Get_("name")
Debug.Print "年龄: " & oRedis.Get_("age")
' 设置过期时间
oRedis.Expire "name", 300
' 检查剩余时间
Debug.Print "name 的剩余生存时间: " & oRedis.TTL("name") & " 秒"
' 计数器示例
oRedis.Set_ "counter", "10"
Debug.Print "初始值: " & oRedis.Get_("counter")
Debug.Print "自增后: " & oRedis.Incr("counter")
Debug.Print "再自增: " & oRedis.Incr("counter")
Debug.Print "自减: " & oRedis.Decr("counter")
' 批量操作
oRedis.MSet "user:1", "张三", "user:2", "李四", "user:3", "王五"
Dim vUsers As Variant
vUsers = oRedis.MGet("user:1", "user:2", "user:3")
Debug.Print vbCrLf & "批量获取结果:"
Dim i As Long
If IsArray(vUsers) Then
For i = 0 To UBound(vUsers)
Debug.Print " 用户" & (i + 1) & ": " & vUsers(i)
Next
End If
oRedis.DisConnect
End Sub