script.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. package redis
  15. import (
  16. "crypto/sha1"
  17. "encoding/hex"
  18. "io"
  19. "strings"
  20. )
  21. // Script encapsulates the source, hash and key count for a Lua script. See
  22. // http://redis.io/commands/eval for information on scripts in Redis.
  23. type Script struct {
  24. keyCount int
  25. src string
  26. hash string
  27. }
  28. // NewScript returns a new script object initialized with the specified number
  29. // of keys and source code.
  30. func NewScript(keyCount int, src string) *Script {
  31. h := sha1.New()
  32. io.WriteString(h, src)
  33. return &Script{keyCount, src, hex.EncodeToString(h.Sum(nil))}
  34. }
  35. func (s *Script) args(spec string, keysAndArgs []interface{}) []interface{} {
  36. args := make([]interface{}, 2+len(keysAndArgs))
  37. args[0] = spec
  38. args[1] = s.keyCount
  39. copy(args[2:], keysAndArgs)
  40. return args
  41. }
  42. // Do evaluates the script and returns the result. Under the covers, Do
  43. // attempts to evaluate the script using the EVALSHA command. If the command
  44. // fails because the script is not loaded, then Do evaluates the script using
  45. // the EVAL command (thus causing the script to load).
  46. func (s *Script) Do(c Conn, keysAndArgs ...interface{}) (interface{}, error) {
  47. v, err := c.Do("EVALSHA", s.args(s.hash, keysAndArgs)...)
  48. if e, ok := err.(Error); ok && strings.HasPrefix(string(e), "NOSCRIPT ") {
  49. v, err = c.Do("EVAL", s.args(s.src, keysAndArgs)...)
  50. }
  51. return v, err
  52. }
  53. // SendHash evaluates the script without waiting for the result. The script is
  54. // evaluated with the EVALSHA command. The application must ensure that the
  55. // script is loaded by a previous call to Send, Do or Load methods.
  56. func (s *Script) SendHash(c Conn, keysAndArgs ...interface{}) error {
  57. return c.Send("EVALSHA", s.args(s.hash, keysAndArgs)...)
  58. }
  59. // Send evaluates the script without waiting for the result.
  60. func (s *Script) Send(c Conn, keysAndArgs ...interface{}) error {
  61. return c.Send("EVAL", s.args(s.src, keysAndArgs)...)
  62. }
  63. // Load loads the script without evaluating it.
  64. func (s *Script) Load(c Conn) error {
  65. _, err := c.Do("SCRIPT", "LOAD", s.src)
  66. return err
  67. }