cache

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2021 License: Apache-2.0 Imports: 13 Imported by: 0

README

go-lib-cache

基础开发框架缓存组件

依赖

  • github.com/BurntSushi/toml
  • github.com/go-redis/redis/v7
  • github.com/opentracing/opentracing-go
  • github.com/stretchr/testify

xhash

介绍
  • 读取 hash 类型数据可以方便的将 map 转成结构体
  • 写入 hash 类型数据可以将结构体转成 map
  • 结构体可以被其他 sdk 复用,例如 xorm, gorm 等
类型支持
  • Bool *Bool
  • Int Int8 Int16 Int32 Int64
  • *Int *Int8 *Int16 *Int32 *Int64
  • Uint Uint8 Uint16 Uint32 Uint64
  • *Uint *Uint8 *Uint16 *Uint32 *Uint64
  • Float32 *Float32 Float64 *Float64
  • Interface
  • Map
  • Slice
  • String *String
  • Struct *Struct
  • time.Time *time.Time
  • alias 例: type UserStatus int
快速开始
// model 转 map
result, err := xhash.Model2map(yourModel)

// map 转 model
yourModel := new(Model)
err := xhash.Map2model(mapVar, yourModel)

trace支持

redisConfig := new(cache.RedisConfig)
err := Cfg("redis").UnmarshalKey(name, redisConfig)
if err != nil {
    log.WithError(err).Error("load redis config err")
    return nil
}
redisEngine, err := cache.CreateRedisClient(name, redisConfig)
if err != nil {
    log.WithError(err).Error("create redis engine err")
    return nil
}
redisEngine.Client.AddHook(&RedisHook{
    RedisConfig: redisConfig,
})
WithCtx
redisEngine.WithCtx(ctx)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NotPtrErr = errors.New("input type is not a slice ptr")
View Source
var NotSliceErr = errors.New("input type is not a slice")

Functions

func IsRedisNil

func IsRedisNil(err error) bool

func MapStringToMapInterface

func MapStringToMapInterface(v map[string]string) map[string]interface{}

func MapStringToMapRedisString

func MapStringToMapRedisString(v map[string]string) map[string]RedisString

Types

type Caches

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

----------------------------------------

Redis 客户端管理器

----------------------------------------

func CreateCaches

func CreateCaches() *Caches

func (*Caches) Redis

func (caches *Caches) Redis(name string) *RedisClient

func (*Caches) RegisterRedisClient

func (caches *Caches) RegisterRedisClient(name string, config *RedisConfig) error

func (*Caches) RegisterRedisClients

func (caches *Caches) RegisterRedisClients(configs map[string]*RedisConfig) error

type RedisClient

type RedisClient struct {
	*redis.Client
	// contains filtered or unexported fields
}

----------------------------------------

Redis 客户端包装

----------------------------------------

func CreateRedisClient

func CreateRedisClient(name string, config *RedisConfig) (*RedisClient, error)

func CreateRedisClientWithDriver

func CreateRedisClientWithDriver(name string, driver *redis.Client) (*RedisClient, error)

func (*RedisClient) Close

func (client *RedisClient) Close() error

func (*RedisClient) Config

func (client *RedisClient) Config() *RedisConfig

func (*RedisClient) Connect

func (client *RedisClient) Connect() error

func (*RedisClient) Copy

func (client *RedisClient) Copy() *RedisClient

func (*RedisClient) Counter

func (client *RedisClient) Counter(key string, args ...interface{}) *RedisCounter

func (*RedisClient) JSON

func (client *RedisClient) JSON(key string, args ...interface{}) *RedisJSON

func (*RedisClient) List

func (client *RedisClient) List(key string, args ...interface{}) *RedisList

func (*RedisClient) Locker

func (client *RedisClient) Locker(key string, args ...interface{}) *RedisLocker

func (*RedisClient) Marker

func (client *RedisClient) Marker(key string, args ...interface{}) *RedisMarker

func (*RedisClient) Name

func (client *RedisClient) Name() string

func (*RedisClient) Ping

func (client *RedisClient) Ping() error

func (*RedisClient) Reconnect

func (client *RedisClient) Reconnect() error

func (*RedisClient) WithCtx

func (client *RedisClient) WithCtx(ctx context.Context) *RedisClient

type RedisClientManager

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

----------------------------------------

Redis 客户端管理器

----------------------------------------

func CreateRedisClientManager

func CreateRedisClientManager() *RedisClientManager

func (*RedisClientManager) Clear

func (manager *RedisClientManager) Clear()

func (*RedisClientManager) Get

func (manager *RedisClientManager) Get(name string) *RedisClient

func (*RedisClientManager) Set

func (manager *RedisClientManager) Set(name string, client *RedisClient)

type RedisConfig

