How redis go operates lua script
Redis go how to operate lua script, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
Lua script filename: test.lua
Local key=KEYS [1] local val=ARGV [1] return redis.call ('SET', key, val)
Go code
Package mainimport ("fmt"io/ioutil"time"github.com/gomodule/redigo/redis") func CliPool () * redis.Pool {return & redis.Pool {MaxIdle: 10, MaxActive: 100, IdleTimeout: time.Duration (20) * time.Second, Dial: func () (redis.Conn Error) {c, err: = redis.Dial ("tcp", "127.0.0.1 redis.Dial 6379") if err! = nil {return nil, err} return c, nil} }} func main () {var data [] byte var err error if data, err = ioutil.ReadFile ("test.lua") Err! = nil {fmt.Printf ("read failed% v", err.Error ()) return} script: = redis.NewScript (1, string (data)) pool: = CliPool () defer pool.Close () cli: = pool.Get () script.Load (cli) arg: = [] string {"key" "value"} args: = redis.Args {} .AddFlat (arg) var reply interface {} if reply, err = script.Do (cli, args...) Err! = nil {fmt.Printf ("redis exec failed% v", err.Error ()) return} fmt.Printf ("return% v", reply)}
Script.Do parameters must be correct, otherwise it is easy to make mistakes, either fill in arg [0], arg [1] or use redis.Args {} to fill in the parameters, other ways are easy to make mistakes. Make sure that all parameters are in one slice after being operated by (s * Script) args (spec string, keysAndArgs [] interface {}) method.
This is the answer to the question about how redis go operates lua script. I hope the above content can be of some help to you. If you still have a lot of doubts to solve, you can follow the industry information channel to learn more about it.