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)
}
執行以上程式
/n","classes":[]}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet">[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設定成功")
}
執行以上代碼
/n鍵golang設定成功/n","classes":[]}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet">[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)
}
執行程式,輸出結果
/n鍵golang設定成功/n鍵golang的值為: yes/n","classes":[]}" data-cke-widget-upcasted="1" data-cke-widget-keep-attr="0" data-widget="codeSnippet">[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

?
后續會慢慢上傳其他資料,
參考:
-
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/
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/1902.html
標籤:Go
上一篇:[Linux]阿里云萬網域名-騰訊云服務器nginx下配置免費的https
下一篇:Go的100天之旅-09Map
