Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

170 строки
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 do not support concurrent calls to the write methods (Send,
  102. // Flush) or concurrent calls to the read method (Receive). Connections do
  103. // allow a concurrent reader and writer.
  104. //
  105. // Because the Do method combines the functionality of Send, Flush and Receive,
  106. // the Do method cannot be called concurrently with the other methods.
  107. //
  108. // For full concurrent access to Redis, use the thread-safe Pool to get and
  109. // release connections from within a goroutine.
  110. //
  111. // Publish and Subscribe
  112. //
  113. // Use the Send, Flush and Receive methods to implement Pub/Sub subscribers.
  114. //
  115. // c.Send("SUBSCRIBE", "example")
  116. // c.Flush()
  117. // for {
  118. // reply, err := c.Receive()
  119. // if err != nil {
  120. // return err
  121. // }
  122. // // process pushed message
  123. // }
  124. //
  125. // The PubSubConn type wraps a Conn with convenience methods for implementing
  126. // subscribers. The Subscribe, PSubscribe, Unsubscribe and PUnsubscribe methods
  127. // send and flush a subscription management command. The receive method
  128. // converts a pushed message to convenient types for use in a type switch.
  129. //
  130. // psc := redis.PubSubConn{c}
  131. // psc.Subscribe("example")
  132. // for {
  133. // switch v := psc.Receive().(type) {
  134. // case redis.Message:
  135. // fmt.Printf("%s: message: %s\n", v.Channel, v.Data)
  136. // case redis.Subscription:
  137. // fmt.Printf("%s: %s %d\n", v.Channel, v.Kind, v.Count)
  138. // case error:
  139. // return v
  140. // }
  141. // }
  142. //
  143. // Reply Helpers
  144. //
  145. // The Bool, Int, Bytes, String, Strings and Values functions convert a reply
  146. // to a value of a specific type. To allow convenient wrapping of calls to the
  147. // connection Do and Receive methods, the functions take a second argument of
  148. // type error. If the error is non-nil, then the helper function returns the
  149. // error. If the error is nil, the function converts the reply to the specified
  150. // type:
  151. //
  152. // exists, err := redis.Bool(c.Do("EXISTS", "foo"))
  153. // if err != nil {
  154. // // handle error return from c.Do or type conversion error.
  155. // }
  156. //
  157. // The Scan function converts elements of a array reply to Go types:
  158. //
  159. // var value1 int
  160. // var value2 string
  161. // reply, err := redis.Values(c.Do("MGET", "key1", "key2"))
  162. // if err != nil {
  163. // // handle error
  164. // }
  165. // if _, err := redis.Scan(reply, &value1, &value2); err != nil {
  166. // // handle error
  167. // }
  168. package redis // import "github.com/garyburd/redigo/redis"