redis.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. //
  19. // Connections
  20. //
  21. // The Conn interface is the primary interface for working with Redis.
  22. // Applications create connections by calling the Dial or DialWithTimeout
  23. // functions. In the future, functions will be added for creating pooled
  24. // connections and sharded connections.
  25. //
  26. // The application must call the connection Close method when the application
  27. // is done with the connection.
  28. //
  29. // Executing Commands
  30. //
  31. // The Conn interface has a generic method for executing Redis commands:
  32. //
  33. // Do(commandName string, args ...interface{}) (reply interface{}, err error)
  34. //
  35. // Arguments of type string and []byte are sent to the server as is. All other
  36. // types are formatted using the fmt.Fprint function. Command replies are
  37. // represented as Go types as follows:
  38. //
  39. // Redis type Go type
  40. // error redis.Error
  41. // integer int64
  42. // status string
  43. // bulk []byte or nil if value not present.
  44. // multi-bulk []interface{} or nil if value not present.
  45. //
  46. // Applications can use type assertions or type switches to determine
  47. // the actual type of a reply.
  48. //
  49. // Pipelining
  50. //
  51. // Connections support pipelining using the Send and Receive methods.
  52. //
  53. // Send(commandName string, args ...interface{}) error
  54. // Receive() (reply interface{}, err error)
  55. //
  56. // Send writes the command to the connection's output buffer. Receive flushes
  57. // the output buffer to the server and reads a single reply. The following
  58. // example shows a simple pipeline:
  59. //
  60. // c.Send("SET", "foo", "bar")
  61. // c.Send("GET", "foo")
  62. // // reply from SET
  63. // if _, err := c.Receive(); err != nil {
  64. // return err
  65. // }
  66. // // reply from GET
  67. // v, err := c.Receive()
  68. // if err != nil {
  69. // return err
  70. // }
  71. //
  72. // The Do method is implemented with the Send and Receive methods. The method
  73. // starts by sending the command. Next, the method receives all unconsumed
  74. // replies including the reply for the command just sent by Do. If any of the
  75. // received replies is an error, then Do returns the error. If there are no
  76. // errors, then Do returns the last reply.
  77. //
  78. // The Send and Do methods can be used together to implement pipelined
  79. // transactions:
  80. //
  81. // c.Send("MULTI")
  82. // c.Send("INCR", "foo")
  83. // c.Send("INCR", "bar")
  84. // r, err := c.Do("EXEC")
  85. // fmt.Println(r) // prints [1, 1]
  86. //
  87. // Publish and Subscribe
  88. //
  89. // The connection Receive method is used to implement blocking subscribers:
  90. //
  91. // c.Do("SUBSCRIBE", "foo")
  92. // for {
  93. // reply, err := c.Receive()
  94. // if err != nil {
  95. // return err
  96. // }
  97. // // consume message
  98. // }
  99. package redis
  100. // Error represets an error returned in a command reply.
  101. type Error string
  102. func (err Error) Error() string { return string(err) }
  103. // Conn represents a connection to a Redis server.
  104. type Conn interface {
  105. // Close closes the connection.
  106. Close() error
  107. // Err returns the permanent error for this connection.
  108. Err() error
  109. // Do sends a command to the server and returns the received reply.
  110. Do(commandName string, args ...interface{}) (reply interface{}, err error)
  111. // Send sends a command for the server without waiting for a reply.
  112. Send(commandName string, args ...interface{}) error
  113. // Receive receives a single reply from the server
  114. Receive() (reply interface{}, err error)
  115. }