type RedisConfig struct {
	Host         string `toml:"host" mapstructure:"host"`
	Port         int    `toml:"port" mapstructure:"port"`
	Password     string `toml:"password" mapstructure:"password"`
	Database     int    `toml:"database" mapstructure:"database"`
	MaxRetries   int    `toml:"max_retries" mapstructure:"max_retries"`
	PoolSize     int    `toml:"pool_size" mapstructure:"pool_size"`
	Ping         bool   `toml:"ping" mapstructure:"ping"`
	DialTimeout  int    `toml:"dial_timeout" mapstructure:"dial_timeout"`
	ReadTimeout  int    `toml:"read_timeout" mapstructure:"read_timeout"`
	WriteTimeout int    `toml:"write_timeout" mapstructure:"write_timeout"`
}

----------------------------------------

Redis 配置项(TOML 支持, Viper 支持)

----------------------------------------

func (*RedisConfig) Bind

func (config *RedisConfig) Bind(bs []byte) error

func (*RedisConfig) Copy

func (config *RedisConfig) Copy() *RedisConfig

获取当前 Redis 配置项的副本

type RedisConfigs

type RedisConfigs struct {
	Items map[string]*RedisConfig `toml:"redis"`
}

----------------------------------------

Redis 配置清单(TOML 支持)

----------------------------------------

func (*RedisConfigs) Bind

func (configs *RedisConfigs) Bind(bs []byte) error

绑定数据到当前 Redis 配置清单

type RedisCounter

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

----------------------------------------

Redis 计数器

----------------------------------------

func CreateRedisCounter

func CreateRedisCounter(client *RedisClient, key string, args ...interface{}) *RedisCounter

func (*RedisCounter) Count

func (c *RedisCounter) Count() (int64, error)

func (*RedisCounter) Decr

func (c *RedisCounter) Decr() error

func (*RedisCounter) DecrOfMarker

func (c *RedisCounter) DecrOfMarker(marker *RedisMarker) error

func (*RedisCounter) Destroy

func (c *RedisCounter) Destroy() error

func (*RedisCounter) Incr

func (c *RedisCounter) Incr() error

func (*RedisCounter) IncrOfMarker

func (c *RedisCounter) IncrOfMarker(marker *RedisMarker) error

func (*RedisCounter) Key

func (c *RedisCounter) Key() string

func (*RedisCounter) Set

func (c *RedisCounter) Set(count int64, expiration time.Duration) error

func (*RedisCounter) Zero

func (c *RedisCounter) Zero(expiration time.Duration) error

type RedisJSON

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

----------------------------------------

Redis JSON 数据

----------------------------------------

func CreateRedisJSON

func CreateRedisJSON(client *RedisClient, key string, args ...interface{}) *RedisJSON

func (*RedisJSON) Destroy

func (h *RedisJSON) Destroy() error

func (*RedisJSON) Exists

func (j *RedisJSON) Exists() (bool, error)

func (*RedisJSON) Get

func (j *RedisJSON) Get(v interface{}) error

func (*RedisJSON) Key

func (j *RedisJSON) Key() string

func (*RedisJSON) Set

func (j *RedisJSON) Set(v interface{}, expiration time.Duration) error

func (*RedisJSON) SetNX

func (j *RedisJSON) SetNX(v interface{}, expiration time.Duration) error

func (*RedisJSON) SetXX

func (j *RedisJSON) SetXX(v interface{}, expiration time.Duration) error

func (*RedisJSON) TrySetNX

func (j *RedisJSON) TrySetNX(v interface{}, expiration time.Duration) (bool, error)

func (*RedisJSON) TrySetXX

func (j *RedisJSON) TrySetXX(v interface{}, expiration time.Duration) (bool, error)

type RedisKey

type RedisKey string

----------------------------------------

Redis 键

----------------------------------------

func (RedisKey) Format

func (k RedisKey) Format(args ...interface{}) string

func (RedisKey) String

func (k RedisKey) String() string

type RedisList

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

----------------------------------------

Redis List

----------------------------------------

func CreateRedisList

func CreateRedisList(client *RedisClient, key string, args ...interface{}) *RedisList

func (*RedisList) LIndex

func (list *RedisList) LIndex(index int64, o interface{}) error

func (*RedisList) LPop

func (list *RedisList) LPop(o interface{}) error

func (*RedisList) LRang

func (list *RedisList) LRang(o interface{}) error

func (*RedisList) RPop

func (list *RedisList) RPop(o interface{}) error

func (*RedisList) RRang

func (list *RedisList) RRang(o interface{}) error

type RedisLocker

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

----------------------------------------

Redis 锁

----------------------------------------

func CreateRedisLocker

func CreateRedisLocker(client *RedisClient, key string, args ...interface{}) *RedisLocker

func (*RedisLocker) DoLock

func (locker *RedisLocker) DoLock() (ok bool, err error)

func (*RedisLocker) DoUnlock

func (locker *RedisLocker) DoUnlock() (bool, error)

func (*RedisLocker) GetExpiration

