redisw

package
v1.7.5 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2024 License: LGPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	REDIS_DEFAULT_IDLE_CONN    = 3   // 最大空闲连接数
	REDIS_DEFAULT_IDLE_TIMEOUT = 240 // 最大空闲时长,单位:秒
	REDIS_DEFAULT_EXEC_RETRY   = 3   // 重试次数
	REDIS_DEFAULT_READ_TIMEOUT = 7   // 命令最大执行时长,单位:秒
)
View Source
const (
	SESS_FOR_EVER   = -1 // 无限
	SESS_NOT_EXISTS = -2 // 不存在
	SESS_ERR_REDIS  = -3 // redis错误

	SESS_PREFIX     = "sess" // 会话缓存前缀
	SESS_LIST_SEP   = ";"    // 角色名之间的分隔符
	SESS_TOKEN_KEY  = "_token_"
	SESS_ONLINE_KEY = "onlines" // 在线用户

	MAX_TIMEOUT = 86400 * 30 // 接近无限时间
)

Variables

View Source
var (
	StrToList      = utils.StrToList // 将字符串数组转为一般数组
	KeysEmptyError = fmt.Errorf("the param which named 'keys' must not empty !")
)

Functions

func ExecMapBytes

func ExecMapBytes(exec ExecMulti, keys ...string) (map[string][]byte, error)

func ExecMapFloat

func ExecMapFloat(exec ExecMulti, keys ...string) (map[string]float64, error)

func ExecMapInt

func ExecMapInt(exec ExecMulti, keys ...string) (map[string]int, error)

func ExecMapInt64

func ExecMapInt64(exec ExecMulti, keys ...string) (map[string]int64, error)

func ExecMapString

func ExecMapString(exec ExecMulti, keys ...string) (map[string]string, error)

func GetJsonContent

func GetJsonContent(data string) string

对list、dict或string的json,去掉两边括号或引号取中间部分 用于快速拼接json,免除判断类型和空值,如 fmt.Sprintf("{%s}", GetJsonContent(json))

func Map2Args

func Map2Args(data Map, asJson bool) []any

func NewRedisConn

func NewRedisConn(cfg dialect.ConnConfig) (redis.Conn, error)

NewRedisConn 根据配置连接redis

cfg = dialect.ConnConfig{
	DSN: (dialect.Redis{
		Host:     "127.0.0.1",
		Database: 3,
	}).BuildDSN(),
	Password: "secrect",
}

func NewRedisConnDb

func NewRedisConnDb(cfg dialect.ConnConfig, db int) (redis.Conn, error)

NewRedisConnDb 建立Redis实际连接

func ReplyBool

func ReplyBool(reply any, err error) (bool, error)

ReplyBool redigo没有将应答中的OK转为bool值(2020-01-16)

func SessListJoin

func SessListJoin(data []string) string

func SessListSplit

func SessListSplit(data string) []string

Types

type CacheData

type CacheData interface {
	GetCacheId() string // 在同一个db中唯一id
}

type ExecMulti

type ExecMulti func(keys ...string) (any, error)

type Map

type Map = map[string]any

func ExecMap

func ExecMap(exec ExecMulti, keys ...string) (data Map, err error)

func NewMap

func NewMap() Map

type RedisContainer

type RedisContainer interface {
	Get() redis.Conn
	Close() error
}

RedisContainer Redis容器,包括 *redis.Pool 和 *redisx.ConnMux 两个实现

type RedisHash

type RedisHash struct {
	*RedisWrapper
	// contains filtered or unexported fields
}

func NewRedisHash

func NewRedisHash(r *RedisWrapper, name string, timeout int) *RedisHash

func (*RedisHash) Delete

func (rh *RedisHash) Delete(keys ...string) (int, error)

func (*RedisHash) DeleteAll

func (rh *RedisHash) DeleteAll() (bool, error)

func (*RedisHash) Exec

func (rh *RedisHash) Exec(cmd string, args ...any) (any, error)

func (*RedisHash) Exists

func (rh *RedisHash) Exists(key string) (bool, error)

func (*RedisHash) Expire

func (rh *RedisHash) Expire(timeout int) (bool, error)

func (*RedisHash) GetAll

func (rh *RedisHash) GetAll() (any, error)

func (*RedisHash) GetAllString

func (rh *RedisHash) GetAllString() (map[string]string, error)

func (*RedisHash) GetFloat

func (rh *RedisHash) GetFloat(key string) (float64, error)

func (*RedisHash) GetInt

