redisCache

package
v0.0.0-...-fc17aaa Latest Latest
Warning

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

Go to latest
Published: May 23, 2023 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Writer LockType = 1
	Read   LockType = 2

	DefaultLockTtl   = time.Minute * 10
	DefaultLockValue = "0"
)

Variables

View Source
var (
	ErrCacheMiss = errors.New("cache: key is missing")
)
View Source
var ErrUnknownType = errors.New("未知数据类型")

Functions

func Marshal

func Marshal(value interface{}) ([]byte, error)

func NewLockKey

func NewLockKey(key string) (lockKey string)

NewLockKey 拼接锁key

func StructToRedisHash

func StructToRedisHash(ctx context.Context, cli redis.Cmdable, key string, data interface{}) error

func Unmarshal

func Unmarshal(b []byte, value interface{}) error

Types

type Cache

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

func New

func New(opt *Options) *Cache

func (*Cache) Delete

func (cd *Cache) Delete(ctx context.Context, key string) error

func (*Cache) DeleteFromLocalCache

func (cd *Cache) DeleteFromLocalCache(key string)

func (*Cache) Exists

func (cd *Cache) Exists(ctx context.Context, key string) bool

Exists reports whether value for the given key exists.

func (*Cache) Get

func (cd *Cache) Get(ctx context.Context, key string, value interface{}) error

Get gets the value for the given key.

func (*Cache) GetS

func (cd *Cache) GetS(ctx context.Context, keys []string, skipLocalCache bool) ([][]byte, error)

func (*Cache) GetSkippingLocalCache

func (cd *Cache) GetSkippingLocalCache(
	ctx context.Context, key string, value interface{},
) error

GetSkippingLocalCache gets the value for the given key skipping local cache.

func (*Cache) Marshal

func (cd *Cache) Marshal(value interface{}) ([]byte, error)

func (*Cache) Once

func (cd *Cache) Once(item *Item) error

Once gets the item.Value for the given item.Key from the cache or executes, caches, and returns the results of the given item.Func, making sure that only one execution is in-flight for a given item.Key at a time. If a duplicate comes in, the duplicate caller waits for the original to complete and receives the same results.

func (*Cache) Set

func (cd *Cache) Set(ctx context.Context, item *Item) error

Set caches the item.

func (*Cache) SetS

func (cd *Cache) SetS(ctx context.Context, items []*Item) error

func (*Cache) Unmarshal

func (cd *Cache) Unmarshal(b []byte, value interface{}) error

type DealWith

type DealWith interface {
	DealWithOnce(z redis.Z) error
	DealWithMultiple(z []redis.Z) error
}

type DelayedQueue

type DelayedQueue interface {
	Put(ctx context.Context, key string, z []*redis.Z, ttl time.Duration) error
	DealWithOnce(ctx context.Context, key string, min, max float64, ttl time.Duration) error
	DealWithMultiple(ctx context.Context, key string, min, max float64, ttl time.Duration) error
}

func NewDelayedQueue

func NewDelayedQueue(rd *rd.Client, fun DealWith) DelayedQueue

type Item

type Item struct {
	Ctx context.Context

	Key   string
	Value interface{}

	// TTL is the cache expiration time.
	// Default TTL is 1 hour.
	TTL time.Duration

	// Do returns value to be cached.
	Do func(*Item) (interface{}, error)

	// SetXX only sets the key if it already exists.
	SetXX bool

	// SetNX only sets the key if it does not already exist.
	SetNX bool

	// SkipLocalCache skips local cache as if it is not set.
	SkipLocalCache bool
}

func (*Item) Context

func (item *Item) Context() context.Context

type LocalCache

type LocalCache interface {
	Set(key string, data []byte)
	Get(key string) ([]byte, bool)
	Del(key string)
}

type LockType

type LockType int

type MarshalFunc

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

type Options

type Options struct {
	Redis        redis.Cmdable
	LocalCache   LocalCache
	StatsEnabled bool
	Marshal      MarshalFunc
	Unmarshal    UnmarshalFunc
}

type Pagination

type Pagination struct {
	Min       string
	Max       string
	Limit     int64 `json:"limit"`
	Offset    int64 `json:"offset"`
	TotalRows int64 `json:"total_rows"`
}

type RedisLock

type RedisLock interface {
	// 加锁, 返回加锁是否成功
	TryLock(ctx context.Context, key string, val interface{}, ttl time.Duration) (bool, []byte, error)
	// 解锁
	UnLock(ctx context.Context, key string, val interface{}) error
	// 等待释放锁
	WaitUnLock(ctx context.Context, key string) (err error)
	// 续约
	RenewLock(ctx context.Context, key string, value interface{}, ttl time.Duration) (err error)
	// 加锁, 如果有锁,等待锁释放再添加锁
	WaitLock(ctx context.Context, key string, val interface{}, ttl time.Duration) (bool, error)
}

func NewRedisLock

func NewRedisLock(redis redis.Cmdable) RedisLock

type SortSet

type SortSet interface {
	// 添加元素
	CacheIndexAdd(ctx context.Context, key string, value []*redis.Z) (result int64, err error)
	// 查询,通过分页
	CacheIndexGetByOrder(ctx context.Context, key string, page *Pagination) (result []string, err error)
	// 查询,通过分数分页
	CacheIndexGetByScore(ctx context.Context, key string, page *Pagination) (result []string, err error)
	// 获取有序集合数量
	CacheGetSortSetCount(ctx context.Context, key string) (result int64, err error)
	// 判断元素是否存在有序集合
	CacheExistsSortSet(ctx context.Context, key, value string) (result bool, err error)
	// 删除有序集合元素
	CacheDelMember(ctx context.Context, key, value string) (err error)
}

func NewCacheIndex

func NewCacheIndex(redis redis.Cmdable) SortSet

type TinyLFU

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

func NewTinyLFU

func NewTinyLFU(size int, ttl time.Duration) *TinyLFU

func (*TinyLFU) Del

func (c *TinyLFU) Del(key string)

func (*TinyLFU) Get

func (c *TinyLFU) Get(key string) ([]byte, bool)

func (*TinyLFU) Set

func (c *TinyLFU) Set(key string, b []byte)

func (*TinyLFU) UseRandomizedTTL

func (c *TinyLFU) UseRandomizedTTL(offset time.Duration)

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