[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
没有报错,表示连接redis成功!!!
[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
键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.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
键golang设置成功
键golang的值为: yes
其他特性自行发挥!!!
后记: 已把我的电子书已上传至github:https://github.com/dongguangming/dgm-collection/tree/master/%E6%95%B0%E6%8D%AE%E5%BA%93/redis
后续会慢慢上传其他资料。
参考:
Golang’s Superior Cache Solution to Memcached and Redis https://www.mailgun.com/blog/golangs-superior-cache-solution-memcached-redis/
A tour of the Redis stars https://www.compose.com/articles/a-tour-of-the-redis-stars-2/
Getting Started with Redis and Go - Tutorial https://tutorialedge.net/golang/go-redis-tutorial/
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
Golang: Redis cluster client example https://golangbyexample.com/golang-redis-cluster-client-example/
go-redisでのRedisデータ型の扱い方 https://qiita.com/momotaro98/items/ed496ba06908b278e103
Golang / Go Crash Course 08 | Using Redis as A Cache for our REST API https://m.youtube.com/watch?v=x5GGLrTuQCA
使用 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/
Distributed Locks using Golang and Redis https://kylewbanks.com/blog/distributed-locks-using-golang-and-redis
Go and Compose - Redis, RethinkDB, and RabbitMQ https://www.compose.com/articles/go-and-compose-redis-rethinkdb-and-rabbitmq/