gredis

package module
v0.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 18, 2025 License: MIT Imports: 13 Imported by: 0

README

gredis simple版本

前提概要

  1. 由于goframe的gredis过度封装导致无法使用底层go-redis的很多原生方法,所以自定义一个返回go-redis原生UniversalClient客户端
  2. 保留了原版从gcfg中读取配置文件的功能,但是增加clear方法用于使用类似nacos作为远程配置中心时,配置文件发生变化时,清除配置缓存和旧的客户端实例
  3. 需要在nacos配置中的OnChange方法中调用Clear,当配置文件发生变化时会回调清除现有配置缓存和客户端实例,不会干扰gf全局缓存,只会修改gredis局部缓存

使用注意

没啥要注意的,只要记得Clear()就行,测试用例里只包含一个incr用例和pipeline用例,因为完全就是go-redis的client的,直接看这玩意怎么用就行

后续

goframe v3版本计划废除gredis,可以期待一下下一个大版本,我看计划表有很多大改 具体看这个issues: https://github.com/gogf/gf/issues/4173

Documentation

Index

Constants

View Source
const (
	DefaultGroupName = "default"

	ConfigNodeNameRedis = "redis"
)

Variables

This section is empty.

Functions

func Clear

func Clear()

func New

func New(config *Config) redis.UniversalClient

func Redis

func Redis(name ...string) redis.UniversalClient

Types

type Config

type Config struct {
	// Address It supports single and cluster redis server. Multiple addresses joined with char ','. Eg: 192.168.1.1:6379, 192.168.1.2:6379.
	Address         string        `json:"address"`
	Db              int           `json:"db"`              // Redis db.
	User            string        `json:"user"`            // Username for AUTH.
	Pass            string        `json:"pass"`            // Password for AUTH.
	SentinelUser    string        `json:"sentinel_user"`   // Username for sentinel AUTH.
	SentinelPass    string        `json:"sentinel_pass"`   // Password for sentinel AUTH.
	MinIdle         int           `json:"minIdle"`         // Minimum number of connections allowed to be idle (default is 0)
	MaxIdle         int           `json:"maxIdle"`         // Maximum number of connections allowed to be idle (default is 10)
	MaxActive       int           `json:"maxActive"`       // Maximum number of connections limit (default is 0 means no limit).
	MaxConnLifetime time.Duration `json:"maxConnLifetime"` // Maximum lifetime of the connection (default is 30 seconds, not allowed to be set to 0)
	IdleTimeout     time.Duration `json:"idleTimeout"`     // Maximum idle time for connection (default is 10 seconds, not allowed to be set to 0)
	WaitTimeout     time.Duration `json:"waitTimeout"`     // Timed out duration waiting to get a connection from the connection pool.
	DialTimeout     time.Duration `json:"dialTimeout"`     // Dial connection timeout for TCP.
	ReadTimeout     time.Duration `json:"readTimeout"`     // Read timeout for TCP. DO NOT set it if not necessary.
	WriteTimeout    time.Duration `json:"writeTimeout"`    // Write timeout for TCP.
	MasterName      string        `json:"masterName"`      // Used in Redis Sentinel mode.
	TLS             bool          `json:"tls"`             // Specifies whether TLS should be used when connecting to the server.
	TLSSkipVerify   bool          `json:"tlsSkipVerify"`   // Disables server name verification when connecting over TLS.
	TLSConfig       *tls.Config   `json:"-"`               // TLS Config to use. When set TLS will be negotiated.
	SlaveOnly       bool          `json:"slaveOnly"`       // Route all commands to slave read-only nodes.
	Cluster         bool          `json:"cluster"`         // Specifies whether cluster mode be used.
	Protocol        int           `json:"protocol"`        // Specifies the RESP version (Protocol 2 or 3.)
}

func ConfigFromMap

func ConfigFromMap(m map[string]interface{}) (config *Config, err error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL