cache

package
v0.0.0-...-7bfb8b4 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2025 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRecordNotFound  = errors.New("缓存记录不存在")
	ErrBadExpireTime   = errors.New("分布式缓存的过期时间不得早于本地缓存")
	ErrDeleteKeyFailed = errors.New("key删除失败")
	ErrBadMultiCache   = errors.New("分布式锁暂不可用")
	ErrDirtyData       = errors.New("读入的数据为脏数据")
)
View Source
var (
	ErrTurnConsumerFailed = errors.New("更换消费者失败")
	ErrExtractFailed      = errors.New("无法获取version和key信息")
)

Functions

func WrapMessageQueueExtractor

func WrapMessageQueueExtractor(c CacheExtracter) []byte

Types

type CacheExtracter

type CacheExtracter interface {
	GetVersion() int64
	GetKey() string
}

type DistributedCache

type DistributedCache struct {
	*redis.ClusterClient
}

func MustNewDistributedCache

func MustNewDistributedCache(addrs []string, conf *redis.ClusterOptions) *DistributedCache

type LocalCache

type LocalCache struct {
	*bigcache.BigCache
	// contains filtered or unexported fields
}

func MustNewLocalCache

func MustNewLocalCache(conf *bigcache.Config) *LocalCache

type MultiCache

type MultiCache struct {
	Ctx           context.Context
	EnableTracing bool
	//该Expire只能为distributedCache设置,localCache一旦设置之后无法改变
	ExpireTime time.Duration

	//使用版本控制将带来更高的一致性,但是需要Set系列的函数传入的data是可以得到version字段的
	//暂时只支持json
	UseVersionControll bool
	RocketmqConsumer   rocketmq.PushConsumer
	// contains filtered or unexported fields
}

func MustNewMultiCache

func MustNewMultiCache(ctx context.Context, dc *DistributedCache, lc *LocalCache, opts ...OptionFunc) *MultiCache

默认会将使用的分布式缓存作为分布式锁

func (*MultiCache) Delete

func (c *MultiCache) Delete(ctx context.Context, keys ...string) error

本包不建议在希望最终一致性的情况下直接使用该Delete函数

func (*MultiCache) DeleteWithMutex

func (c *MultiCache) DeleteWithMutex(ctx context.Context, key string) error

func (*MultiCache) Get

func (c *MultiCache) Get(ctx context.Context, key string) ([]byte, error)

func (*MultiCache) GetWithMutex

func (c *MultiCache) GetWithMutex(ctx context.Context, key string) ([]byte, error)

func (*MultiCache) RegisterRocketmq

func (c *MultiCache) RegisterRocketmq(pc rocketmq.PushConsumer, topic string) error

func (*MultiCache) Set

func (c *MultiCache) Set(ctx context.Context, key string, value []byte) error

本包不建议在希望最终一致性的情况下直接使用该Set函数

func (*MultiCache) SetWithMutex

func (c *MultiCache) SetWithMutex(ctx context.Context, key string, value []byte) error

func (*MultiCache) SetWithVersionMutex

func (c *MultiCache) SetWithVersionMutex(ctx context.Context, key string, value []byte, version int64) error

结合版本控制和分布式锁机制,注意value参数中应当也要有version字段,多出来的version参数则是避免再次unmarshal得到

func (*MultiCache) Stats

func (c *MultiCache) Stats() bigcache.Stats

type OptionFunc

type OptionFunc func(*MultiCache)

func WithExpireTime

func WithExpireTime(t time.Duration) OptionFunc

func WithRedlock

func WithRedlock(r *redlock.RedLock) OptionFunc

Jump to

Keyboard shortcuts

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