func (rh *RedisHash) GetInt(key string) (int, error)

func (*RedisHash) GetInt64

func (rh *RedisHash) GetInt64(key string) (int64, error)

func (*RedisHash) GetKeys

func (rh *RedisHash) GetKeys() []string

func (*RedisHash) GetMulti

func (rh *RedisHash) GetMulti(keys ...string) (any, error)

func (*RedisHash) GetSize

func (rh *RedisHash) GetSize() int

GetSize 获取哈希表元素数量

func (*RedisHash) GetString

func (rh *RedisHash) GetString(key string) (string, error)

func (*RedisHash) GetTimeout

func (rh *RedisHash) GetTimeout(predict bool) int

GetTimeout 获取剩余时间 -1=无限 -2=不存在 -3=出错

func (*RedisHash) GetVal

func (rh *RedisHash) GetVal(key string) (any, error)

func (*RedisHash) IncrInt

func (rh *RedisHash) IncrInt(key string, offset int) (int, error)

func (*RedisHash) IncrInt64

func (rh *RedisHash) IncrInt64(key string, offset int64) (int64, error)

func (*RedisHash) LoadForeignJson

func (rh *RedisHash) LoadForeignJson(data Map) (err error)

只能得到CacheData数据的Map,基本类型需要自己加载

func (*RedisHash) LoadForeignString

func (rh *RedisHash) LoadForeignString(keys ...string) (result map[string]string, err error)

func (*RedisHash) LoadMap

func (rh *RedisHash) LoadMap(keys ...string) (data Map, err error)

func (*RedisHash) LoadMapInt

func (rh *RedisHash) LoadMapInt(keys ...string) (map[string]int, error)

func (*RedisHash) LoadMapString

func (rh *RedisHash) LoadMapString(keys ...string) (map[string]string, error)

func (*RedisHash) LoadSummary

func (rh *RedisHash) LoadSummary(data Map) (map[string]string, error)

func (*RedisHash) OrigDelete added in v1.6.0

func (rh *RedisHash) OrigDelete(keys ...any) (int, error)

func (*RedisHash) OrigMulti added in v1.6.0

func (rh *RedisHash) OrigMulti(cmd string, keys ...any) (any, error)

func (*RedisHash) SaveForeignData

func (rh *RedisHash) SaveForeignData(data Map) (ok bool, err error)

基本类型保存于自身,CacheData数据关联保存为Json

func (*RedisHash) SaveMap

func (rh *RedisHash) SaveMap(data Map, asJson bool) (bool, error)

func (*RedisHash) SetNX

func (rh *RedisHash) SetNX(key string, value any) (int, error)

func (*RedisHash) SetVal

func (rh *RedisHash) SetVal(key string, value any) (int, error)

type RedisWrapper

type RedisWrapper struct {
	MaxIdleConn int // 最大空闲连接数
	MaxIdleTime int // 最大空闲时长
	RetryTimes  int // 重试次数
	MaxReadTime int // 命令最大执行时长(不算连接部分)
	RedisContainer
}

RedisWrapper Redis包装器,给容器加上超时等参数

func NewRedisConnMux

func NewRedisConnMux(conn redis.Conn, err error) *RedisWrapper

NewRedisConnMux 建立Redis连接复用

func NewRedisPool

func NewRedisPool(cfg dialect.ConnConfig, maxIdle int) *RedisWrapper

NewRedisPool 建立Redis连接池

func NewRedisWrapper

func NewRedisWrapper() *RedisWrapper

NewRedisWrapper 包装一个Redis空容器

func (*RedisWrapper) Delete

func (r *RedisWrapper) Delete(keys ...string) (int, error)

func (*RedisWrapper) DeleteAll

func (r *RedisWrapper) DeleteAll() (bool, error)

DeleteAll 清空当前db

func (*RedisWrapper) Exec

func (r *RedisWrapper) Exec(cmd string, args ...any) (any, error)

Exec 执行命令,将会重试几次

func (*RedisWrapper) Exists

func (r *RedisWrapper) Exists(key string) (bool, error)

func (*RedisWrapper) Expire

func (r *RedisWrapper) Expire(key string, timeout int) (bool, error)

Expire 设置键的过期时间

func (*RedisWrapper) Find

func (r *RedisWrapper) Find(wildcard string) ([]string, error)

Find 模糊查找

func (*RedisWrapper) GetBytes

func (r *RedisWrapper) GetBytes(key string) ([]byte, error)

func (*RedisWrapper) GetFloat

func (r *RedisWrapper) GetFloat(key string) (float64, error)

