doc.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. // shareded 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. // 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, Flush and Receive methods.
  54. //
  55. // Send(commandName string, args ...interface{}) error
  56. // Flush() error
  57. // Receive() (reply interface{}, err error)
  58. //
  59. // Send writes the command to the connection's output buffer. Flush flushes the
  60. // connection's output buffer to the server. Receive reads a single reply from
  61. // the server. The following example shows a simple pipeline.
  62. //
  63. // c.Send("SET", "foo", "bar")
  64. // c.Send("GET", "foo")
  65. // c.Flush()
  66. // c.Receive() // reply from SET
  67. // v, err = c.Receive() // reply from GET
  68. //
  69. // The Do method combines the functionality of the Send, Flush and Receive
  70. // methods. The Do method starts by writing the command and flushing the output
  71. // buffer. Next, the Do method receives all pending replies including the reply
  72. // for the command just sent by Do. If any of the received replies is an error,
  73. // then Do returns the error. If there are no errors, then Do returns the last
  74. // reply.
  75. //
  76. // Use the Send and Do methods to implement pipelined transactions.
  77. //
  78. // c.Send("MULTI")
  79. // c.Send("INCR", "foo")
  80. // c.Send("INCR", "bar")
  81. // r, err := c.Do("EXEC")
  82. // fmt.Println(r) // prints [1, 1]
  83. //
  84. // Thread Safety
  85. //
  86. // The connection Send and Flush methods cannot be called concurrently with
  87. // other calls to these methods. The connection Receive method cannot be called
  88. // concurrently with other calls to Receive. Because the connection Do method
  89. // uses Send, Flush and Receive, the Do method cannot be called concurrently
  90. // with Send, Flush, Receive or Do. Unless stated otherwise, all other
  91. // concurrent access is allowed.
  92. //
  93. // Publish and Subscribe
  94. //
  95. // Use the Send, Flush and Receive methods to implement Pub/Sub subscribers.
  96. //
  97. // c.Send("SUBSCRIBE", "example")
  98. // c.Flush()
  99. // for {
  100. // reply, err := c.Receive()
  101. // if err != nil {
  102. // return err
  103. // }
  104. // // process pushed message
  105. // }
  106. //
  107. // The PubSubConn type wraps a Conn with convenience methods for implementing
  108. // subscribers. The Subscribe, PSubscribe, Unsubscribe and PUnsubscribe methods
  109. // send and flush a subscription management command. The receive method
  110. // converts a pushed message to convenient types for use in a type switch.
  111. //
  112. // psc := PubSubConn{c}
  113. // psc.Subscribe("example")
  114. // for {
  115. // switch v := psc.Receive().(type) {
  116. // case redis.Message:
  117. // fmt.Printf("%s: message: %s\n", v.Channel, v.Data)
  118. // case redis.Subscription:
  119. // fmt.Printf("%s: %s %d\n", v.Channel, v.Kind, v.Count)
  120. // case error:
  121. // return v
  122. // }
  123. package redis