cache

package
v1.1.40 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package cache 提供缓冲相关的服务

Index

Constants

View Source
const (
	ReplyOK = "OK"
	DEL     = "DEL"
	EXISTS  = "EXISTS"
	EXPIRE  = "EXPIRE"
	GET     = "GET"
	HDEL    = "HDEL"
	HGETALL = "HGETALL"
	HINCRBY = "HINCRBY"
	INCR    = "INCR"
	INCRBY  = "INCRBY"
	SET     = "SET"
	SETEX   = "SETEX"
	ZADD    = "ZADD"
	ZCARD   = "ZCARD"
	ZRANGE  = "ZRANGE"
	ZREM    = "ZREM"
)

redis的命令及响应

View Source
const (
	DefaultConnectTimout = 5 * 1000
	DefaultReadTimeout   = 5 * 1000
	DefaultWriteTimeout  = 5 * 1000
	DefaultMaxActive     = 100
	DefaultMaxIdle       = 2
	DefaultIdleTimeout   = 60 * 1000
)

Redis连接池的默认参数

Variables

This section is empty.

Functions

func CheckNilErr

func CheckNilErr(err error) (bool, error)

CheckNilErr check the err:

if err == nil, return true,nil; if the err is ErrNIl return false,nil;,otherwise return false ,err

func MsgPackDecodeBytes

func MsgPackDecodeBytes(bytes []byte, dest interface{}) (err error)

MsgPackDecodeBytes decode bytes to dest use msgpack

func MsgPackEncodeBytes

func MsgPackEncodeBytes(data interface{}) (bytes []byte, err error)

MsgPackEncodeBytes encode data to bytes use msgpack

func TryLock

func TryLock(lockKey string, lockSencods int, paramConf *ParamConf, redisClient *RedisClient) (bool, error)

TryLock try to lock lockKey in lockSencods

func UnLock

func UnLock(lockKey string, paramConf *ParamConf, redisClient *RedisClient) error

UnLock unlock lockey

Types

type Param

type Param interface {
	//Group cache group id
	Group() string
	//Key cache key
	Key() string
	//Expire second time
	Expire() int
}

Param is the cache param

type ParamConf

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

ParamConf is the cache param conf with cache group,key prefix and expire

func NewParamConf

func NewParamConf(group, keyPrefix string, expire int) *ParamConf

NewParamConf create ParamConf

func (*ParamConf) Expire

func (p *ParamConf) Expire() int

Expire return expire second

func (*ParamConf) Group

func (p *ParamConf) Group() string

Group return cache group

func (*ParamConf) KeyPrefix

func (p *ParamConf) KeyPrefix() string

KeyPrefix return key prefix

func (*ParamConf) NewParamKey

func (p *ParamConf) NewParamKey(key string) *ParamKey

NewParamKey create new ParamKey with key

func (*ParamConf) NewParamKeyWithoutPrefix

func (p *ParamConf) NewParamKeyWithoutPrefix(key string) *ParamKey

NewParamKeyWithoutPrefix create new ParamKey without p.keyPrefix

func (*ParamConf) NewWithExpire

func (p *ParamConf) NewWithExpire(expire int) *ParamConf

NewWithExpire create new ParamConf with new expire parameter

func (*ParamConf) NewWithKeyPrefix

func (p *ParamConf) NewWithKeyPrefix(keyPrefix string) *ParamConf

NewWithKeyPrefix append keyPrefix to exist ParamConf,return new ParamConf

type ParamKey

type ParamKey struct {
	ParamConf
	// contains filtered or unexported fields
}

ParamKey is the cache param with key

func (*ParamKey) Key

func (p *ParamKey) Key() string

Key implements Param.Key()

func (*ParamKey) NewWithExpire

func (p *ParamKey) NewWithExpire(expire int) *ParamKey

NewWithExpire new key with expire

type Pipeline

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

Pipeline the command and results

func NewPipeline

func NewPipeline(r *RedisClient) (*Pipeline, error)

NewPipeline new pipeline from RedisClient

func (*Pipeline) Close

func (p *Pipeline) Close()

Close all redis connection

func (*Pipeline) Flush

func (p *Pipeline) Flush() error

Flush all command in the output buffers.

func (*Pipeline) Receive

func (p *Pipeline) Receive() (replies []*PipelineReply, err error)

Receive all reply from reids server

func (*Pipeline) Send

func (p *Pipeline) Send(param Param, command string, args ...interface{}) error

Send write the command to the redis conn out buffer.

type PipelineReply

type PipelineReply struct {
	Reply interface{}
	Err   error
}

PipelineReply reply from pipeline

type RedisClient

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

RedisClient redis client

func NewRedisClient

func NewRedisClient(groups map[string][]*RedisServer) *RedisClient

NewRedisClient create new Redis

func NewRedisClientWithConf

func NewRedisClientWithConf(conf *RedisConf) *RedisClient

NewRedisClientWithConf create redis from conf

func (*RedisClient) Del

func (p *RedisClient) Del(param Param) (deleted bool, err error)

Del del the param.Key()

func (*RedisClient) Do

func (p *RedisClient) Do(param Param, fn func(conn redis.Conn) (interface{}, error)) (reply interface{}, err error)

Do exec redis commands with param and key

func (*RedisClient) Eval