func (*RedisWrapper) GetInt

func (r *RedisWrapper) GetInt(key string) (int, error)

func (*RedisWrapper) GetInt64

func (r *RedisWrapper) GetInt64(key string) (int64, error)

func (*RedisWrapper) GetMaxReadDuration

func (r *RedisWrapper) GetMaxReadDuration() time.Duration

GetMaxReadDuration 单命令最大执行时长(不算连接部分)

func (*RedisWrapper) GetMulti

func (r *RedisWrapper) GetMulti(keys ...string) (any, error)

func (*RedisWrapper) GetSize

func (r *RedisWrapper) GetSize() int

GetSize 当前db缓存键的数量

func (*RedisWrapper) GetStrLen

func (r *RedisWrapper) GetStrLen(key string) (int, error)

func (*RedisWrapper) GetString

func (r *RedisWrapper) GetString(key string) (string, error)

func (*RedisWrapper) GetTimeout

func (r *RedisWrapper) GetTimeout(key string) int

GetTimeout -1=无限 -2=不存在 -3=出错

func (*RedisWrapper) IncrFloat

func (r *RedisWrapper) IncrFloat(key string, offset float64) (float64, error)

计数增加

func (*RedisWrapper) IncrInt

func (r *RedisWrapper) IncrInt(key string, offset int) (int, error)

计数增加

func (*RedisWrapper) IncrInt64

func (r *RedisWrapper) IncrInt64(key string, offset int64) (int64, error)

计数增加

func (*RedisWrapper) LoadJson

func (r *RedisWrapper) LoadJson(key string, obj any) error

func (*RedisWrapper) LoadMap

func (r *RedisWrapper) LoadMap(keys ...string) (data Map, err error)

func (*RedisWrapper) LoadMapInt

func (r *RedisWrapper) LoadMapInt(keys ...string) (map[string]int, error)

func (*RedisWrapper) LoadMapString

func (r *RedisWrapper) LoadMapString(keys ...string) (map[string]string, error)

func (*RedisWrapper) OrigDelete added in v1.6.0

func (r *RedisWrapper) OrigDelete(keys ...any) (int, error)

func (*RedisWrapper) OrigMulti added in v1.6.0

func (r *RedisWrapper) OrigMulti(cmd string, keys ...any) (any, error)

func (*RedisWrapper) Rename

func (r *RedisWrapper) Rename(old, dst string) (bool, error)

func (*RedisWrapper) SaveJson

func (r *RedisWrapper) SaveJson(key string, obj any, timeout int) (bool, error)

func (*RedisWrapper) SaveMap

func (r *RedisWrapper) SaveMap(data Map, asJson bool) (bool, error)

func (*RedisWrapper) SetNX

func (r *RedisWrapper) SetNX(key string, value any) (int, error)

func (*RedisWrapper) SetPart

func (r *RedisWrapper) SetPart(key string, value any, offset int) (int, error)

func (*RedisWrapper) SetVal

func (r *RedisWrapper) SetVal(key string, value any, timeout int) (bool, error)

type Session

type Session struct {
	*RedisHash
	// contains filtered or unexported fields
}

会话

func NewSession

func NewSession(reg *SessionRegistry, key string, timeout int) *Session

创建会话

func (*Session) AddFlash

func (sess *Session) AddFlash(messages ...string) (int, error)

AddFlash 添加临时消息

func (*Session) BindRoles

func (sess *Session) BindRoles(uid string, roles []string, kick bool) (string, error)

BindRoles 绑定用户角色,返回旧的sid

func (*Session) GetFlashes

func (sess *Session) GetFlashes(n int) ([]string, error)

GetFlashes 数量n为最大取出多少条消息,-1表示所有消息

func (*Session) GetKey

func (sess *Session) GetKey() string

func (*Session) WrapExec

func (sess *Session) WrapExec(cmd string, args ...any) (any, error)

WrapExec 执行普通命令

type SessionRegistry

type SessionRegistry struct {
	Onlines *RedisHash
	*RedisWrapper
	// contains filtered or unexported fields
}

func NewRegistry

func NewRegistry(w *RedisWrapper) *SessionRegistry

func (*SessionRegistry) DelSession

func (sr *SessionRegistry) DelSession(token string) bool

func (SessionRegistry) GetKey

func (sr SessionRegistry) GetKey(token string) string

func (*SessionRegistry) GetSession

func (sr *SessionRegistry) GetSession(token string, timeout int) *Session

Jump to

Keyboard shortcuts

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