redisx

package
v0.0.0-...-418145e Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	//redis过期时间(30分钟)
	Redis_30M_TTL time.Duration = time.Duration(30) * time.Minute
	//redis过期时间(24小时)
	Redis_24Hour_TTL time.Duration = time.Duration(24) * time.Hour
	//1个月的有效期
	Redis_Expiration_Month_TTL time.Duration = time.Duration(30) * time.Hour * 24
	//无过期时间
	Redis_NoExpiration_TTL time.Duration = time.Duration(-1)
)
View Source
const (
	RedisRandLen = 16
)

Variables

View Source
var (
	ErrKeyNotExist = errors.New("redis: key is missing")
	ErrValueIsNil  = errors.New("redis: value is nil")
)

Functions

func RedisValueMapToMap

func RedisValueMapToMap[V any](vMap RedisValueMap, filterFn func(*V) bool) (map[string]*V, error)

func RedisValueMapToSlice

func RedisValueMapToSlice[V any](vMap RedisValueMap, filterFn func(*V) bool) ([]*V, error)

将map[string]IRedis转换为一个对象数组 filterFn: 转换时是否执行筛选,如果为nil,则不执行任何筛选

Types

type IRedisHashService

type IRedisHashService interface {
	HashGet(key string, field string, opts ...RedisValueOption) IRedisValue
	HashGetAll(key string, opts ...RedisValueOption) (RedisValueMap, error)

	HashSet(key string, values map[string]interface{}, opts ...RedisValueOption) error
}

func NewRedisHashService

func NewRedisHashService(options *RedisOptions) IRedisHashService

type IRedisKeyService

type IRedisKeyService interface {
	GetRedisClient() *redis.Client

	//搜索符合pattern的key列表
	Keys(keyPattern string, opts ...RedisValueOption) ([]string, error)
	ExistKey(key string, opts ...RedisValueOption) (bool, error)
	DeleteKey(key string, opts ...RedisValueOption) error
	//删除指定前缀的所有key
	DeleteKeys(pattern string, opts ...RedisValueOption) error
	//设置key的过期时间
	KeyExpire(key string, duration time.Duration, opts ...RedisValueOption) error
	//获取key的过期时间
	KeyTimeToLive(key string, opts ...RedisValueOption) *time.Duration
}

对redis中的key进行操作的接口

type IRedisPipelineService

type IRedisPipelineService interface {
}

type IRedisService

type IRedisService interface {
	IRedisKeyService
	IRedisStringService
	IRedisHashService
}

访问redis

func NewRedisService

func NewRedisService(options *RedisOptions) IRedisService

new一个IRedisService

type IRedisStringService

type IRedisStringService interface {
	//获取key值
	StringGet(key string, opts ...RedisValueOption) IRedisValue
	//获取多个key值
	StringMGet(opts []RedisValueOption, keys ...string) (map[string]IRedisValue, error)
	//设置值
	StringSet(key string, value interface{}, opts ...RedisValueOption) error

	//自增id
	KeyIncr(key string, opts ...RedisValueOption) (int64, error)
	KeyIncrBy(key string, value int64, opts ...RedisValueOption) (int64, error)
	GetIncr(key string, opts ...RedisValueOption) (int64, error)
	//自减id
	KeyDecr(key string, opts ...RedisValueOption) (int64, error)
	KeyDecrBy(key string, decrement int64, opts ...RedisValueOption) (int64, error)
	GetDecr(key string, opts ...RedisValueOption) (int64, error)
}

func NewRedisStringService

func NewRedisStringService(options *RedisOptions) IRedisStringService

type IRedisValue

type IRedisValue interface {
	//获取值是否存在
	Exist() bool
	Bytes() []byte
	Err() error

	//转换为目标值
	ToValue(val interface{}) error

	ValToString() string
	ValToInt() (int, error)
	ValToInt32() (int32, error)
	ValToInt64() (int64, error)
	ValToBool() (bool, error)
	ValToTime() (time.Time, error)
}

redis值

type MarshalFunc

type MarshalFunc func(interface{}) ([]byte, error)

type RedisHashService

type RedisHashService struct {
	*RedisKeyService
}

func (*RedisHashService) HashGet

func (s *RedisHashService) HashGet(key string, field string, opts ...RedisValueOption) IRedisValue

get field from hash

func (*RedisHashService) HashGetAll

func (s *RedisHashService) HashGetAll(key string, opts ...RedisValueOption) (RedisValueMap, error)

get all value from hash

func (*RedisHashService) HashSet

func (s *RedisHashService) HashSet(key string, values map[string]interface{}, opts ...RedisValueOption) error

type RedisKeyService

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

默认的IRedisKeyService实现

func NewRedisKeyService

func NewRedisKeyService(options *RedisOptions) *RedisKeyService

func (*RedisKeyService) DeleteKey

func (s *RedisKeyService) DeleteKey(key string, opts ...RedisValueOption) error

func (*RedisKeyService) DeleteKeys

func (s *RedisKeyService) DeleteKeys(pattern string, opts ...RedisValueOption) error

