1. Getting started with Redis

  • Installing Redis

    sudo apt-get update
    sudo apt-get install redis-server
    
  • Starting Redis

    # Edit /etc/redis/redis.conf to modify default host and port
    sudo redis-server /etc/redis/redis.conf
    
  • Check if Redis is working

    ➜  ~ redis-cli
    127.0.0.1:6379> ping
    PONG
    

2. Installing Redis Client for Golang

  • Installing driver

    # refer to https://yongchaohe.github.io/notes/2019/10/18/gRPC-installation-in-China.html
    cd $GOPATH/src
    git clone https://github.com/go-redis/redis.git github.com/go-redis/redis
    git clone https://github.com/open-telemetry/opentelemetry-go.git go.opentelemetry.io/otel 
    

3. Demo

  • Golang Code

    package main
      
    import (
        "fmt"
        "time"
        "context"
        "encoding/json"
        "github.com/go-redis/redis"
    )
      
    const (
        Ch_stat  = "f_stat"
        Ch_alloc = "f_alloc"
    )
      
    type FStat struct {
        SDC   string `json:"src_dc"`
        DDC   string `json:"dst_dc"`
        Usage int    `json:"Usage"`
    }
      
    type FAlloc struct {    
        SDC   string `json:"src_dc"`
        DDC   string `json:"dst_dc"`
        Alloc int    `json:"allocation"`
    }
      
    func mainDaemon() {
        for {
            time.Sleep(time.Second)
        }
    }
      
    func ReadAlloc(message string) []FAlloc {
        var fa []FAlloc
        json.Unmarshal([]byte(message), &fa)
        return fa
    }
      
    func WriteStat(fu []FStat) string {
        var message string = ""
        for _, item := range(fu) {
            jContent, err := json.Marshal(&item)
            if err != nil {
                continue
            } 
            if message == "" {
                message = "[" + string(jContent)
            } else {
                message = message + "," + string(jContent)
            }
        }
        return message + "]"
    }
      
    func main() {
        client := redis.NewClient(&redis.Options{
            Addr     : "10.19.0.80:6379",
            Password : "",
            DB       : 0,
        })
      
        pong, err := client.Ping(context.Background()).Result()
        fmt.Println(pong, err)
      
        var fu1 FStat = FStat {
            SDC   : "A",
            DDC   : "B",
            Usage : 1000,
        }
        var fu []FStat
        fu = append(fu, fu1)
      
        jContent := WriteStat(fu)
        fmt.Println(jContent)
        // https://godoc.org/github.com/go-redis/redis#ClusterClient.Publish
        // https://godoc.org/github.com/go-redis/redis#IntCmd.Result
        subscriber, err := client.Publish(context.Background(), Ch_stat, jContent).Result()
        if err != nil {
            fmt.Println(err)
        } else {
            fmt.Println(subscriber)
        }
      
        // subscribe from channel flow_allocation
        // https://godoc.org/github.com/go-redis/redis#Client.Subscribe
        go func() {
            sub := client.Subscribe(context.Background(), Ch_alloc)
            _, err  = sub.Receive(context.Background())
            if err != nil {
                fmt.Println(err)
            } else {
                ch := sub.Channel()
                for msg := range(ch) {
                fmt.Println(msg.Channel, msg.Payload)
                // jContent = `[{"src_dc":"A","dst_dc":"B","allocation":100},{"src_dc":"C","dst_dc":"D","allocation":1000}]`
                jContent := msg.Payload
                fa := ReadAlloc(jContent)
                fmt.Println(fa)
                }
            }
        } ()
        mainDaemon()
    }
    
  • Publish

    10.19.0.80:6379> subscribe f_stat
    Reading messages... (press Ctrl-C to quit)
    1) "subscribe"
    2) "f_stat"
    3) (integer) 1
    1) "message"
    2) "f_stat"
    3) "[{\"src_dc\":\"A\",\"dst_dc\":\"B\",\"Usage\":1000}]"
    
  • Subscribe

    10.19.0.80:6379> publish f_alloc '[{"src_dc":"A","dst_dc":"B","allocation":1000}]'
    (integer) 2
    

REF

  1. redis: github.com/go-redis/redis
  2. Redis client for Golang