github.com/wfusion/gofusion@v1.1.14/common/infra/drivers/redis/interface.go (about)

     1  package redis
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/redis/go-redis/v9"
     7  
     8  	"github.com/wfusion/gofusion/common/utils"
     9  )
    10  
    11  type Dialect interface {
    12  	New(ctx context.Context, option Option, opts ...utils.OptionExtender) (redis *Redis, err error)
    13  }
    14  
    15  type newOption struct {
    16  	hooks []redis.Hook
    17  }
    18  
    19  type Option struct {
    20  	Cluster   bool     `yaml:"cluster" json:"cluster" toml:"cluster"`
    21  	Endpoints []string `yaml:"endpoints" json:"endpoints" toml:"endpoints"`
    22  	DB        uint     `yaml:"db" json:"db" toml:"db"`
    23  	User      string   `yaml:"user" json:"user" toml:"user"`
    24  	Password  string   `yaml:"password" json:"password" toml:"password" encrypted:""`
    25  
    26  	// Dial timeout for establishing new connections.
    27  	// Default is 5 seconds.
    28  	DialTimeout string `yaml:"dial_timeout" json:"dial_timeout" toml:"dial_timeout" default:"5s"`
    29  	// Timeout for socket reads. If reached, commands will fail
    30  	// with a timeout instead of blocking. Supported values:
    31  	//   - `0` - default timeout (3 seconds).
    32  	//   - `-1` - no timeout (block indefinitely).
    33  	//   - `-2` - disables SetReadDeadline calls completely.
    34  	ReadTimeout string `yaml:"read_timeout" json:"read_timeout" toml:"read_timeout" default:"3s"`
    35  	// Timeout for socket writes. If reached, commands will fail
    36  	// with a timeout instead of blocking.  Supported values:
    37  	//   - `0` - default timeout (3 seconds).
    38  	//   - `-1` - no timeout (block indefinitely).
    39  	//   - `-2` - disables SetWriteDeadline calls completely.
    40  	WriteTimeout string `yaml:"write_timeout" json:"write_timeout" toml:"write_timeout" default:"3s"`
    41  
    42  	// Minimum number of idle connections which is useful when establishing
    43  	// new connection is slow.
    44  	MinIdleConns int `yaml:"min_idle_conns" json:"min_idle_conns" toml:"min_idle_conns"`
    45  	// Maximum number of idle connections.
    46  	MaxIdleConns int `yaml:"max_idle_conns" json:"max_idle_conns" toml:"max_idle_conns"`
    47  	// ConnMaxIdleTime is the maximum amount of time a connection may be idle.
    48  	// Should be less than server's timeout.
    49  	//
    50  	// Expired connections may be closed lazily before reuse.
    51  	// If d <= 0, connections are not closed due to a connection's idle time.
    52  	//
    53  	// Default is 30 minutes. -1 disables idle timeout check.
    54  	ConnMaxIdleTime string `yaml:"conn_max_idle_time" json:"conn_max_idle_time" toml:"conn_max_idle_time" default:"30m"`
    55  	// ConnMaxLifetime is the maximum amount of time a connection may be reused.
    56  	//
    57  	// Expired connections may be closed lazily before reuse.
    58  	// If <= 0, connections are not closed due to a connection's age.
    59  	//
    60  	// Default is to not close idle connections.
    61  	ConnMaxLifetime string `yaml:"conn_max_life_time" json:"conn_max_life_time" toml:"conn_max_life_time"`
    62  
    63  	// Maximum number of retries before giving up.
    64  	// Default is 3 retries; -1 (not 0) disables retries.
    65  	MaxRetries int `yaml:"max_retries" json:"max_retries" toml:"max_retries" default:"3"`
    66  	// Minimum backoff between each retry.
    67  	// Default is 8 milliseconds; -1 disables backoff.
    68  	MinRetryBackoff string `yaml:"min_retry_backoff" json:"min_retry_backoff" toml:"min_retry_backoff" default:"8ms"`
    69  	// Maximum backoff between each retry.
    70  	// Default is 512 milliseconds; -1 disables backoff.
    71  	MaxRetryBackoff string `yaml:"max_retry_backoff" json:"max_retry_backoff" toml:"max_retry_backoff" default:"512ms"`
    72  
    73  	// Maximum number of socket connections.
    74  	// Default is 10 connections per every available CPU as reported by runtime.GOMAXPROCS.
    75  	PoolSize int `yaml:"pool_size" json:"pool_size" toml:"pool_size"`
    76  	// Amount of time client waits for connection if all connections
    77  	// are busy before returning an error.
    78  	// Default is ReadTimeout + 1 second.
    79  	PoolTimeout string `yaml:"pool_timeout" json:"pool_timeout" toml:"pool_timeout"`
    80  }
    81  
    82  type Redis struct {
    83  	redis redis.UniversalClient
    84  }
    85  
    86  func (r *Redis) GetProxy() redis.UniversalClient {
    87  	return r.redis
    88  }
    89  
    90  func (r *Redis) Close() error {
    91  	switch rdsCli := r.redis.(type) {
    92  	case *redis.ClusterClient:
    93  		return rdsCli.Close()
    94  	case *redis.Client:
    95  		return rdsCli.Close()
    96  	default:
    97  		return nil
    98  	}
    99  }
   100  
   101  func (r *Redis) PoolStatus() *redis.PoolStats {
   102  	switch rdsCli := r.redis.(type) {
   103  	case *redis.ClusterClient:
   104  		return rdsCli.PoolStats()
   105  	case *redis.Client:
   106  		return rdsCli.PoolStats()
   107  	default:
   108  		return new(redis.PoolStats)
   109  	}
   110  }