删除指定前缀的所有key

func (*RedisKeyService) ExistKey

func (s *RedisKeyService) ExistKey(key string, opts ...RedisValueOption) (bool, error)

func (*RedisKeyService) GetRedisClient

func (s *RedisKeyService) GetRedisClient() *redis.Client

func (*RedisKeyService) KeyExpire

func (s *RedisKeyService) KeyExpire(key string, duration time.Duration, opts ...RedisValueOption) error

设置key的过期时间

func (*RedisKeyService) KeyTimeToLive

func (s *RedisKeyService) KeyTimeToLive(key string, opts ...RedisValueOption) *time.Duration

获取key的过期时间

func (*RedisKeyService) Keys

func (s *RedisKeyService) Keys(keyPattern string, opts ...RedisValueOption) ([]string, error)

搜索符合pattern的key列表

type RedisLock

type RedisLock struct {
	Store   *redis.Client
	Seconds uint32
	Key     string
	Value   string
}

A RedisLock is a redis lock.

func NewRedisLock

func NewRedisLock(store *redis.Client, key string, value string, expire uint32) *RedisLock

NewRedisLock returns a RedisLock.

func (*RedisLock) RedisAcquire

func (rl *RedisLock) RedisAcquire() (bool, error)

RedisAcquire Lua script方式加锁

func (*RedisLock) RedisRelease

func (rl *RedisLock) RedisRelease() (int64, error)

RedisRelease releases the lock. @ return @ int64: Released number @ 0 - exist key but given a wrong token @ -1 - key is not exist @ -2 - other errors

func (*RedisLock) SetExpire

func (rl *RedisLock) SetExpire(seconds int)

SetExpire sets the expiration.

func (*RedisLock) ToString

func (rl *RedisLock) ToString() string

type RedisOptions

type RedisOptions struct {
	KeyPrefix string
	//默认的有效期时间,如果不设,则表示没有有效期
	DefaultTTL *time.Duration

	Unmarshal UnmarshalFunc
	Marshal   MarshalFunc
	// contains filtered or unexported fields
}

模块参数配置

func NewRedisOptions

func NewRedisOptions(client *redis.Client) *RedisOptions

创建默认的配置项

type RedisPipelineService

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

默认的IRedisPipelineService实现

func NewRedisPipelineService

func NewRedisPipelineService(options *RedisOptions) *RedisPipelineService

func (*RedisPipelineService) PipelineSetData

func (s *RedisPipelineService) PipelineSetData(list map[string]interface{}, opts ...RedisValueOption) error

type RedisStringService

type RedisStringService struct {
	*RedisKeyService
}

func (*RedisStringService) GetDecr

func (s *RedisStringService) GetDecr(key string, opts ...RedisValueOption) (int64, error)

func (*RedisStringService) GetIncr

func (s *RedisStringService) GetIncr(key string, opts ...RedisValueOption) (int64, error)

func (*RedisStringService) KeyDecr

func (s *RedisStringService) KeyDecr(key string, opts ...RedisValueOption) (int64, error)

func (*RedisStringService) KeyDecrBy

func (s *RedisStringService) KeyDecrBy(key string, decrement int64, opts ...RedisValueOption) (int64, error)

func (*RedisStringService) KeyIncr

func (s *RedisStringService) KeyIncr(key string, opts ...RedisValueOption) (int64, error)

func (*RedisStringService) KeyIncrBy

func (s *RedisStringService) KeyIncrBy(key string, value int64, opts ...RedisValueOption) (int64, error)

func (*RedisStringService) StringGet

func (s *RedisStringService) StringGet(key string, opts ...RedisValueOption) IRedisValue

func (*RedisStringService) StringMGet

func (s *RedisStringService) StringMGet(opts []RedisValueOption, keys ...string) (map[string]IRedisValue, error)

获取多个key值

func (*RedisStringService) StringSet

func (s *RedisStringService) StringSet(key string, value interface{}, opts ...RedisValueOption) error

type RedisValueMap

type RedisValueMap map[string]IRedisValue

type RedisValueOption

type RedisValueOption func(*RedisValueOptions)

func SuppressedLoadIfEmpty

func SuppressedLoadIfEmpty() RedisValueOption

不自动load

func WithContext

func WithContext(ctx context.Context) RedisValueOption

func WithExpiredTime

func WithExpiredTime(expiredTime time.Time) RedisValueOption

func WithKeyPrefix

func WithKeyPrefix(keyPrefix string) RedisValueOption

指定key前缀

func WithMarshalFunc

func WithMarshalFunc(marshal MarshalFunc) RedisValueOption

func WithTTL

func WithTTL(ttl time.Duration) RedisValueOption

值带上ttl

func WithUnmarshalFunc

func WithUnmarshalFunc(unmarshal UnmarshalFunc) RedisValueOption

func WithoutPrefixKey

func WithoutPrefixKey() RedisValueOption

key不带上prefix

type RedisValueOptions

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

type UnmarshalFunc

type UnmarshalFunc func([]byte, interface{}) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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