redis.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 is a client for the Redis database.
  15. //
  16. // Package redis only supports the binary-safe Redis protocol, so you can use
  17. // it with any Redis version >= 1.2.0.
  18. package redis
  19. // Error represets an error returned in a command reply.
  20. type Error string
  21. func (err Error) Error() string { return string(err) }
  22. // Conn represents a connection to a Redis server.
  23. //
  24. // The Do method executes a Redis command. Command arguments of type string and
  25. // []byte are sent to the server as is. All other argument types are formatted
  26. // using the fmt.Fprint() function. Command replies are represented as Go types
  27. // as follows:
  28. //
  29. // Redis type Go type
  30. // error redis.Error
  31. // integer int64
  32. // status string
  33. // bulk []byte or nil if value not present.
  34. // multi-bulk []interface{} or nil if value not present.
  35. //
  36. // Connections support pipelining using the Send and Receive methods. Send
  37. // formats and buffers outgoing commands. Receive flushes the outgoing command
  38. // buffer and reads an incoming reply. The following example shows a simple
  39. // pipeline:
  40. //
  41. // c.Send("SET", "foo", "bar")
  42. // c.Send("GET", "foo")
  43. // // reply from SET
  44. // if _, err := c.Receive(); err != nil {
  45. // return err
  46. // }
  47. // // reply from GET
  48. // v, err := c.Receive()
  49. // if err != nil {
  50. // return err
  51. // }
  52. //
  53. // This API can be used to implement a blocking subscriber:
  54. //
  55. // c.Do("SUBSCRIBE", "foo")
  56. // for {
  57. // reply, err := c.Receive()
  58. // if err != nil {
  59. // // handle error
  60. // }
  61. // // consume message
  62. // }
  63. type Conn interface {
  64. // Close closes the connection.
  65. Close() error
  66. // Err returns the permanent error for this connection.
  67. Err() error
  68. // Do sends a command to the server and returns the received reply.
  69. Do(cmd string, args ...interface{}) (reply interface{}, err error)
  70. // Send sends a command for the server without waiting for a reply.
  71. Send(cmd string, args ...interface{}) error
  72. // Receive receives a single reply from the server
  73. Receive() (reply interface{}, err error)
  74. }