在go-redis中处理Redis数据类型的方式
首先
这篇文章提供了使用go-redis实现Redis中以下三种数据结构的设置和获取的示例代码。
-
- String
-
- List
- Hash(map, dictionary)
环境
macOS: 10.13.6 高山
Redis 服务器版本:4.0.11
Go 版本:go1.11
源代码
package main
import (
"fmt"
"github.com/go-redis/redis"
)
func main() {
client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 0, // use default DB
})
fmt.Println("Redis client:", client)
fmt.Println("---------------------------------------------------")
fmt.Println("Start StringGetSet")
StringGetSet(client)
fmt.Println("---------------------------------------------------")
fmt.Println("Start ListGetSet")
ListGetSet(client)
fmt.Println("---------------------------------------------------")
fmt.Println("Start HashGetSet")
HashGetSet(client)
fmt.Println("---------------------------------------------------")
}
func StringGetSet(client *redis.Client) {
key := "StringGetSet_Key"
// Set
err := client.Set(key, "StringGetSet_Val", 0).Err()
if err != nil {
fmt.Println("redis.Client.Set Error:", err)
}
// Get
val, err := client.Get(key).Result()
if err != nil {
fmt.Println("redis.Client.Get Error:", err)
}
fmt.Println(val)
}
func ListGetSet(client *redis.Client) {
key := "ListGetSet_Key"
// Set
listVal := []string{"val1", "va2", "val3"}
err := client.RPush(key, listVal).Err()
if err != nil {
fmt.Println("redis.Client.RPush Error:", err)
}
// Get
// Get by lrange
lrangeVal, err := client.LRange(key, 0, -1).Result()
if err != nil {
fmt.Println("redis.Client.LRange Error:", err)
}
fmt.Println(lrangeVal)
// Get by lindex
lindexVal, err := client.LIndex(key, 2).Result()
if err != nil {
fmt.Println("redis.Client.LIndex Error:", err)
}
fmt.Println(lindexVal)
}
func HashGetSet(client *redis.Client) {
key := "HashGetSet_Key"
// Set
for field, val := range map[string]string{"field1": "val1", "field2": "val2"} {
fmt.Println("Inserting", "field:", field, "val:", val)
err := client.HSet(key, field, val).Err()
if err != nil {
fmt.Println("redis.Client.HSet Error:", err)
}
}
// Get
// HGet(key, field string) *StringCmd
hgetVal, err := client.HGet(key, "field1").Result()
if err != nil {
fmt.Println("redis.Client.HGet Error:", err)
}
fmt.Println(hgetVal)
// HGetAll
hgetallVal, err := client.HGetAll(key).Result()
if err != nil {
fmt.Println("redis.Client.HGetAll Error:", err)
}
// fmt.Println("reflect.TypeOf(hgetallVal):", reflect.TypeOf(hgetallVal)) // map[string]string
fmt.Println(hgetallVal)
}
运行结果
使用Docker官方镜像启动Redis。
docker run -d -p 6379:6379 --name myredis redis
运行Go代码
go run main.go
执行结果
Redis client: Redis<localhost:6379 db:0>
---------------------------------------------------
Start StringGetSet
StringGetSet_Val
---------------------------------------------------
Start ListGetSet
[val1 va2 val3]
val3
---------------------------------------------------
Start HashGetSet
Inserting field: field1 val: val1
Inserting field: field2 val: val2
val1
map[field1:val1 field2:val2]
---------------------------------------------------
请参阅
-
- https://godoc.org/github.com/go-redis/redis
- http://blog.sojiro.me/blog/2014/07/27/rediss-data-type-and-usage/