redis

package module
v0.1.18 Latest Latest
Warning

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

Go to latest
Published: May 30, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

README

Redis

基于redigo封装的Redis库

License GoDoc Go Report Card

特性

  • 简化Redis实例初始化流程,基于配置自动对Redis进行初始化
  • 支持连接池、多实例等场景
  • 基于Redis实现的分布式锁lock,支持加锁、等待锁释放、等待锁释放并加锁、基于锁实现幂等 等能力
  • 基于Redis实现延迟定时器timer,支持业务中触发延迟回调
  • 基于Redis和timer实现Storer,维护Mem、Redis、MySQL三者之间的数据同步关系(暂无法保证特殊情况下数据一致性),有助于基于对象开发时维护对象的同步

快速开始

  • toml 配置文件

    [redis.default]
        addr = "127.0.0.1:6379"
        pawd = "123456"
    [redis.stats]
        addr = "127.0.0.1:6379"
        pawd = "123456"
    
  • 使用方法

    // 初始化config包,参考config模块
    code...
    
    // 验证Redis实例的配置正确性与连通性。非必须
    if err = redisp.Valid(); err != nil {
        fmt.Printf("redis error: %s\n", err)
        os.Exit(-1)
    }
    
    // 如下方式可以直接使用Redis实例
    conn := redis.Client("default")
    defer conn.Close()
    
    conn.Do("GET", "key")
    
    // lock、timer、storer 的使用方法见代码
    

高级用法

  • 分布式锁、延迟定时器、Storer 使用方法参考代码

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Client

func Client(name string) (r rds.Conn)

func DefaultClient

func DefaultClient() (r rds.Conn)

func DefaultPool

func DefaultPool() (r *rds.Pool)

func HasError

func HasError(err error) bool

func NewRedisPool

func NewRedisPool(cfg *Cfg) *rds.Pool

func Pool

func Pool(name string) (r *rds.Pool)

func SafeClient

func SafeClient(name string) (r rds.Conn, err error)

func SafePool

func SafePool(name string) (r *rds.Pool, err error)

func Valid

func Valid(names ...string) (err error)

验证Redis实例的配置正确性与连通性。 参数names是实例的名称列表,如果为空则检测所有配置的实例

Types

type Cfg

type Cfg struct {
	// dial
	DialAddr           string        `toml:"addr"`
	DialUsername       string        `toml:"user"`
	DialPassword       string        `toml:"pawd"`
	DialDatabase       int           `toml:"database"`
	DialConnectTimeout time.Duration `toml:"dial_timeout"`
	DialReadTimeout    time.Duration `toml:"read_timeout"`
	DialWriteTimeout   time.Duration `toml:"write_timeout"`

	// pool
	PoolMaxIdle         int           `toml:"pool_max_idle"`           // 最大闲置连接数
	PoolMaxActive       int           `toml:"pool_max_active"`         // 最大活跃连接数
	PoolIdleTimeout     time.Duration `toml:"pool_idle_time"`          // 闲置的过期时间,在Get方法中会对过期的连接删除
	PoolWait            bool          `toml:"pool_wait"`               // 当活跃连接达到上限,Get时是等待还是返回错误。为false时返回错误,为true时阻塞等待
	PoolMaxConnLifetime time.Duration `toml:"pool_max_conn_life_time"` // 连接最长生存时间,当超过时间会被删除
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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