redis.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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, DialWithTimeout or
  23. // NewConn functions. In the future, functions will be added for creating
  24. // pooled 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. The value
  36. // false is converted to "0" and the value true is converted to "1". The value
  37. // nil is converted to "". All other values are converted to a string using the
  38. // fmt.Fprint function. Command replies are represented using the following Go
  39. // types:
  40. //
  41. // Redis type Go type
  42. // error redis.Error
  43. // integer int64
  44. // status string
  45. // bulk []byte or nil if value not present.
  46. // multi-bulk []interface{} or nil if value not present.
  47. //
  48. // Applications can use type assertions or type switches to determine the type
  49. // of a reply.
  50. //
  51. // Pipelining
  52. //
  53. // Connections support pipelining using the Send and Receive methods.
  54. //
  55. // Send(commandName string, args ...interface{}) error
  56. // Receive() (reply interface{}, err error)
  57. //
  58. // Send writes the command to the connection's output buffer. Receive flushes
  59. // the output buffer to the server and reads a single reply. The following
  60. // example shows a simple pipeline:
  61. //
  62. // c.Send("SET", "foo", "bar")
  63. // c.Send("GET", "foo")
  64. // // reply from SET
  65. // if _, err := c.Receive(); err != nil {
  66. // return err
  67. // }
  68. // // reply from GET
  69. // v, err := c.Receive()
  70. // if err != nil {
  71. // return err
  72. // }
  73. //
  74. // The Do method is implemented with the Send and Receive methods. The method
  75. // starts by sending the command. Next, the method receives all unconsumed
  76. // replies including the reply for the command just sent by Do. If any of the
  77. // received replies is an error, then Do returns the error. If there are no
  78. // errors, then Do returns the last reply.
  79. //
  80. // The Send and Do methods can be used together to implement pipelined
  81. // transactions:
  82. //
  83. // c.Send("MULTI")
  84. // c.Send("INCR", "foo")
  85. // c.Send("INCR", "bar")
  86. // r, err := c.Do("EXEC")
  87. // fmt.Println(r) // prints [1, 1]
  88. //
  89. // Publish and Subscribe
  90. //
  91. // The connection Receive method is used to implement blocking subscribers:
  92. //
  93. // c.Send("SUBSCRIBE", "foo")
  94. // for {
  95. // reply, err := c.Receive()
  96. // if err != nil {
  97. // return err
  98. // }
  99. // // consume message
  100. // }
  101. //
  102. // Thread Safety
  103. //
  104. // The Send method cannot be called concurrently with other calls to Send. The
  105. // Receive method cannot be called concurrently with other calls to Receive.
  106. // Because the Do method invokes Send and Receive, the Do method cannot be
  107. // called concurrently with Send, Receive or Do. All other concurrent access is
  108. // allowed.
  109. package redis
  110. import ()
  111. // Error represents an error returned in a command reply.
  112. type Error string
  113. func (err Error) Error() string { return string(err) }
  114. // Conn represents a connection to a Redis server.
  115. type Conn interface {
  116. // Close closes the connection.
  117. Close() error
  118. // Err returns the permanent error for this connection.
  119. Err() error
  120. // Do sends a command to the server and returns the received reply.
  121. Do(commandName string, args ...interface{}) (reply interface{}, err error)
  122. // Send sends a command for the server without waiting for a reply.
  123. Send(commandName string, args ...interface{}) error
  124. // Receive receives a single reply from the server
  125. Receive() (reply interface{}, err error)
  126. }