Golang操作Redis

Posted 知其黑、受其白

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Golang操作Redis相关的知识,希望对你有一定的参考价值。

阅读目录

redis 使用

使用第三方开源的 redis库:
github.com/garyburd/redigo/redis

命令行输入 :

PS E:\\go_test>  go get github.com/garyburd/redigo/redis
go: downloading github.com/garyburd/redigo v1.6.4
go: module github.com/garyburd/redigo is deprecated: Use github.com/gomodule/redigo instead.
go: added github.com/garyburd/redigo v1.6.4
PS E:\\go_test>

链接 Redis

package main

import (
	"fmt"

	"github.com/garyburd/redigo/redis"
)

func main() 
	c, err := redis.Dial("tcp", "localhost:6379")
	if err != nil 
		fmt.Println("conn redis failed,", err)
		return
	

	fmt.Println("redis conn success")

	defer c.Close()

PS E:\\go_test> go run .\\main.go
redis conn success
PS E:\\go_test>

String 类型 Set、Get 操作

package main

import (
	"fmt"

	"github.com/garyburd/redigo/redis"
)

func main() 
	// redis.DialDatabase(1),//dialOption参数可以配置选择数据库、连接密码、心跳检测等等
	c, err := redis.Dial("tcp", "localhost:6379", redis.DialDatabase(1), redis.DialPassword("123456"))
	if err != nil 
		fmt.Println("conn redis failed,", err)
		return
	

	defer c.Close()
	_, err = c.Do("Set", "abc", 100)
	if err != nil 
		fmt.Println(err)
		return
	

	r, err := redis.Int(c.Do("Get", "abc"))
	if err != nil 
		fmt.Println("get abc failed,", err)
		return
	

	fmt.Println(r)

运行结果:

PS E:\\go_test> go run .\\main.go
100
PS E:\\go_test>

String 批量操作

package main

import (
	"fmt"

	"github.com/garyburd/redigo/redis"
)

func main() 
	c, err := redis.Dial("tcp", "localhost:6379", redis.DialDatabase(1), redis.DialPassword("123456"))
	if err != nil 
		fmt.Println("conn redis failed,", err)
		return
	

	defer c.Close()
	_, err = c.Do("MSet", "abc", 100, "efg", 300)
	if err != nil 
		fmt.Println(err)
		return
	

	r, err := redis.Ints(c.Do("MGet", "abc", "efg"))
	if err != nil 
		fmt.Println("get abc failed,", err)
		return
	

	for _, v := range r 
		fmt.Println(v)
	

PS E:\\go_test> go run .\\main.go
100
300
PS E:\\go_test>

设置过期时间

package main

import (
	"fmt"

	"github.com/garyburd/redigo/redis"
)

func main() 
	c, err := redis.Dial("tcp", "localhost:6379", redis.DialDatabase(1), redis.DialPassword("123456"))
	if err != nil 
		fmt.Println("conn redis failed,", err)
		return
	

	defer c.Close()
	_, err = c.Do("expire", "abc", 10)
	if err != nil 
		fmt.Println(err)
		return
	


List 队列操作

package main

import (
	"fmt"

	"github.com/garyburd/redigo/redis"
)

func main() 
	c, err := redis.Dial("tcp", "localhost:6379", redis.DialDatabase(1), redis.DialPassword("123456"))
	if err != nil 
		fmt.Println("conn redis failed,", err)
		return
	

	defer c.Close()
	_, err = c.Do("lpush", "book_list", "abc", "ceg", 300)
	if err != nil 
		fmt.Println(err)
		return
	

	r, err := redis.String(c.Do("lpop", "book_list"))
	if err != nil 
		fmt.Println("get abc failed,", err)
		return
	

	fmt.Println(r)

运行结果:

PS E:\\go_test> go run .\\main.go
300
PS E:\\go_test>

Hash 表

package main

import (
	"fmt"

	"github.com/garyburd/redigo/redis"
)

func main() 
	c, err := redis.Dial("tcp", "localhost:6379", redis.DialDatabase(1), redis.DialPassword("123456"))
	if err != nil 
		fmt.Println("conn redis failed,", err)
		return
	

	defer c.Close()
	_, err = c.Do("HSet", "books", "abc", 100)
	if err != nil 
		fmt.Println(err)
		return
	

	r, err := redis.Int(c.Do("HGet", "books", "abc"))
	if err != nil 
		fmt.Println("get abc failed,", err)
		return
	

	fmt.Println(r)

PS E:\\go_test> go run .\\main.go
100
PS E:\\go_test>

Redis 连接池

package main

import (
	"fmt"

	"github.com/garyburd/redigo/redis"
)

var pool *redis.Pool //创建redis连接池

func init() 
	pool = &redis.Pool //实例化一个连接池
		MaxIdle: 16, //最初的连接数量
		// MaxActive:1000000,    //最大连接数量
		MaxActive:   0,   //连接池最大连接数量,不确定可以用0(0表示自动定义),按需分配
		IdleTimeout: 300, //连接关闭时间 300秒 (300秒不使用自动关闭)
		Dial: func() (redis.Conn, error)  //要连接的redis数据库
			return redis.Dial("tcp", "localhost:6379", redis.DialDatabase(1), redis.DialPassword("123456"))
		,
	


func main() 
	c := pool.Get() //从连接池,取一个链接
	defer c.Close() //函数运行结束 ,把连接放回连接池

	_, err := c.Do("Set", "abc", 200)
	if err != nil 
		fmt.Println(err)
		return
	

	r, err := redis.Int(c.Do("Get", "abc"))
	if err != nil 
		fmt.Println("get abc faild :", err)
		return
	
	fmt.Println(r)
	pool.Close() //关闭连接池

PS E:\\go_test> go run .\\main.go
200
PS E:\\go_test>

以上是关于Golang操作Redis的主要内容,如果未能解决你的问题,请参考以下文章

Golang Redis操作

编程实践Golang 操作 Redis 代码实战

编程实践Golang 操作 Redis 代码实战

Golang操作数据库Redis

redis基本操作

golang redis事务 --- 2022-04-03