1 // Copyright 2012 Gary Burd 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"): you may 4 // not use this file except in compliance with the License. You may obtain 5 // a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations 13 // under the License. 14 15 package redis 16 17 import ( 18 "crypto/sha1" 19 "encoding/hex" 20 "io" 21 "strings" 22 ) 23 24 // Script encapsulates the source, hash and key count for a Lua script. See 25 // http://redis.io/commands/eval for information on scripts in Redis. 26 type Script struct { 27 keyCount int 28 src string 29 hash string 30 } 31 32 // NewScript returns a new script object. If keyCount is greater than or equal 33 // to zero, then the count is automatically inserted in the EVAL command 34 // argument list. If keyCount is less than zero, then the application supplies 35 // the count as the first value in the keysAndArgs argument to the Do, Send and 36 // SendHash methods. 37 func NewScript(keyCount int, src string) *Script { 38 h := sha1.New() 39 io.WriteString(h, src) 40 return &Script{keyCount, src, hex.EncodeToString(h.Sum(nil))} 41 } 42 43 func (s *Script) args(spec string, keysAndArgs []interface{}) []interface{} { 44 var args []interface{} 45 if s.keyCount < 0 { 46 args = make([]interface{}, 1+len(keysAndArgs)) 47 args[0] = spec 48 copy(args[1:], keysAndArgs) 49 } else { 50 args = make([]interface{}, 2+len(keysAndArgs)) 51 args[0] = spec 52 args[1] = s.keyCount 53 copy(args[2:], keysAndArgs) 54 } 55 return args 56 } 57 58 // Hash returns the script hash. 59 func (s *Script) Hash() string { 60 return s.hash 61 } 62 63 // Do evaluates the script. Under the covers, Do optimistically evaluates the 64 // script using the EVALSHA command. If the command fails because the script is 65 // not loaded, then Do evaluates the script using the EVAL command (thus 66 // causing the script to load). 67 func (s *Script) Do(c Conn, keysAndArgs ...interface{}) (interface{}, error) { 68 v, err := c.Do("EVALSHA", s.args(s.hash, keysAndArgs)...) 69 if e, ok := err.(Error); ok && strings.HasPrefix(string(e), "NOSCRIPT ") { 70 v, err = c.Do("EVAL", s.args(s.src, keysAndArgs)...) 71 } 72 return v, err 73 } 74 75 // SendHash evaluates the script without waiting for the reply. The script is 76 // evaluated with the EVALSHA command. The application must ensure that the 77 // script is loaded by a previous call to Send, Do or Load methods. 78 func (s *Script) SendHash(c Conn, keysAndArgs ...interface{}) error { 79 return c.Send("EVALSHA", s.args(s.hash, keysAndArgs)...) 80 } 81 82 // Send evaluates the script without waiting for the reply. 83 func (s *Script) Send(c Conn, keysAndArgs ...interface{}) error { 84 return c.Send("EVAL", s.args(s.src, keysAndArgs)...) 85 } 86 87 // Load loads the script without evaluating it. 88 func (s *Script) Load(c Conn) error { 89 _, err := c.Do("SCRIPT", "LOAD", s.src) 90 return err 91 } 92