You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

169 lines
6.1 KiB

  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. // The Redigo FAQ (https://github.com/garyburd/redigo/wiki/FAQ) contains more
  17. // documentation about this package.
  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. // sharded and other types of 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. // The Redis command reference (http://redis.io/commands) lists the available
  36. // commands. An example of using the Redis APPEND command is:
  37. //
  38. // n, err := conn.Do("APPEND", "key", "value")
  39. //
  40. // The Do method converts command arguments to binary strings for transmission
  41. // to the server as follows:
  42. //
  43. // Go Type Conversion
  44. // []byte Sent as is
  45. // string Sent as is
  46. // int, int64 strconv.FormatInt(v)
  47. // float64 strconv.FormatFloat(v, 'g', -1, 64)
  48. // bool true -> "1", false -> "0"
  49. // nil ""
  50. // all other types fmt.Print(v)
  51. //
  52. // Redis command reply types are represented using the following Go types:
  53. //
  54. // Redis type Go type
  55. // error redis.Error
  56. // integer int64
  57. // simple string string
  58. // bulk string []byte or nil if value not present.
  59. // array []interface{} or nil if value not present.
  60. //
  61. // Use type assertions or the reply helper functions to convert from
  62. // interface{} to the specific Go type for the command result.
  63. //
  64. // Pipelining
  65. //
  66. // Connections support pipelining using the Send, Flush and Receive methods.
  67. //
  68. // Send(commandName string, args ...interface{}) error
  69. // Flush() error
  70. // Receive() (reply interface{}, err error)
  71. //
  72. // Send writes the command to the connection's output buffer. Flush flushes the
  73. // connection's output buffer to the server. Receive reads a single reply from
  74. // the server. The following example shows a simple pipeline.
  75. //
  76. // c.Send("SET", "foo", "bar")
  77. // c.Send("GET", "foo")
  78. // c.Flush()
  79. // c.Receive() // reply from SET
  80. // v, err = c.Receive() // reply from GET
  81. //
  82. // The Do method combines the functionality of the Send, Flush and Receive
  83. // methods. The Do method starts by writing the command and flushing the output
  84. // buffer. Next, the Do method receives all pending replies including the reply
  85. // for the command just sent by Do. If any of the received replies is an error,
  86. // then Do returns the error. If there are no errors, then Do returns the last
  87. // reply. If the command argument to the Do method is "", then the Do method
  88. // will flush the output buffer and receive pending replies without sending a
  89. // command.
  90. //
  91. // Use the Send and Do methods to implement pipelined transactions.
  92. //
  93. // c.Send("MULTI")
  94. // c.Send("INCR", "foo")
  95. // c.Send("INCR", "bar")
  96. // r, err := c.Do("EXEC")
  97. // fmt.Println(r) // prints [1, 1]
  98. //
  99. // Concurrency
  100. //
  101. // Connections support one concurrent caller to the Recieve method and one
  102. // concurrent caller to the Send and Flush methods. No other concurrency is
  103. // supported including concurrent calls to the Do method.
  104. //
  105. // For full concurrent access to Redis, use the thread-safe Pool to get, use
  106. // and release a connection from within a goroutine. Connections returned from
  107. // a Pool have the concurrency restrictions described in the previous
  108. // paragraph.
  109. //
  110. // Publish and Subscribe
  111. //
  112. // Use the Send, Flush and Receive methods to implement Pub/Sub subscribers.
  113. //
  114. // c.Send("SUBSCRIBE", "example")
  115. // c.Flush()
  116. // for {
  117. // reply, err := c.Receive()
  118. // if err != nil {
  119. // return err
  120. // }
  121. // // process pushed message
  122. // }
  123. //
  124. // The PubSubConn type wraps a Conn with convenience methods for implementing
  125. // subscribers. The Subscribe, PSubscribe, Unsubscribe and PUnsubscribe methods
  126. // send and flush a subscription management command. The receive method
  127. // converts a pushed message to convenient types for use in a type switch.
  128. //
  129. // psc := redis.PubSubConn{c}
  130. // psc.Subscribe("example")
  131. // for {
  132. // switch v := psc.Receive().(type) {
  133. // case redis.Message:
  134. // fmt.Printf("%s: message: %s\n", v.Channel, v.Data)
  135. // case redis.Subscription:
  136. // fmt.Printf("%s: %s %d\n", v.Channel, v.Kind, v.Count)
  137. // case error:
  138. // return v
  139. // }
  140. // }
  141. //
  142. // Reply Helpers
  143. //
  144. // The Bool, Int, Bytes, String, Strings and Values functions convert a reply
  145. // to a value of a specific type. To allow convenient wrapping of calls to the
  146. // connection Do and Receive methods, the functions take a second argument of
  147. // type error. If the error is non-nil, then the helper function returns the
  148. // error. If the error is nil, the function converts the reply to the specified
  149. // type:
  150. //
  151. // exists, err := redis.Bool(c.Do("EXISTS", "foo"))
  152. // if err != nil {
  153. // // handle error return from c.Do or type conversion error.
  154. // }
  155. //
  156. // The Scan function converts elements of a array reply to Go types:
  157. //
  158. // var value1 int
  159. // var value2 string
  160. // reply, err := redis.Values(c.Do("MGET", "key1", "key2"))
  161. // if err != nil {
  162. // // handle error
  163. // }
  164. // if _, err := redis.Scan(reply, &value1, &value2); err != nil {
  165. // // handle error
  166. // }
  167. package redis // import "github.com/garyburd/redigo/redis"