redis.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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
  15. import (
  16. "errors"
  17. "time"
  18. )
  19. // Error represents an error returned in a command reply.
  20. type Error string
  21. func (err Error) Error() string { return string(err) }
  22. // Conn represents a connection to a Redis server.
  23. type Conn interface {
  24. // Close closes the connection.
  25. Close() error
  26. // Err returns a non-nil value when the connection is not usable.
  27. Err() error
  28. // Do sends a command to the server and returns the received reply.
  29. Do(commandName string, args ...interface{}) (reply interface{}, err error)
  30. // Send writes the command to the client's output buffer.
  31. Send(commandName string, args ...interface{}) error
  32. // Flush flushes the output buffer to the Redis server.
  33. Flush() error
  34. // Receive receives a single reply from the Redis server
  35. Receive() (reply interface{}, err error)
  36. }
  37. // Argument is the interface implemented by an object which wants to control how
  38. // the object is converted to Redis bulk strings.
  39. type Argument interface {
  40. // RedisArg returns a value to be encoded as a bulk string per the
  41. // conversions listed in the section 'Executing Commands'.
  42. // Implementations should typically return a []byte or string.
  43. RedisArg() interface{}
  44. }
  45. // Scanner is implemented by an object which wants to control its value is
  46. // interpreted when read from Redis.
  47. type Scanner interface {
  48. // RedisScan assigns a value from a Redis value. The argument src is one of
  49. // the reply types listed in the section `Executing Commands`.
  50. //
  51. // An error should be returned if the value cannot be stored without
  52. // loss of information.
  53. RedisScan(src interface{}) error
  54. }
  55. // ConnWithTimeout is an optional interface that allows the caller to override
  56. // a connection's default read timeout. This interface is useful for executing
  57. // the BLPOP, BRPOP, BRPOPLPUSH, XREAD and other commands that block at the
  58. // server.
  59. //
  60. // A connection's default read timeout is set with the DialReadTimeout dial
  61. // option. Applications should rely on the default timeout for commands that do
  62. // not block at the server.
  63. //
  64. // All of the Conn implementations in this package satisfy the ConnWithTimeout
  65. // interface.
  66. //
  67. // Use the DoWithTimeout and ReceiveWithTimeout helper functions to simplify
  68. // use of this interface.
  69. type ConnWithTimeout interface {
  70. Conn
  71. // Do sends a command to the server and returns the received reply.
  72. // The timeout overrides the read timeout set when dialing the
  73. // connection.
  74. DoWithTimeout(timeout time.Duration, commandName string, args ...interface{}) (reply interface{}, err error)
  75. // Receive receives a single reply from the Redis server. The timeout
  76. // overrides the read timeout set when dialing the connection.
  77. ReceiveWithTimeout(timeout time.Duration) (reply interface{}, err error)
  78. }
  79. var errTimeoutNotSupported = errors.New("redis: connection does not support ConnWithTimeout")
  80. // DoWithTimeout executes a Redis command with the specified read timeout. If
  81. // the connection does not satisfy the ConnWithTimeout interface, then an error
  82. // is returned.
  83. func DoWithTimeout(c Conn, timeout time.Duration, cmd string, args ...interface{}) (interface{}, error) {
  84. cwt, ok := c.(ConnWithTimeout)
  85. if !ok {
  86. return nil, errTimeoutNotSupported
  87. }
  88. return cwt.DoWithTimeout(timeout, cmd, args...)
  89. }
  90. // ReceiveWithTimeout receives a reply with the specified read timeout. If the
  91. // connection does not satisfy the ConnWithTimeout interface, then an error is
  92. // returned.
  93. func ReceiveWithTimeout(c Conn, timeout time.Duration) (interface{}, error) {
  94. cwt, ok := c.(ConnWithTimeout)
  95. if !ok {
  96. return nil, errTimeoutNotSupported
  97. }
  98. return cwt.ReceiveWithTimeout(timeout)
  99. }