func (p *RedisClient) Eval(param Param, script *redis.Script, args ...interface{}) (reply interface{}, err error)

Eval lua script for param.Key() with args

func (*RedisClient) Exists

func (p *RedisClient) Exists(param Param) (exists bool, err error)

Exists check the param.Key() exist

func (*RedisClient) Expire

func (p *RedisClient) Expire(param Param) (expired bool, err error)

Expire set timeout on key `param.Key()`,the timeout is `param.Expire()` second

func (*RedisClient) Get

func (p *RedisClient) Get(param Param) (reply interface{}, ok bool, err error)

Get value from redis with param and key,if the param.Expire >0 then will EXPIRE the key

func (*RedisClient) GetConn

func (p *RedisClient) GetConn(param Param) (conn redis.Conn, err error)

GetConn acquire redis.Conn in param.Group. If has mutiple servers in redis group,choose server with hash code which generated by fnv(key) % len(servers)

func (*RedisClient) GetFloat64

func (p *RedisClient) GetFloat64(param Param) (reply float64, ok bool, err error)

GetFloat64 get int64 value from redis with param

func (*RedisClient) GetGroupServers

func (p *RedisClient) GetGroupServers(group string) ([]*RedisServer, error)

GetGroupServers query the servers for group

func (*RedisClient) GetInt

func (p *RedisClient) GetInt(param Param) (reply int, ok bool, err error)

GetInt get int value from redis with param

func (*RedisClient) GetInt64

func (p *RedisClient) GetInt64(param Param) (reply int64, ok bool, err error)

GetInt64 get int64 value from redis with param

func (*RedisClient) GetObject

func (p *RedisClient) GetObject(param Param, dest interface{}) (ok bool, err error)

GetObject get bytes whose key is param.Key(),then decode bytes to dest

func (*RedisClient) GetObjects

func (p *RedisClient) GetObjects(paramConf *ParamConf, keys []string, dest interface{}, getByKey func(key string, index int) (interface{}, error)) error

GetObjects batch get struct object,use MsgPackDecodeBytes to decode bytes and append to dest

func (*RedisClient) GetString

func (p *RedisClient) GetString(param Param) (reply string, ok bool, err error)

GetString get int64 value from redis with param

func (*RedisClient) Incr

func (p *RedisClient) Incr(param Param) (val int64, err error)

Incr value from redis with param and key

func (*RedisClient) IncrBy

func (p *RedisClient) IncrBy(param Param, increment int64) (val int64, err error)

IncrBy value from redis with param and key,if the param.Expire >0 then will EXPIRE the key

func (*RedisClient) Set

func (p *RedisClient) Set(param Param, data interface{}) error

Set param.Key() with value `data`,if param.Exipre >0,then set key with expire second

func (*RedisClient) SetObject

func (p *RedisClient) SetObject(param Param, data interface{}) error

SetObject set param.Key() with value `data`,if param.Exipre >0,then set key with expire second

func (*RedisClient) TTL

func (p *RedisClient) TTL(param Param) (val int, err error)

TTL get ttl for key

type RedisConf

type RedisConf struct {
	Servers   []*RedisServer            `yaml:"servers"`      //实例列表
	Groups    map[string][]string       `yaml:"groups"`       //Redis组定义,key为组ID;value为Server的id列表
	Pool      *RedisPoolConf            `yaml:"pool"`         //默认的链接池配置
	GroupPool map[string]*RedisPoolConf `yaml:"groups_pools"` //Redis组的连接池配置
	// contains filtered or unexported fields
}

RedisConf redis config

func (*RedisConf) Parse

func (p *RedisConf) Parse() error

Parse implements Configurer interface

func (*RedisConf) RedisConfig

func (p *RedisConf) RedisConfig() *RedisConf

RedisConfig implements RedisConfigurer

type RedisConfigurer

type RedisConfigurer interface {
	c.Configurer
	RedisConfig() *RedisConf
}

RedisConfigurer Redis配置器

type RedisPoolConf

type RedisPoolConf struct {
	ConnectTimeout int `yaml:"connect_timeout"` //连接超时时间,单位毫秒秒
	ReadTimeout    int `yaml:"read_timeout"`    //读取超时,单位毫秒
	WriteTimeout   int `yaml:"write_timeout"`   //写取超时,单位毫秒
	MaxIdle        int `yaml:"max_idle"`        //最大空闲连接
	MaxActive      int `yaml:"max_active"`      //最大活跃连接,0表示不限制
	IdleTimeout    int `yaml:"idel_teimout"`    //空闲连接的超时时间,单位毫秒
}

RedisPoolConf Redis连接池配置

type RedisServer

type RedisServer struct {
	ID   string `yaml:"id"`   //Redis实例的id
	Host string `yaml:"host"` //Redis主机地址
	Port int    `yaml:"port"` //Redis的端口
	Auth string `yaml:"auth"` //Redis认证密码
	// contains filtered or unexported fields
}

RedisServer Redis实例的配置

func (*RedisServer) GetConn

func (p *RedisServer) GetConn() (redis.Conn, error)

GetConn acquire redis conn

Directories

Path Synopsis
Package counter supply counter service
Package counter supply counter service
Package list has some list cache implemented by redis
Package list has some list cache implemented by redis

Jump to

Keyboard shortcuts

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