项目地址:
https://github.com/go-redis/redis
使用:
package main import ( "fmt" "github.com/go-redis/redis" ) func main() { //// 连接到集群 //rdb := redis.NewClusterClient(&redis.ClusterOptions{ // Addrs: []string{ // "10.0.0.11:6301", // "10.0.0.11:6302", // "10.0.0.11:6303", // "10.0.0.11:6304", // "10.0.0.11:6305", // "10.0.0.11:6306", // }, // Password: "", //}) // 连接单台服务器 rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", DB: 0, }) // *字符串(String) stringKey := "string:key" if err := rdb.Set(stringKey, "string-value", 0).Err(); err == nil { if res := rdb.Get(stringKey); res.Err() == nil { fmt.Println(res.Val()) } } // *哈希表(Hash) hashKey := "hash:key" if err := rdb.HMSet(hashKey, map[string]interface{}{ "key1": "value1", "key2": "value2", "key3": "value3", }).Err(); err == nil { if res := rdb.HGetAll(hashKey); res.Err() == nil { fmt.Println(res.Val()) } } // *列表(List) listKey := "list:key" if err := rdb.LPush(listKey, []string{ "list-value1", "list-value2", "list-value3", }).Err(); err == nil { for { if res := rdb.RPop(listKey); res.Err() == nil { fmt.Println(res.Val()) } else { break } } } // *集合(Set) setKey := "set:key" if err := rdb.SAdd(setKey, []string{ "set-value1", "set-value1", "set-value2", "set-value3", }).Err(); err == nil { if res := rdb.SMembers(setKey); res.Err() == nil { fmt.Println(res.Val()) } } // *有序集合(Sorted Set) sSetKey := "sSet:key" if err := rdb.ZAdd( sSetKey, redis.Z{Score: 0, Member: "sSet-value5"}, redis.Z{Score: 1, Member: "sSet-value2"}, redis.Z{Score: 3, Member: "sSet-value1"}, // 得分最高 redis.Z{Score: 2, Member: "sSet-value3"}, ).Err(); err == nil { if res := rdb.ZRevRange(sSetKey, 0, 2); res.Err() == nil { fmt.Println(res.Val()) } } // *位图(BitMap) bitMapKey := "bitMap:key:user:1" if err := rdb.SetBit(bitMapKey, int64(20230101), 1).Err(); err == nil { if res := rdb.GetBit(bitMapKey, int64(20230101)); res.Err() == nil { fmt.Println(res.Val()) } if res := rdb.BitCount(bitMapKey, &redis.BitCount{ Start: 0 / 8, End: 20300000 / 8, }); res.Err() == nil { fmt.Println(res.Val()) } } // *事务 + Watch watchKey1 := "watch:key:1" watchKey2 := "watch:key:2" if err := rdb.Watch(func(tx *redis.Tx) error { num1, err := tx.Get(watchKey1).Int64() if err != nil && err != redis.Nil { return err } num2, err := tx.Get(watchKey2).Int64() if err != nil && err != redis.Nil { return err } _, err = tx.TxPipelined(func(pipeliner redis.Pipeliner) error { if err := pipeliner.Set(watchKey1, num1+1, 0).Err(); err != nil { pipeliner.Discard() // 回滚事务 return err } if err := pipeliner.Set(watchKey2, num2+1, 0).Err(); err != nil { pipeliner.Discard() // 回滚事务 return err } cmd, _ := pipeliner.Exec() // 提交事务 fmt.Println("tx-cmd:", cmd) return nil }) return err }, watchKey1, watchKey2); err != nil { fmt.Println(err) } // *管道(Pipeline) pipe := rdb.Pipeline() // 批量设置 for i := 0; i < 100; i++ { pipeKey := fmt.Sprintf("pipeline:key:%d", i) pipe.Set(pipeKey, i, 0) } cmd, _ := pipe.Exec() fmt.Println("pipeline-set-cmd:", cmd) //// 批量获取 //pipeRes := make([]*redis.StringCmd, 0) //for i := 0; i < 100; i++ { // pipeKey := fmt.Sprintf("pipeline:key:%d", i) // pipeRes = append(pipeRes, pipe.Get(pipeKey)) //} //cmd, _ := pipe.Exec() //fmt.Println("pipeline-get-cmd:",cmd) //for _, item := range pipeRes { // fmt.Println(item.Val()) //} // *使用 lua 脚本 script := redis.NewScript(` local key = KEYS[1] local num = ARGV[1] local value = redis.call("GET", key) if not value then value = 0 end value = value + num redis.call("SET", key, value) return value `) keys := []string{"lua:key"} values := []interface{}{1} if num, err := script.Run(rdb, keys, values...).Int(); err != nil { fmt.Println(err) } else { fmt.Println(num) } }
文档更新时间: 2024-04-20 10:57 作者:lee