首页 > 其他 > 详细

Golang学习系列第七天:操作Redis

时间:2020-07-26 19:26:49      阅读:62      评论:0      收藏:0      [点我收藏+]

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

技术分享图片技术分享图片?

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

 

参考:

    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/

Golang学习系列第七天:操作Redis

原文:https://www.cnblogs.com/dongguangming/p/13380433.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!