script.go 3.0 KB

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