cache

package
v2.1.12 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2026 License: Apache-2.0 Imports: 8 Imported by: 139

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeleteContext added in v2.1.5

func DeleteContext(ctx context.Context, cache Cache, key string) error

DeleteContext delete value in cache.

func GetContext added in v2.1.5

func GetContext(ctx context.Context, cache Cache, key string) interface{}

GetContext get value from cache

func IsExistContext added in v2.1.5

func IsExistContext(ctx context.Context, cache Cache, key string) bool

IsExistContext check value exists in cache.

func SetContext added in v2.1.5

func SetContext(ctx context.Context, cache Cache, key string, val interface{}, timeout time.Duration) error

SetContext set value to cache

Types

type Cache

type Cache interface {
	Get(key string) interface{}
	Set(key string, val interface{}, timeout time.Duration) error
	IsExist(key string) bool
	Delete(key string) error
}

Cache interface

type ContextCache added in v2.1.5

type ContextCache interface {
	Cache
	GetContext(ctx context.Context, key string) interface{}
	SetContext(ctx context.Context, key string, val interface{}, timeout time.Duration) error
	IsExistContext(ctx context.Context, key string) bool
	DeleteContext(ctx context.Context, key string) error
}

ContextCache interface

type Memcache

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

Memcache struct contains *memcache.Client

func NewMemcache

func NewMemcache(server ...string) *Memcache

NewMemcache create new memcache

func (*Memcache) Delete

func (mem *Memcache) Delete(key string) error

Delete delete value in memcache.

func (*Memcache) Get

func (mem *Memcache) Get(key string) interface{}

Get return cached value

func (*Memcache) IsExist

func (mem *Memcache) IsExist(key string) bool

IsExist check value exists in memcache.

func (*Memcache) Set

func (mem *Memcache) Set(key string, val interface{}, timeout time.Duration) (err error)

Set cached value with key and expire time.

type Memory

type Memory struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Memory struct contains *memcache.Client

func NewMemory

func NewMemory() *Memory

NewMemory create new memcache

func (*Memory) Delete

func (mem *Memory) Delete(key string) error

Delete delete value in memcache.

func (*Memory) Get

func (mem *Memory) Get(key string) interface{}

Get return cached value

func (*Memory) IsExist

func (mem *Memory) IsExist(key string) bool

IsExist check value exists in memcache.

func (*Memory) Set

func (mem *Memory) Set(key string, val interface{}, timeout time.Duration) (err error)

Set cached value with key and expire time.

type Redis

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

Redis .redis cache

func NewRedis

func NewRedis(ctx context.Context, opts *RedisOpts) *Redis

NewRedis 实例化

func (*Redis) Delete

func (r *Redis) Delete(key string) error

Delete 删除

func (*Redis) DeleteContext added in v2.1.5

func (r *Redis) DeleteContext(ctx context.Context, key string) error

DeleteContext 删除

func (*Redis) Get

func (r *Redis) Get(key string) interface{}

Get 获取一个值

func (*Redis) GetContext added in v2.1.5

func (r *Redis) GetContext(ctx context.Context, key string) interface{}

GetContext 获取一个值

func (*Redis) IsExist

func (r *Redis) IsExist(key string) bool

IsExist 判断key是否存在

func (*Redis) IsExistContext added in v2.1.5

func (r *Redis) IsExistContext(ctx context.Context, key string) bool

IsExistContext 判断key是否存在

func (*Redis) Set

func (r *Redis) Set(key string, val interface{}, timeout time.Duration) error

Set 设置一个值

func (*Redis) SetConn

func (r *Redis) SetConn(conn redis.UniversalClient)

SetConn 设置conn

func (*Redis) SetContext added in v2.1.5

func (r *Redis) SetContext(ctx context.Context, key string, val interface{}, timeout time.Duration) error

SetContext 设置一个值

func (*Redis) SetRedisCtx added in v2.1.3

func (r *Redis) SetRedisCtx(ctx context.Context)

SetRedisCtx 设置redis ctx 参数

type RedisOpts

type RedisOpts struct {
	Host         string `json:"host"            yaml:"host"`
	Username     string `json:"username"        yaml:"username"`
	Password     string `json:"password"        yaml:"password"`
	Database     int    `json:"database"        yaml:"database"`
	MinIdleConns int    `json:"min_idle_conns"  yaml:"min_idle_conns"` // 最小空闲连接数
	PoolSize     int    `json:"pool_size"       yaml:"pool_size"`      // 连接池大小,0 表示使用默认值(即 CPU 核心数 * 10)
	MaxRetries   int    `json:"max_retries"     yaml:"max_retries"`    // 最大重试次数,-1 表示不重试,0 表示使用默认值(即 3 次)
	DialTimeout  int    `json:"dial_timeout"    yaml:"dial_timeout"`   // 连接超时时间(秒),0 表示使用默认值(即 5 秒)
	ReadTimeout  int    `json:"read_timeout"    yaml:"read_timeout"`   // 读取超时时间(秒),-1 表示不超时,0 表示使用默认值(即 3 秒)
	WriteTimeout int    `json:"write_timeout"   yaml:"write_timeout"`  // 写入超时时间(秒),-1 表示不超时,0 表示使用默认值(即 ReadTimeout)
	PoolTimeout  int    `json:"pool_timeout"    yaml:"pool_timeout"`   // 连接池获取连接超时时间(秒),0 表示使用默认值(即 ReadTimeout + 1 秒)
	IdleTimeout  int    `json:"idle_timeout"    yaml:"idle_timeout"`   // 空闲连接超时时间(秒),-1 表示禁用空闲连接超时检查,0 表示使用默认值(即 5 分钟)
	UseTLS       bool   `json:"use_tls"         yaml:"use_tls"`        // 是否使用 TLS

	// Deprecated: 应使用 MinIdleConns 代替
	MaxIdle int `json:"max_idle" yaml:"max_idle"`
	// Deprecated: 应使用 PoolSize 代替
	MaxActive int `json:"max_active" yaml:"max_active"`
}

RedisOpts redis 连接属性

Jump to

Keyboard shortcuts

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