forked from mirror/redis
Add script example to the README.
This commit is contained in:
parent
1ee83d1eff
commit
054c21e571
30
v2/README.md
30
v2/README.md
|
@ -161,6 +161,36 @@ fmt.Println(msg, err)
|
||||||
// &{mychannel hello} <nil>
|
// &{mychannel hello} <nil>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
To use Lua scripting:
|
||||||
|
|
||||||
|
```go
|
||||||
|
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 <nil>
|
||||||
|
// 0 <nil>
|
||||||
|
// GET keynx: foo
|
||||||
|
```
|
||||||
|
|
||||||
You can also write custom commands:
|
You can also write custom commands:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
|
|
@ -152,12 +152,12 @@ func ExampleScript() {
|
||||||
defer client.Close()
|
defer client.Close()
|
||||||
|
|
||||||
setnx := redis.NewScript(`
|
setnx := 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])
|
redis.call("set", KEYS[1], ARGV[1])
|
||||||
return 1
|
return 1
|
||||||
end
|
end
|
||||||
return 0
|
return 0
|
||||||
`)
|
`)
|
||||||
|
|
||||||
run1 := setnx.Run(client, []string{"keynx"}, []string{"foo"})
|
run1 := setnx.Run(client, []string{"keynx"}, []string{"foo"})
|
||||||
fmt.Println(run1.Val().(int64), run1.Err())
|
fmt.Println(run1.Val().(int64), run1.Err())
|
||||||
|
|
Loading…
Reference in New Issue