Golang学习系列第七天:操作Redis
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:github.com/dongguangming/dgm-colle...
后续会慢慢上传其他资料。
参考:
Golang’s Superior Cache Solution to Memcached and Redis www.mailgun.com/blog/golangs-super...
A tour of the Redis stars www.compose.com/articles/a-tour-of...
Getting Started with Redis and Go - Tutorial tutorialedge.net/golang/go-redis-t...
How to Use Redis Go Client go-redis/redis with GoLang kb.objectrocket.com/redis/how-to-u...
Golang: Redis cluster client example golangbyexample.com/golang-redis-c...
go-redisでのRedisデータ型の扱い方 qiita.com/momotaro98/items/ed496ba...
Golang / Go Crash Course 08 | Using Redis as A Cache for our REST API m.youtube.com/watch?v=x5GGLrTuQCA
使用 Go 语言读写Redis协议 colobu.com/2019/04/16/Reading-and-...,www.redisgreen.net/blog/reading-an...
Distributed Locks using Golang and Redis kylewbanks.com/blog/distributed-lo...
Go and Compose - Redis, RethinkDB, and RabbitMQ www.compose.com/articles/go-and-co...
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: