Skip to content

Commit

Permalink
Example #19: Pipeline of commands
Browse files Browse the repository at this point in the history
  • Loading branch information
tisnik committed Jun 30, 2020
1 parent a14c445 commit 75e17ac
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
61 changes: 61 additions & 0 deletions lesson7/redis/19_pipeline/05_pipeline.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package main

import (
"fmt"

"github.com/go-redis/redis/v8"
)

// adresa určující službu Redisu, která se má použít
const redisAddress = "localhost:6379"

func main() {
// vytvoření nového klienta s předáním konfiguračních parametrů
client := redis.NewClient(&redis.Options{
Addr: redisAddress,
Password: "", // no password set
DB: 0, // use default DB
})

// neměli bychom zapomenout na ukončení práce s klientem
defer func() {
err := client.Close()
if err != nil {
panic(err)
}
}()

// získáme kontext
context := client.Context()

// pokus o klasický handshake typu PING-PONG
_, err := client.Ping(context).Result()
if err != nil {
panic(err)
}

// smazání hodnoty, pokud existovala
client.Del(context, "counter1")
client.Del(context, "counter2")
client.Del(context, "accumulator")

var counter1 *redis.IntCmd
var counter2 *redis.IntCmd
var accumulator *redis.FloatCmd

for i := 0; i < 5; i++ {
_, err = client.Pipelined(context, func(pipe redis.Pipeliner) error {
counter1 = pipe.Incr(context, "counter1")
counter2 = pipe.Decr(context, "counter2")
accumulator = pipe.IncrByFloat(context, "accumulator", 3.14)
return nil
})
if err != nil {
panic(err)
}

fmt.Printf("1st counter: %d\n", counter1.Val())
fmt.Printf("2nd counter: %d\n", counter2.Val())
fmt.Printf("accumulator: %f\n\n", accumulator.Val())
}
}
5 changes: 5 additions & 0 deletions lesson7/redis/19_pipeline/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module redis1

go 1.13

require github.com/go-redis/redis/v8 v8.0.0-beta.5

0 comments on commit 75e17ac

Please sign in to comment.