doc.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. // status string
  58. // bulk []byte or nil if value not present.
  59. // multi-bulk []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.
  88. //
  89. // Use the Send and Do methods to implement pipelined transactions.
  90. //
  91. // c.Send("MULTI")
  92. // c.Send("INCR", "foo")
  93. // c.Send("INCR", "bar")
  94. // r, err := c.Do("EXEC")
  95. // fmt.Println(r) // prints [1, 1]
  96. //
  97. // Concurrency
  98. //
  99. // Connections support a single concurrent caller to the write methods (Send,
  100. // Flush) and a single concurrent caller to the read method (Receive). Because
  101. // Do method combines the functionality of Send, Flush and Receive, the Do
  102. // method cannot be called concurrently with the other methods.
  103. //
  104. // For full concurrent access to Redis, use the thread-safe Pool to get and
  105. // release connections from within a goroutine.
  106. //
  107. // Publish and Subscribe
  108. //
  109. // Use the Send, Flush and Receive methods to implement Pub/Sub subscribers.
  110. //
  111. // c.Send("SUBSCRIBE", "example")
  112. // c.Flush()
  113. // for {
  114. // reply, err := c.Receive()
  115. // if err != nil {
  116. // return err
  117. // }
  118. // // process pushed message
  119. // }
  120. //
  121. // The PubSubConn type wraps a Conn with convenience methods for implementing
  122. // subscribers. The Subscribe, PSubscribe, Unsubscribe and PUnsubscribe methods
  123. // send and flush a subscription management command. The receive method
  124. // converts a pushed message to convenient types for use in a type switch.
  125. //
  126. // psc := PubSubConn{c}
  127. // psc.Subscribe("example")
  128. // for {
  129. // switch v := psc.Receive().(type) {
  130. // case redis.Message:
  131. // fmt.Printf("%s: message: %s\n", v.Channel, v.Data)
  132. // case redis.Subscription:
  133. // fmt.Printf("%s: %s %d\n", v.Channel, v.Kind, v.Count)
  134. // case error:
  135. // return v
  136. // }
  137. //
  138. // Reply Helpers
  139. //
  140. // The Bool, Int, Bytes, String, Strings and Values functions convert a reply
  141. // to a value of a specific type. To allow convenient wrapping of calls to the
  142. // connection Do and Receive methods, the functions take a second argument of
  143. // type error. If the error is non-nil, then the helper function returns the
  144. // error. If the error is nil, the function converts the reply to the specified
  145. // type:
  146. //
  147. // exists, err := redis.Bool(c.Do("EXISTS", "foo"))
  148. // if err != nil {
  149. // // handle error return from c.Do or type conversion error.
  150. // }
  151. //
  152. // The Scan function converts elements of a multi-bulk reply to Go types:
  153. //
  154. // var value1 int
  155. // var value2 string
  156. // reply, err := redis.Values(c.Do("MGET", "key1", "key2"))
  157. // if err != nil {
  158. // // handle error
  159. // }
  160. // if _, err := redis.Scan(reply, &value1, &value2); err != nil {
  161. // // handle error
  162. // }
  163. package redis