forked from mirror/redis
Merge pull request #125 from go-redis/fix/better-script-example
Better Script example.
This commit is contained in:
commit
e1116afab6
|
@ -7,6 +7,7 @@ Supports:
|
||||||
- [Pub/Sub](http://godoc.org/gopkg.in/redis.v3#PubSub).
|
- [Pub/Sub](http://godoc.org/gopkg.in/redis.v3#PubSub).
|
||||||
- [Transactions](http://godoc.org/gopkg.in/redis.v3#Multi).
|
- [Transactions](http://godoc.org/gopkg.in/redis.v3#Multi).
|
||||||
- [Pipelining](http://godoc.org/gopkg.in/redis.v3#Client.Pipeline).
|
- [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).
|
- [Timeouts](http://godoc.org/gopkg.in/redis.v3#Options).
|
||||||
- [Redis Sentinel](http://godoc.org/gopkg.in/redis.v3#NewFailoverClient).
|
- [Redis Sentinel](http://godoc.org/gopkg.in/redis.v3#NewFailoverClient).
|
||||||
- [Redis Cluster](http://godoc.org/gopkg.in/redis.v3#NewClusterClient).
|
- [Redis Cluster](http://godoc.org/gopkg.in/redis.v3#NewClusterClient).
|
||||||
|
|
|
@ -204,26 +204,26 @@ func ExamplePubSub() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleScript() {
|
func ExampleScript() {
|
||||||
setnx := redis.NewScript(`
|
IncrByXX := redis.NewScript(`
|
||||||
if redis.call("get", KEYS[1]) == false then
|
if redis.call("GET", KEYS[1]) ~= false then
|
||||||
redis.call("set", KEYS[1], ARGV[1])
|
return redis.call("INCRBY", KEYS[1], ARGV[1])
|
||||||
return 1
|
|
||||||
end
|
end
|
||||||
return 0
|
return false
|
||||||
`)
|
`)
|
||||||
|
|
||||||
v1, err := setnx.Run(client, []string{"keynx"}, []string{"foo"}).Result()
|
n, err := IncrByXX.Run(client, []string{"xx_counter"}, []string{"2"}).Result()
|
||||||
fmt.Println(v1.(int64), err)
|
fmt.Println(n, err)
|
||||||
|
|
||||||
v2, err := setnx.Run(client, []string{"keynx"}, []string{"bar"}).Result()
|
err = client.Set("xx_counter", "40", 0).Err()
|
||||||
fmt.Println(v2.(int64), err)
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
get := client.Get("keynx")
|
n, err = IncrByXX.Run(client, []string{"xx_counter"}, []string{"2"}).Result()
|
||||||
fmt.Println(get)
|
fmt.Println(n, err)
|
||||||
|
|
||||||
// Output: 1 <nil>
|
// Output: <nil> redis: nil
|
||||||
// 0 <nil>
|
// 42 <nil>
|
||||||
// GET keynx: foo
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Example_customCommand() {
|
func Example_customCommand() {
|
||||||
|
|
Loading…
Reference in New Issue