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 15package redis 16 17import ( 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. 26type 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. 37func 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 43func (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// Do evaluates the script. Under the covers, Do optimistically evaluates the 59// script using the EVALSHA command. If the command fails because the script is 60// not loaded, then Do evaluates the script using the EVAL command (thus 61// causing the script to load). 62func (s *Script) Do(c Conn, keysAndArgs ...interface{}) (interface{}, error) { 63 v, err := c.Do("EVALSHA", s.args(s.hash, keysAndArgs)...) 64 if e, ok := err.(Error); ok && strings.HasPrefix(string(e), "NOSCRIPT ") { 65 v, err = c.Do("EVAL", s.args(s.src, keysAndArgs)...) 66 } 67 return v, err 68} 69 70// SendHash evaluates the script without waiting for the reply. The script is 71// evaluated with the EVALSHA command. The application must ensure that the 72// script is loaded by a previous call to Send, Do or Load methods. 73func (s *Script) SendHash(c Conn, keysAndArgs ...interface{}) error { 74 return c.Send("EVALSHA", s.args(s.hash, keysAndArgs)...) 75} 76 77// Send evaluates the script without waiting for the reply. 78func (s *Script) Send(c Conn, keysAndArgs ...interface{}) error { 79 return c.Send("EVAL", s.args(s.src, keysAndArgs)...) 80} 81 82// Load loads the script without evaluating it. 83func (s *Script) Load(c Conn) error { 84 _, err := c.Do("SCRIPT", "LOAD", s.src) 85 return err 86} 87