• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

golang 操作 redisgithub.comgo-redisredis 包使用

武飞扬头像
m0_67401606
帮助5

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 中加入:

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、创建连接池
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 所储存的值的类型

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)的末尾

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

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfiaeja
系列文章
更多 icon
同类精品
更多 icon
继续加载