From 7b70bfeadc24704e4b610aef5c17fe487e572bd3 Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Tue, 15 Oct 2013 13:49:21 +0300 Subject: [PATCH] Add Script example. --- v2/example_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/v2/example_test.go b/v2/example_test.go index dd6273b..aabea69 100644 --- a/v2/example_test.go +++ b/v2/example_test.go @@ -145,6 +145,34 @@ func ExamplePubSub() { // &{mychannel hello} } +func ExampleScript() { + client := redis.NewTCPClient(&redis.Options{ + Addr: ":6379", + }) + defer client.Close() + + setnx := redis.NewScript(` + if redis.call("get", KEYS[1]) == false then + redis.call("set", KEYS[1], ARGV[1]) + return 1 + end + return 0 + `) + + run1 := setnx.Run(client, []string{"keynx"}, []string{"foo"}) + fmt.Println(run1.Val().(int64), run1.Err()) + + run2 := setnx.Run(client, []string{"keynx"}, []string{"bar"}) + fmt.Println(run2.Val().(int64), run2.Err()) + + get := client.Get("keynx") + fmt.Println(get) + + // Output: 1 + // 0 + // GET keynx: foo +} + func Example_customCommand() { Get := func(client *redis.Client, key string) *redis.StringCmd { cmd := redis.NewStringCmd("GET", key)