首页 > 技术文章 > Golang学习系列第七天:操作Redis

dongguangming 2020-07-26 14:57 原文

0   redis安装请参考Redis备忘录

1.  golang操作redis

切换到golang工作目录,新建项目redis,然后建立连接redis的文件

[root@master src]# pwd
/dongguangming/goworkspace/src
[root@master src]# mkdir redis
[root@master src]# cd redis/
[root@master redis]# touch redis-conn.go

编辑redis-conn.go文件 ,即

[root@master redis]# vi  redis-conn.go 

键入以下代码

package main

import (
	"fmt"
	"github.com/go-redis/redis"
)

func main() {
	fmt.Println("golang连接redis")

	client := redis.NewClient(&redis.Options{
		Addr: "192.168.8.200:6379",
		Password: "123456",
		DB: 0,
	})

	pong, err := client.Ping().Result()
	fmt.Println(pong, err)

}

执行以上程序

[root@master redis]# go run redis-conn.go 
golang连接redis
PONG <nil>

没有报错,表示连接redis成功!!!

 

1.1  添加键值

通过golang设置redis键值前,请先通过redis shell查询下是否存在

[root@master ~]# redis-cli  -h 192.168.8.200 -p 6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.8.200:6379> get golang
(nil)

很好键golang并不存在,返回nil

然后通过golang添加键值

package main

import (
	"fmt"
	"github.com/go-redis/redis"
)

func main() {
	fmt.Println("golang连接redis")

	client := redis.NewClient(&redis.Options{
		Addr: "192.168.8.200:6379",
		Password: "123456",
		DB: 0,
	})

	pong, err := client.Ping().Result()
	fmt.Println(pong, err)
        
        //添加键值对
        err = client.Set("golang", "yes", 0).Err()
        if err != nil {
            fmt.Println(err)
        }
        fmt.Println("键golang设置成功")
}

执行以上代码

[root@master redis]# go run redis-conn.go 
golang连接redis
PONG <nil>
键golang设置成功

最后通过shell 查看是否键是否设置成功

[root@master ~]# redis-cli  -h 192.168.8.200 -p 6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.8.200:6379> get golang
(nil)
192.168.8.200:6379> get golang
"yes"

结果表明通golang设置的键golang的值为yes生效了!!!

 

1.2  获取键值

获取1.1设置的键值


import (
	"fmt"
	"github.com/go-redis/redis"
)

func main() {
	fmt.Println("golang连接redis")

	client := redis.NewClient(&redis.Options{
		Addr: "192.168.8.200:6379",
		Password: "123456",
		DB: 0,
	})

	pong, err := client.Ping().Result()
	fmt.Println(pong, err)
        
         
        //添加键值对
        err = client.Set("golang", "yes", 0).Err()
        if err != nil {
            fmt.Println(err)
        }
        fmt.Println("键golang设置成功")
       
         //通过键查询值
         val, err := client.Get("golang").Result()
        if err != nil {
          fmt.Println(err)
        }

       fmt.Println("键golang的值为: ",val)

}

执行程序,输出结果

[root@master redis]# go run redis-conn.go 
golang连接redis
PONG <nil>
键golang设置成功
键golang的值为:  yes

其他特性自行发挥!!!

 

后记: 已把我的电子书已上传至github:https://github.com/dongguangming/dgm-collection/tree/master/%E6%95%B0%E6%8D%AE%E5%BA%93/redis

dongguagming github

后续会慢慢上传其他资料。

 

参考:

    1. Golang’s Superior Cache Solution to Memcached and Redis https://www.mailgun.com/blog/golangs-superior-cache-solution-memcached-redis/

    2. A tour of the Redis stars  https://www.compose.com/articles/a-tour-of-the-redis-stars-2/

    3. Getting Started with Redis and Go - Tutorial https://tutorialedge.net/golang/go-redis-tutorial/

    4. How to Use Redis Go Client go-redis/redis with GoLang https://kb.objectrocket.com/redis/how-to-use-redis-go-client-go-redis-redis-with-golang-592

    5. Golang: Redis cluster client example https://golangbyexample.com/golang-redis-cluster-client-example/

    6. go-redisでのRedisデータ型の扱い方 https://qiita.com/momotaro98/items/ed496ba06908b278e103

    7.  Golang / Go Crash Course 08 | Using Redis as A Cache for our REST API https://m.youtube.com/watch?v=x5GGLrTuQCA

    8. 使用 Go 语言读写Redis协议 https://colobu.com/2019/04/16/Reading-and-Writing-Redis-Protocol-in-Go/https://www.redisgreen.net/blog/reading-and-writing-redis-protocol/

    9. Distributed Locks using Golang and Redis https://kylewbanks.com/blog/distributed-locks-using-golang-and-redis

    10. Go and Compose - Redis, RethinkDB, and RabbitMQ https://www.compose.com/articles/go-and-compose-redis-rethinkdb-and-rabbitmq/

推荐阅读