script.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. If keyCount is greater than or equal
  29. // to zero, then the count is automatically inserted in the EVAL command
  30. // argument list. If keyCount is less than zero, then the application supplies
  31. // the count as the first value in the keysAndArgs argument to the Do, Send and
  32. // SendHash methods.
  33. func NewScript(keyCount int, src string) *Script {
  34. h := sha1.New()
  35. io.WriteString(h, src)
  36. return &Script{keyCount, src, hex.EncodeToString(h.Sum(nil))}
  37. }
  38. func (s *Script) args(spec string, keysAndArgs []interface{}) []interface{} {
  39. var args []interface{}
  40. if s.keyCount < 0 {
  41. args = make([]interface{}, 1+len(keysAndArgs))
  42. args[0] = spec
  43. copy(args[1:], keysAndArgs)
  44. } else {
  45. args = make([]interface{}, 2+len(keysAndArgs))
  46. args[0] = spec
  47. args[1] = s.keyCount
  48. copy(args[2:], keysAndArgs)
  49. }
  50. return args
  51. }
  52. // Do evaluates the script. Under the covers, Do optimistically evaluates the
  53. // script using the EVALSHA command. If the command fails because the script is
  54. // not loaded, then Do evaluates the script using the EVAL command (thus
  55. // causing the script to load).
  56. func (s *Script) Do(c Conn, keysAndArgs ...interface{}) (interface{}, error) {
  57. v, err := c.Do("EVALSHA", s.args(s.hash, keysAndArgs)...)
  58. if e, ok := err.(Error); ok && strings.HasPrefix(string(e), "NOSCRIPT ") {
  59. v, err = c.Do("EVAL", s.args(s.src, keysAndArgs)...)
  60. }
  61. return v, err
  62. }
  63. // SendHash evaluates the script without waiting for the reply. The script is
  64. // evaluated with the EVALSHA command. The application must ensure that the
  65. // script is loaded by a previous call to Send, Do or Load methods.
  66. func (s *Script) SendHash(c Conn, keysAndArgs ...interface{}) error {
  67. return c.Send("EVALSHA", s.args(s.hash, keysAndArgs)...)
  68. }
  69. // Send evaluates the script without waiting for the reply.
  70. func (s *Script) Send(c Conn, keysAndArgs ...interface{}) error {
  71. return c.Send("EVAL", s.args(s.src, keysAndArgs)...)
  72. }
  73. // Load loads the script without evaluating it.
  74. func (s *Script) Load(c Conn) error {
  75. _, err := c.Do("SCRIPT", "LOAD", s.src)
  76. return err
  77. }