1. 程式人生 > 實用技巧 >Golang學習系列第七天:操作Redis

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: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 Redishttps://www.mailgun.com/blog/golangs-superior-cache-solution-memcached-redis/

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

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

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

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

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

    7. Golang/GoCrashCourse08|UsingRedisasACacheforourRESTAPIhttps://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 Redishttps://kylewbanks.com/blog/distributed-locks-using-golang-and-redis

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