ZUnionStore 可实现key的复制功能。
ZRangeWithScores 获取集合类型的key中数据,0 -1代表获取所有数据。
各个函数功能可参考官网链接。
https://cheatography.com/tasjaevan/cheat-sheets/redis/
package main
import (
"fmt"
"github.com/go-redis/redis"
ddredis "gopkg.in/CodapeWild/dd-trace-go.v1/contrib/go-redis/redis"
)
func main() {
// init redis conncetions
redisOpt := redis.Options{
Addr: "127.0.0.1:30180",//自己填写
DB: 0,
}
redisOpt.Password = "*****"//自己填写
key := "test11"
key2 := "test22"
testRedis := ddredis.NewClient(&redisOpt, ddredis.WithServiceName("test.redis"))
res := testRedis.ZIncr(key, redis.Z{Score: 2.0, Member: "test2"})
res1, _ := testRedis.ZRangeWithScores(key, 0, -1).Result()
fmt.Printf("res:%v\n", res)//res:zadd test11 incr 2 test2: 2
fmt.Printf("res1:%v\n", res1)//res1:[{2 test2}]
testRedis.ZUnionStore(key2, redis.ZStore{Weights: []float64{1}}, key)
res2, _ := testRedis.ZRangeWithScores(key2, 0, -1).Result()
fmt.Printf("res2:%v\n", res2)//res2:[{2 test2}]
_, err := config.Redis.ZRank(curKey, "test").Result()
if err != nil {
//可判断key是否存在
}
config.Redis.ZAdd(curKey, redis.Z{Score: 90, Member: "test"})//只添加key,不累加
//删除key
keys, err := config.Redis.Keys("test*").Result()
if err != nil {
l.Errorf("redis get keys err :%w", err.Error())
return
}
for _, key := range keys {
config.Redis.Del(key)
}