go-redis/redis 使用
go-redis 是go用来链接redis数据库的包。截止当前时间github上star 8.7k
环境:
go | go-redis |
---|---|
go1.13.5 | v6.15.7+incompatible |
安装:
使用go mod 进行安装
在go.mod 中加入:
1 2 3 4 5 | module github.com/luslin/tools go 1.13 require ( github.com/go-redis/redis v6.15.7+incompatible ) |
然后执行go mod download 就可以可。 我的GOPROXY设置为https://goproxy.cn,direct
使用:
示例:
a、创建连接池
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | func RedisClientPool(addr string, password string, db int) *redis.Client{ redis_opt := redis.Options{ Addr: addr, Password: password, DB: db, } // 创建连接池 redisdb := redis.NewClient(&redis_opt) // 判断是否能够链接到数据库 pong, err := redisdb.Ping().Result() if err != nil { fmt.Println(pong, err) } return redisdb } |
redis.Options 默认池大小为10×cpu核数
b、key命令
命令 | 描述 |
---|---|
DEL key | 用于在 key 存在时删除 key |
EXISTS key | 检查给定 key 是否存在 |
EXPIRE key seconds | 为给定 key 设置过期时间,以秒计 |
KEYS pattern | 查找所有符合给定模式( pattern)的 key |
TTL key | 以秒为单位,返回给定 key 的剩余生存时间 |
TYPE key | 返回 key 所储存的值的类型 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | func TestKey(t *testing.T) { writePool := RedisClientPool("***.**.***.**:6379","passwd@2020", 5) // 在 key 存在时删除 key。 intcmd := writePool.Del("set") fmt.Println(intcmd.Result()) // 0 <nil> 当键存在时 返回1 <nil> // 检查给定 key 是否存在。 intcmd = writePool.Exists("set1") fmt.Println(intcmd.Result()) // 1 <nil> // 为给定 key 设置过期时间,以秒计。 boolcmd := writePool.Expire("set1", time.Hour) fmt.Println(boolcmd.Result()) // true <nil> // 查找所有符合给定模式( pattern)的 key stringSliceCmd := writePool.Keys("s*") fmt.Println(stringSliceCmd.Result()) // [set2 set1 set3] <nil> // 以秒为单位,返回给定 key 的剩余生存时间(TTL, time to live)。 durationCmd := writePool.TTL("set1") fmt.Println(durationCmd.Result()) // 1h0m0s <nil> // 返回 key 所储存的值的类型。 statusCmd := writePool.Type("set1") fmt.Println(statusCmd.Result()) // string <nil> } |
c、字符串命令
命令 | 描述 |
---|---|
SET key value time.Duration | 设置指定 key 的值及过期时间, 0为永久 |
GET key | 获取指定 key 的值 |
GETRANGE key start end | 返回 key 中字符串值的子字符 |
GETSET key value | 给定 key 的值设为 value ,并返回 key 的旧值(old value) |
MGET key1 [key2…] | 获取所有(一个或多个)给定 key 的值 |
SETNX key value | 只有在 key 不存在时设置 key 的值 |
STRLEN key | 返回 key 所储存的字符串值的长度 |
MSET key value [key value …] | 同时设置一个或多个 key-value 对 |
MSETNX key value [key value …] | 同时设置一个或多个 key-value 对,当且仅当所有给定 key 都不存在。 |
INCRBY key increment | 将 key 所储存的值加上给定的增量值(increment) |
INCRBYFLOAT key increment | 将 key 所储存的值加上给定的浮点增量值(increment) |
DECR key | 将 key 中储存的数字值减一 |
DECRBY key decrement | key 所储存的值减去给定的减量值(decrement) |
APPEND key value | 如果 key 已经存在并且是一个字符串, APPEND 命令将指定的 value 追加到该 key 原来值(value)的末尾 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | func TestString(t *testing.T) { statusCmd := writePool.Set("string1","ss",0) fmt.Println(statusCmd.Result()) // OK <nil> stringCmd := writePool.Get("string1") fmt.Println(stringCmd.Result()) // ss <nil> stringCmd = writePool.GetRange("string1",0,0) fmt.Println(stringCmd.Result()) // s <nil> stringCmd = writePool.GetSet("string1","ss2") fmt.Println(stringCmd.Result()) // ss <nil> slicecmd := writePool.MGet("string1","set1","set2","setn") fmt.Println(slicecmd.Result()) // [ss2 ss ss <nil>] <nil> boolcmd := writePool.SetNX("string1","ss3",0) fmt.Println(boolcmd.Result()) // false <nil> intcmd := writePool.StrLen("string1") fmt.Println(intcmd.Result()) // 3 <nil> statusCmd = writePool.MSet("string2","ss2","string3","ss3","string4","ss4") fmt.Println(statusCmd.Result()) // OK <nil> boolcmd = writePool.MSetNX("string2","ss2","string3","ss3","string4", "ss4") fmt.Println(boolcmd.Result()) // false <nil> writePool.Set("int1",0,0) intcmd = writePool.Incr("int1") fmt.Println(intcmd.Result()) // 1 <nil> intcmd = writePool.IncrBy("int1",10) fmt.Println(intcmd.Result()) // 11 <nil floatcmd := writePool.IncrByFloat("int1",12.2) fmt.Println(floatcmd.Result()) // 23.2 <nil> intcmd = writePool.Append("string2","adsfasdf") fmt.Println(intcmd.Result()) // 11 <nil> } |
d、其他命令
详细数据结构及方法,见菜鸟教程:https://www.runoob.com/redis/redis-hashes.html