From c9fb737f06e6a9ec2f109b30c9f52368815a4c4e Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Tue, 16 Jun 2015 10:31:21 +0300 Subject: [PATCH] Better Script example. --- README.md | 1 + example_test.go | 32 ++++++++++++++++---------------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index ea3ffd5..d554582 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ Supports: - [Pub/Sub](http://godoc.org/gopkg.in/redis.v3#PubSub). - [Transactions](http://godoc.org/gopkg.in/redis.v3#Multi). - [Pipelining](http://godoc.org/gopkg.in/redis.v3#Client.Pipeline). +- [Scripting](http://godoc.org/gopkg.in/redis.v3#Script). - [Timeouts](http://godoc.org/gopkg.in/redis.v3#Options). - [Redis Sentinel](http://godoc.org/gopkg.in/redis.v3#NewFailoverClient). - [Redis Cluster](http://godoc.org/gopkg.in/redis.v3#NewClusterClient). diff --git a/example_test.go b/example_test.go index 0da5573..e948410 100644 --- a/example_test.go +++ b/example_test.go @@ -204,26 +204,26 @@ func ExamplePubSub() { } func ExampleScript() { - setnx := redis.NewScript(` - if redis.call("get", KEYS[1]) == false then - redis.call("set", KEYS[1], ARGV[1]) - return 1 - end - return 0 - `) + IncrByXX := redis.NewScript(` + if redis.call("GET", KEYS[1]) ~= false then + return redis.call("INCRBY", KEYS[1], ARGV[1]) + end + return false + `) - v1, err := setnx.Run(client, []string{"keynx"}, []string{"foo"}).Result() - fmt.Println(v1.(int64), err) + n, err := IncrByXX.Run(client, []string{"xx_counter"}, []string{"2"}).Result() + fmt.Println(n, err) - v2, err := setnx.Run(client, []string{"keynx"}, []string{"bar"}).Result() - fmt.Println(v2.(int64), err) + err = client.Set("xx_counter", "40", 0).Err() + if err != nil { + panic(err) + } - get := client.Get("keynx") - fmt.Println(get) + n, err = IncrByXX.Run(client, []string{"xx_counter"}, []string{"2"}).Result() + fmt.Println(n, err) - // Output: 1 - // 0 - // GET keynx: foo + // Output: redis: nil + // 42 } func Example_customCommand() {