redis

package module
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2021 License: MIT Imports: 10 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

redis

base on go-redis library

Build Status codecov Go Report Card
GoDoc

Usage

go get github.com/goapt/redis
configs := make(map[string]redis.Config)

configs["default"] = Config{
    Server: "127.0.0.1:6379",
}

redis.Connect(configs)
client := redis.NewRedisWithName("default")

Documentation

Index

Constants

View Source
const (
	CACHE_WEEK_TTL = 604800 * time.Second // 7天
	CACHE_DAY_TTL  = 86400 * time.Second  // 1天
	CACHE_HOUR_TTL = 3600 * time.Second   // 1小时
	CACHE_MIN_TTL  = 60 * time.Second     // 1分钟
)

Variables

View Source
var DecodeHook = func(from reflect.Type, to reflect.Type, v interface{}) (interface{}, error) {
	if from.Kind() == reflect.String && to.Kind() == reflect.Struct {

		if to.String() == "time.Time" || to.String() == "*time.Time" {
			ss := v.(string)
			t := time.Time{}
			err := t.UnmarshalBinary([]byte(ss))
			if err != nil {
				var layout = "2006-01-02 15:04:05"
				if strings.Index(ss, "T") != -1 {
					layout = time.RFC3339
				}
				t, err = time.ParseInLocation(layout, ss, loc)
				if err != nil {
					return nil, err
				}
			}
			return t, nil
		}

		vi := reflect.New(to).Interface()
		if vv, ok := vi.(interface{ Scan(interface{}) error }); ok {
			err := vv.Scan(v)
			if err != nil {
				return nil, err
			}
			return vv, nil
		}
	}

	return v, nil
}
View Source
var ErrClosed = redis.ErrClosed
View Source
var ErrNoData = errors.New("data is empty")
View Source
var (
	ScanTagName = "db"
)

Functions

func Client

func Client(name ...string) *redis.Client

func Connect

func Connect(configs map[string]Config)

func IsNil added in v1.3.0

func IsNil(err error) bool

func Open added in v1.3.0

func Open(addr string, options ...func(options *redis.Options)) *redis.Client

Open redis client

func OpenSentinel added in v1.3.0

func OpenSentinel(options func(options *redis.FailoverOptions)) *redis.Client

Open redis client

Types

type Config

type Config struct {
	Server       string
	Password     string
	DB           int
	MaxRetries   int
	DialTimeout  int `json:"dial_timeout" toml:"dial_timeout"`
	ReadTimeout  int `json:"read_timeout" toml:"read_timeout"`
	WriteTimeout int `json:"write_timeout" toml:"write_timeout"`

	// sentinel
	MasterName       string `json:"master_name" toml:"master_name"`
	SentinelAddrs    string `json:"sentinel_addrs" toml:"sentinel_addrs"`
	SentinelUsername string `json:"sentinel_username" toml:"sentinel_username"`
	SentinelPassword string `json:"sentinel_password" toml:"sentinel_password"`
	CaCert           string `json:"ca_cert" toml:"ca_cert"`
	CertFile         string `json:"cert_file" toml:"cert_file"`
	CertKey          string `json:"cert_key" toml:"cert_key"`
}

type Redis added in v1.3.0

type Redis struct {
	// contains filtered or unexported fields
}

func NewRedis added in v1.3.0

func NewRedis(client *redis.Client) *Redis

func NewRedisWithName added in v1.3.0

func NewRedisWithName(name string) *Redis

func (*Redis) Client added in v1.3.0

func (b *Redis) Client() *redis.Client

func (*Redis) Decr added in v1.3.0

func (b *Redis) Decr(key string) (int64, error)

func (*Redis) DecrBy added in v1.3.0

func (b *Redis) DecrBy(key string, value int64) (int64, error)

func (*Redis) Del added in v1.3.0

func (b *Redis) Del(key string) error

func (*Redis) Exists added in v1.3.0

func (b *Redis) Exists(key string) bool

func (*Redis) Expire added in v1.3.0

func (b *Redis) Expire(key string, expiration time.Duration) (bool, error)

func (*Redis) Get added in v1.3.0

func (b *Redis) Get(key string) (string, error)

func (*Redis) HDel added in v1.3.0

func (b *Redis) HDel(key string, field string) error

func (*Redis) HExists added in v1.3.0

func (b *Redis) HExists(key string, field string) (bool, error)

func (*Redis) HGet added in v1.3.0

func (b *Redis) HGet(key string, field string) (string, error)

func (*Redis) HGetAll added in v1.3.0

func (b *Redis) HGetAll(key string, m interface{}) error

func (*Redis) HGetAllMap added in v1.3.0

func (b *Redis) HGetAllMap(key string) (map[string]string, error)

func (*Redis) HIncrBy added in v1.3.0

func (b *Redis) HIncrBy(key string, field string, incr int64) (int64, error)

func (*Redis) HMSet added in v1.3.0

func (b *Redis) HMSet(key string, m interface{}) error

HMSet support gosql.Model and map[string]interface{}

func (*Redis) HSet added in v1.3.0

func (b *Redis) HSet(key string, field string, value interface{}) error

func (*Redis) Incr added in v1.3.0

func (b *Redis) Incr(key string) (int64, error)

func (*Redis) IncrBy added in v1.3.0

func (b *Redis) IncrBy(key string, value int64) (int64, error)

func (*Redis) LPop added in v1.3.0

func (b *Redis) LPop(key string) (string, error)

func (*Redis) LPush added in v1.3.0

func (b *Redis) LPush(key string, values ...interface{}) (int64, error)

func (*Redis) RPop added in v1.3.0

func (b *Redis) RPop(key string) (string, error)

func (*Redis) RPush added in v1.3.0

func (b *Redis) RPush(key string, values ...interface{}) (int64, error)

func (*Redis) SAdd added in v1.3.0

func (b *Redis) SAdd(key string, members ...interface{}) (int64, error)

func (*Redis) Set added in v1.3.0

func (b *Redis) Set(key, val string) error

Set Our specification is that all keys must have an expiration time, so the Set key will expire in one day by default

func (*Redis) SetEX added in v1.3.0

func (b *Redis) SetEX(key, val string, expiration time.Duration) error

func (*Redis) SetNX added in v1.3.0

func (b *Redis) SetNX(key, val string, expiration time.Duration) (bool, error)

func (*Redis) TTL added in v1.3.0

func (b *Redis) TTL(key string) time.Duration

Jump to

Keyboard shortcuts

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