func (locker *RedisLocker) GetExpiration() time.Duration

func (*RedisLocker) GetRetries

func (locker *RedisLocker) GetRetries() int

func (*RedisLocker) Key

func (locker *RedisLocker) Key() string

func (*RedisLocker) Lock

func (locker *RedisLocker) Lock() bool

func (*RedisLocker) SetExpiration

func (locker *RedisLocker) SetExpiration(expiration time.Duration) *RedisLocker

func (*RedisLocker) SetInterval

func (locker *RedisLocker) SetInterval(interval time.Duration) *RedisLocker

func (*RedisLocker) SetRetries

func (locker *RedisLocker) SetRetries(retries int) *RedisLocker

func (*RedisLocker) Touch

func (locker *RedisLocker) Touch(duration time.Duration) error

func (*RedisLocker) TouchAt

func (locker *RedisLocker) TouchAt(t time.Time) error

func (*RedisLocker) Unlock

func (locker *RedisLocker) Unlock() bool

type RedisMarker

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

func CreateRedisMarker

func CreateRedisMarker(client *RedisClient, key string, args ...interface{}) *RedisMarker

func (*RedisMarker) Exist

func (marker *RedisMarker) Exist() (bool, error)

func (*RedisMarker) Expire

func (marker *RedisMarker) Expire(duration time.Duration) error

func (*RedisMarker) ExpireAt

func (marker *RedisMarker) ExpireAt(t time.Time) error

func (*RedisMarker) GetExpiration

func (marker *RedisMarker) GetExpiration() time.Duration

func (*RedisMarker) Key

func (marker *RedisMarker) Key() string

func (*RedisMarker) Mark

func (marker *RedisMarker) Mark() (bool, error)

func (*RedisMarker) SetExpiration

func (marker *RedisMarker) SetExpiration(expiration time.Duration) *RedisMarker

func (*RedisMarker) Unmark

func (marker *RedisMarker) Unmark() (bool, error)

type RedisString

type RedisString string

----------------------------------------

Redis 字符串

----------------------------------------

func (RedisString) Bool

func (s RedisString) Bool() bool

func (RedisString) Buffer

func (s RedisString) Buffer() *bytes.Buffer

func (RedisString) Bytes

func (s RedisString) Bytes() []byte

func (RedisString) Float32

func (s RedisString) Float32() float32

func (RedisString) Float64

func (s RedisString) Float64() float64

func (RedisString) Int

func (s RedisString) Int() int

func (RedisString) Int16

func (s RedisString) Int16() int16

func (RedisString) Int32

func (s RedisString) Int32() int32

func (RedisString) Int64

func (s RedisString) Int64() int64

func (RedisString) Int8

func (s RedisString) Int8() int8

func (RedisString) IsEmpty

func (s RedisString) IsEmpty() bool

func (RedisString) JSON

func (s RedisString) JSON(v interface{}) error

func (RedisString) ParseTime

func (s RedisString) ParseTime(layout string) time.Time

func (RedisString) Slice

func (s RedisString) Slice(sep string) []string

func (RedisString) String

func (s RedisString) String() string

func (RedisString) Time

func (s RedisString) Time() time.Time

func (RedisString) TryBool

func (s RedisString) TryBool() (bool, error)

func (RedisString) TryFloat32

func (s RedisString) TryFloat32() (float32, error)

func (RedisString) TryFloat64

func (s RedisString) TryFloat64() (float64, error)

func (RedisString) TryInt

func (s RedisString) TryInt() (int, error)

func (RedisString) TryInt16

func (s RedisString) TryInt16() (int16, error)

func (RedisString) TryInt32

func (s RedisString) TryInt32() (int32, error)

func (RedisString) TryInt64

func (s RedisString) TryInt64() (int64, error)

func (RedisString) TryInt8

func (s RedisString) TryInt8() (int8, error)

func (RedisString) TryParseTime

func (s RedisString) TryParseTime(layout string) (time.Time, error)

func (RedisString) TryTime

func (s RedisString) TryTime() (time.Time, error)

func (RedisString) TryUint

func (s RedisString) TryUint() (uint, error)

func (RedisString) TryUint16

func (s RedisString) TryUint16() (uint16, error)

func (RedisString) TryUint32

func (s RedisString) TryUint32() (uint32, error)

func (RedisString) TryUint64

func (s RedisString) TryUint64() (uint64, error)

func (RedisString) TryUint8

func (s RedisString) TryUint8() (uint8, error)

func (RedisString) Uint

func (s RedisString) Uint() uint

func (RedisString) Uint16

func (s RedisString) Uint16() uint16

func (RedisString) Uint32

func (s RedisString) Uint32() uint32

func (RedisString) Uint64

func (s RedisString) Uint64() uint64

func (RedisString) Uint8

func (s RedisString) Uint8() uint8

Jump to

Keyboard shortcuts

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