package main
import (
"context"
"errors"
"fmt"
"github.com/go-redis/redis/v8"
)
var ctx = context.Background()
func main() {
ExampleClient()
}
func ExampleClient() {
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
})
err := rdb.Set(ctx, "key", "value", 0).Err()
if err != nil {
panic(err)
}
val, err := rdb.Get(ctx, "key").Result()
if err != nil {
panic(err)
}
fmt.Println("key", val)
script := `return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}`
res, err := rdb.Eval(ctx, script, []string{"key1", "key2"}, "ag1", "arg2").Result()
fmt.Printf("Eval T=%T v=%v err=%v\n", res, res, err)
script = `if redis.call('get',KEYS[1]) then return 0;else return 1 end`
res, err = rdb.Eval(ctx, script, []string{"key2"}).Result()
fmt.Printf("Eval2 T=%T v=%v err=%v\n", res, res, err)
script = `local v = redis.call('get',KEYS[1]);if v then redis.call('del',KEYS[1]);return 1;end;return 0;`
res, err = rdb.Eval(ctx, script, []string{"key"}).Int64()
fmt.Printf("Eval3 T=%T v=%v err=%v\n", res, res, err)
val, err = rdb.Get(ctx, "key").Result()
if err != nil {
if errors.Is(err, redis.Nil) {
fmt.Println("nil:", err.Error())
}
}
fmt.Println("key", val)
}
|