doc.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. Package redis supports
  15. // Redis version 1.2.0 and above. All commands are supported.
  16. //
  17. // Connections
  18. //
  19. // The Conn interface is the primary interface for working with Redis.
  20. // Applications create connections by calling the Dial, DialWithTimeout or
  21. // NewConn functions. In the future, functions will be added for creating
  22. // sharded and other types of connections.
  23. //
  24. // The application must call the connection Close method when the application
  25. // is done with the connection.
  26. //
  27. // Executing Commands
  28. //
  29. // The Conn interface has a generic method for executing Redis commands:
  30. //
  31. // Do(commandName string, args ...interface{}) (reply interface{}, err error)
  32. //
  33. // Arguments of type string and []byte are sent to the server as is. The value
  34. // false is converted to "0" and the value true is converted to "1". The value
  35. // nil is converted to "". All other values are converted to a string using the
  36. // fmt.Fprint function. Command replies are represented using the following Go
  37. // types:
  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. // The Redis command reference (http://redis.io/commands) documents the Redis
  47. // type returned for each command. Use type assertions to convert from
  48. // interface{} to the specific Go type for the command result.
  49. //
  50. // Pipelining
  51. //
  52. // Connections support pipelining using the Send, Flush and Receive methods.
  53. //
  54. // Send(commandName string, args ...interface{}) error
  55. // Flush() error
  56. // Receive() (reply interface{}, err error)
  57. //
  58. // Send writes the command to the connection's output buffer. Flush flushes the
  59. // connection's output buffer to the server. Receive reads a single reply from
  60. // the server. The following example shows a simple pipeline.
  61. //
  62. // c.Send("SET", "foo", "bar")
  63. // c.Send("GET", "foo")
  64. // c.Flush()
  65. // c.Receive() // reply from SET
  66. // v, err = c.Receive() // reply from GET
  67. //
  68. // The Do method combines the functionality of the Send, Flush and Receive
  69. // methods. The Do method starts by writing the command and flushing the output
  70. // buffer. Next, the Do method receives all pending replies including the reply
  71. // for the command just sent by Do. If any of the received replies is an error,
  72. // then Do returns the error. If there are no errors, then Do returns the last
  73. // reply.
  74. //
  75. // Use the Send and Do methods to implement pipelined transactions.
  76. //
  77. // c.Send("MULTI")
  78. // c.Send("INCR", "foo")
  79. // c.Send("INCR", "bar")
  80. // r, err := c.Do("EXEC")
  81. // fmt.Println(r) // prints [1, 1]
  82. //
  83. // Thread Safety
  84. //
  85. // The connection Send and Flush methods cannot be called concurrently with
  86. // other calls to these methods. The connection Receive method cannot be called
  87. // concurrently with other calls to Receive. Because the connection Do method
  88. // uses Send, Flush and Receive, the Do method cannot be called concurrently
  89. // with Send, Flush, Receive or Do. Unless stated otherwise, all other
  90. // concurrent access is allowed.
  91. //
  92. // Publish and Subscribe
  93. //
  94. // Use the Send, Flush and Receive methods to implement Pub/Sub subscribers.
  95. //
  96. // c.Send("SUBSCRIBE", "example")
  97. // c.Flush()
  98. // for {
  99. // reply, err := c.Receive()
  100. // if err != nil {
  101. // return err
  102. // }
  103. // // process pushed message
  104. // }
  105. //
  106. // The PubSubConn type wraps a Conn with convenience methods for implementing
  107. // subscribers. The Subscribe, PSubscribe, Unsubscribe and PUnsubscribe methods
  108. // send and flush a subscription management command. The receive method
  109. // converts a pushed message to convenient types for use in a type switch.
  110. //
  111. // psc := PubSubConn{c}
  112. // psc.Subscribe("example")
  113. // for {
  114. // switch v := psc.Receive().(type) {
  115. // case redis.Message:
  116. // fmt.Printf("%s: message: %s\n", v.Channel, v.Data)
  117. // case redis.Subscription:
  118. // fmt.Printf("%s: %s %d\n", v.Channel, v.Kind, v.Count)
  119. // case error:
  120. // return v
  121. // }
  122. //
  123. // Reply Helpers
  124. //
  125. // The Bool, Int, Bytes, String, Strings and Values functions convert a reply
  126. // to a value of a specific type. To allow convenient wrapping of calls to the
  127. // connection Do and Receive methods, the functions take a second argument of
  128. // type error. If the error is non-nil, then the helper function returns the
  129. // error. If the error is nil, the function converts the reply to the specified
  130. // type:
  131. //
  132. // exists, err := redis.Bool(c.Do("EXISTS", "foo"))
  133. // if err != nil {
  134. // // handle error return from c.Do or type conversion error.
  135. // }
  136. //
  137. // The Scan function converts elements of a multi-bulk reply to Go types:
  138. //
  139. // var value1 int
  140. // var value2 string
  141. // reply, err := redis.Values(c.Do("MGET", "key1", "key2"))
  142. // if err != nil {
  143. // // handle error
  144. // }
  145. // if _, err := redis.Scan(reply, &value1, &value2); err != nil {
  146. // // handle error
  147. // }
  148. package redis