gredis.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. // Package gredis provides convenient client for redis server.
  7. //
  8. // Redis Client.
  9. //
  10. // Redis Commands Official: https://redis.io/commands
  11. //
  12. // Redis Chinese Documentation: http://redisdoc.com/
  13. package gredis
  14. import (
  15. "context"
  16. "fmt"
  17. "github.com/gogf/gf/internal/intlog"
  18. "time"
  19. "github.com/gogf/gf/container/gmap"
  20. "github.com/gogf/gf/container/gvar"
  21. "github.com/gomodule/redigo/redis"
  22. )
  23. // Redis client.
  24. type Redis struct {
  25. pool *redis.Pool // Underlying connection pool.
  26. group string // Configuration group.
  27. config *Config // Configuration.
  28. ctx context.Context // Context.
  29. }
  30. // Conn is redis connection.
  31. type Conn struct {
  32. redis.Conn
  33. ctx context.Context
  34. redis *Redis
  35. }
  36. // Config is redis configuration.
  37. type Config struct {
  38. Host string `json:"host"`
  39. Port int `json:"port"`
  40. Db int `json:"db"`
  41. Pass string `json:"pass"` // Password for AUTH.
  42. MaxIdle int `json:"maxIdle"` // Maximum number of connections allowed to be idle (default is 10)
  43. MaxActive int `json:"maxActive"` // Maximum number of connections limit (default is 0 means no limit).
  44. IdleTimeout time.Duration `json:"idleTimeout"` // Maximum idle time for connection (default is 10 seconds, not allowed to be set to 0)
  45. MaxConnLifetime time.Duration `json:"maxConnLifetime"` // Maximum lifetime of the connection (default is 30 seconds, not allowed to be set to 0)
  46. ConnectTimeout time.Duration `json:"connectTimeout"` // Dial connection timeout.
  47. TLS bool `json:"tls"` // Specifies the config to use when a TLS connection is dialed.
  48. TLSSkipVerify bool `json:"tlsSkipVerify"` // Disables server name verification when connecting over TLS.
  49. }
  50. // PoolStats is statistics of redis connection pool.
  51. type PoolStats struct {
  52. redis.PoolStats
  53. }
  54. const (
  55. defaultPoolIdleTimeout = 10 * time.Second
  56. defaultPoolConnTimeout = 10 * time.Second
  57. defaultPoolMaxIdle = 10
  58. defaultPoolMaxActive = 100
  59. defaultPoolMaxLifeTime = 30 * time.Second
  60. )
  61. var (
  62. // Pool map.
  63. pools = gmap.NewStrAnyMap(true)
  64. )
  65. // New creates a redis client object with given configuration.
  66. // Redis client maintains a connection pool automatically.
  67. func New(config *Config) *Redis {
  68. // The MaxIdle is the most important attribute of the connection pool.
  69. // Only if this attribute is set, the created connections from client
  70. // can not exceed the limit of the server.
  71. if config.MaxIdle == 0 {
  72. config.MaxIdle = defaultPoolMaxIdle
  73. }
  74. // This value SHOULD NOT exceed the connection limit of redis server.
  75. if config.MaxActive == 0 {
  76. config.MaxActive = defaultPoolMaxActive
  77. }
  78. if config.IdleTimeout == 0 {
  79. config.IdleTimeout = defaultPoolIdleTimeout
  80. }
  81. if config.ConnectTimeout == 0 {
  82. config.ConnectTimeout = defaultPoolConnTimeout
  83. }
  84. if config.MaxConnLifetime == 0 {
  85. config.MaxConnLifetime = defaultPoolMaxLifeTime
  86. }
  87. return &Redis{
  88. config: config,
  89. pool: pools.GetOrSetFuncLock(fmt.Sprintf("%v", config), func() interface{} {
  90. return &redis.Pool{
  91. Wait: true,
  92. IdleTimeout: config.IdleTimeout,
  93. MaxActive: config.MaxActive,
  94. MaxIdle: config.MaxIdle,
  95. MaxConnLifetime: config.MaxConnLifetime,
  96. Dial: func() (redis.Conn, error) {
  97. c, err := redis.Dial(
  98. "tcp",
  99. fmt.Sprintf("%s:%d", config.Host, config.Port),
  100. redis.DialConnectTimeout(config.ConnectTimeout),
  101. redis.DialUseTLS(config.TLS),
  102. redis.DialTLSSkipVerify(config.TLSSkipVerify),
  103. )
  104. if err != nil {
  105. return nil, err
  106. }
  107. intlog.Printf(context.TODO(), `open new connection, config:%+v`, config)
  108. // AUTH
  109. if len(config.Pass) > 0 {
  110. if _, err := c.Do("AUTH", config.Pass); err != nil {
  111. return nil, err
  112. }
  113. }
  114. // DB
  115. if _, err := c.Do("SELECT", config.Db); err != nil {
  116. return nil, err
  117. }
  118. return c, nil
  119. },
  120. // After the conn is taken from the connection pool, to test if the connection is available,
  121. // If error is returned then it closes the connection object and recreate a new connection.
  122. TestOnBorrow: func(c redis.Conn, t time.Time) error {
  123. _, err := c.Do("PING")
  124. return err
  125. },
  126. }
  127. }).(*redis.Pool),
  128. }
  129. }
  130. // NewFromStr creates a redis client object with given configuration string.
  131. // Redis client maintains a connection pool automatically.
  132. // The parameter <str> like:
  133. // 127.0.0.1:6379,0
  134. // 127.0.0.1:6379,0,password
  135. func NewFromStr(str string) (*Redis, error) {
  136. config, err := ConfigFromStr(str)
  137. if err != nil {
  138. return nil, err
  139. }
  140. return New(config), nil
  141. }
  142. // Close closes the redis connection pool,
  143. // it will release all connections reserved by this pool.
  144. // It is not necessary to call Close manually.
  145. func (r *Redis) Close() error {
  146. if r.group != "" {
  147. // If it is an instance object,
  148. // it needs to remove it from the instance Map.
  149. instances.Remove(r.group)
  150. }
  151. pools.Remove(fmt.Sprintf("%v", r.config))
  152. return r.pool.Close()
  153. }
  154. // Clone clones and returns a new Redis object, which is a shallow copy of current one.
  155. func (r *Redis) Clone() *Redis {
  156. newRedis := New(r.config)
  157. *newRedis = *r
  158. return newRedis
  159. }
  160. // Ctx is a channing function which sets the context for next operation.
  161. func (r *Redis) Ctx(ctx context.Context) *Redis {
  162. newRedis := r.Clone()
  163. newRedis.ctx = ctx
  164. return newRedis
  165. }
  166. // Conn returns a raw underlying connection object,
  167. // which expose more methods to communicate with server.
  168. // **You should call Close function manually if you do not use this connection any further.**
  169. func (r *Redis) Conn() *Conn {
  170. return &Conn{
  171. Conn: r.pool.Get(),
  172. ctx: r.ctx,
  173. redis: r,
  174. }
  175. }
  176. // GetConn is alias of Conn, see Conn.
  177. // Deprecated, use Conn instead.
  178. func (r *Redis) GetConn() *Conn {
  179. return r.Conn()
  180. }
  181. // SetMaxIdle sets the maximum number of idle connections in the pool.
  182. func (r *Redis) SetMaxIdle(value int) {
  183. r.pool.MaxIdle = value
  184. }
  185. // SetMaxActive sets the maximum number of connections allocated by the pool at a given time.
  186. // When zero, there is no limit on the number of connections in the pool.
  187. //
  188. // Note that if the pool is at the MaxActive limit, then all the operations will wait for
  189. // a connection to be returned to the pool before returning.
  190. func (r *Redis) SetMaxActive(value int) {
  191. r.pool.MaxActive = value
  192. }
  193. // SetIdleTimeout sets the IdleTimeout attribute of the connection pool.
  194. // It closes connections after remaining idle for this duration. If the value
  195. // is zero, then idle connections are not closed. Applications should set
  196. // the timeout to a value less than the server's timeout.
  197. func (r *Redis) SetIdleTimeout(value time.Duration) {
  198. r.pool.IdleTimeout = value
  199. }
  200. // SetMaxConnLifetime sets the MaxConnLifetime attribute of the connection pool.
  201. // It closes connections older than this duration. If the value is zero, then
  202. // the pool does not close connections based on age.
  203. func (r *Redis) SetMaxConnLifetime(value time.Duration) {
  204. r.pool.MaxConnLifetime = value
  205. }
  206. // Stats returns pool's statistics.
  207. func (r *Redis) Stats() *PoolStats {
  208. return &PoolStats{r.pool.Stats()}
  209. }
  210. // Do sends a command to the server and returns the received reply.
  211. // Do automatically get a connection from pool, and close it when the reply received.
  212. // It does not really "close" the connection, but drops it back to the connection pool.
  213. func (r *Redis) Do(commandName string, args ...interface{}) (interface{}, error) {
  214. conn := &Conn{
  215. Conn: r.pool.Get(),
  216. ctx: r.ctx,
  217. redis: r,
  218. }
  219. defer conn.Close()
  220. return conn.Do(commandName, args...)
  221. }
  222. // DoWithTimeout sends a command to the server and returns the received reply.
  223. // The timeout overrides the read timeout set when dialing the connection.
  224. func (r *Redis) DoWithTimeout(timeout time.Duration, commandName string, args ...interface{}) (interface{}, error) {
  225. conn := &Conn{
  226. Conn: r.pool.Get(),
  227. ctx: r.ctx,
  228. redis: r,
  229. }
  230. defer conn.Close()
  231. return conn.DoWithTimeout(timeout, commandName, args...)
  232. }
  233. // DoVar returns value from Do as gvar.Var.
  234. func (r *Redis) DoVar(commandName string, args ...interface{}) (*gvar.Var, error) {
  235. return resultToVar(r.Do(commandName, args...))
  236. }
  237. // DoVarWithTimeout returns value from Do as gvar.Var.
  238. // The timeout overrides the read timeout set when dialing the connection.
  239. func (r *Redis) DoVarWithTimeout(timeout time.Duration, commandName string, args ...interface{}) (*gvar.Var, error) {
  240. return resultToVar(r.DoWithTimeout(timeout, commandName, args...))
  241. }