• golang 操作 redis(github.comgo-redisredis 包使用)


    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
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5

    然后执行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
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    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   当键存在时 返回1 
    	// 检查给定 key 是否存在。
    	intcmd = writePool.Exists("set1")
    	fmt.Println(intcmd.Result())  // 1 
    	// 为给定 key 设置过期时间,以秒计。
    	boolcmd := writePool.Expire("set1", time.Hour)
    	fmt.Println(boolcmd.Result()) // true 
    	// 查找所有符合给定模式( pattern)的 key
    	stringSliceCmd := writePool.Keys("s*")
    	fmt.Println(stringSliceCmd.Result()) // [set2 set1 set3] 
    	// 以秒为单位,返回给定 key 的剩余生存时间(TTL, time to live)。
    	durationCmd := writePool.TTL("set1")
    	fmt.Println(durationCmd.Result()) // 1h0m0s 
    	// 返回 key 所储存的值的类型。
    	statusCmd := writePool.Type("set1")
    	fmt.Println(statusCmd.Result()) // string 
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    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 
    	stringCmd := writePool.Get("string1")
    	fmt.Println(stringCmd.Result()) // ss 
    	stringCmd = writePool.GetRange("string1",0,0)
    	fmt.Println(stringCmd.Result()) // s 
    	stringCmd = writePool.GetSet("string1","ss2")
    	fmt.Println(stringCmd.Result()) // ss 
    	slicecmd := writePool.MGet("string1","set1","set2","setn")
    	fmt.Println(slicecmd.Result())  // [ss2 ss ss ] 
    	boolcmd := writePool.SetNX("string1","ss3",0)
    	fmt.Println(boolcmd.Result())  // false 
    	intcmd := writePool.StrLen("string1")
    	fmt.Println(intcmd.Result())  // 3 
    	statusCmd = writePool.MSet("string2","ss2","string3","ss3","string4","ss4")
    	fmt.Println(statusCmd.Result()) // OK 
    	boolcmd = writePool.MSetNX("string2","ss2","string3","ss3","string4", "ss4")
    	fmt.Println(boolcmd.Result()) // false 
    	writePool.Set("int1",0,0)
    	intcmd = writePool.Incr("int1")
    	fmt.Println(intcmd.Result())  // 1 
    	intcmd = writePool.IncrBy("int1",10)
    	fmt.Println(intcmd.Result())  // 11 
    	intcmd = writePool.Append("string2","adsfasdf")
    	fmt.Println(intcmd.Result())  // 11 
    }
    
    • 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

    d、其他命令

    详细数据结构及方法,见菜鸟教程:https://www.runoob.com/redis/redis-hashes.html

  • 相关阅读:
    【操作系统】为什么需要内核
    Allegro如何添加泪滴操作指导
    ts-面向对象
    数人云操作系统 2.0 发布
    四、分类算法 - 随机森林
    Nexus【应用 01】上传jar包到私有Maven仓库的两种方法:手动 Upload 和 mvn deploy 命令(配置+操作流程)
    OneNote 教程,如何在 OneNote 中做笔记?
    【数据结构】P1 线性表+顺序表
    力扣第216 组合总和 ||| c++ 回溯 + 注释
    HTML5学习笔记(四)
  • 原文地址:https://blog.csdn.net/m0_67401606/article/details/126364179