cache

package
v0.0.0-...-dc62a57 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2025 License: Apache-2.0 Imports: 17 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	VersionStr = "vrs"
	DataStr    = "data"
	KeyStr     = "key"
	ExpireStr  = "exp"
	ActionStr  = "act"
)
View Source
const (
	CacheChannel = "cache-channel"
)

Variables

View Source
var (
	ErrRecordNotFound  = errors.New("缓存记录不存在")
	ErrBadExpireTime   = errors.New("分布式缓存的过期时间不得早于本地缓存")
	ErrDeleteKeyFailed = errors.New("key删除失败")
	ErrSetKeyFailed    = errors.New("key设置失败")
	ErrBadMultiCache   = errors.New("multiCache暂不可用")
	ErrDirtyData       = errors.New("读入的数据为脏数据")
	ErrMarshalFailed   = errors.New("marshal 失败")
	ErrUnmarshalFailed = errors.New("unmarshal 失败")
)

Functions

This section is empty.

Types

type CacheEntry

type CacheEntry struct {
	Version int64  `json:"vrs"`
	Data    []byte `json:"data"`
}

type CacheUpdateMessage

type CacheUpdateMessage struct {
	Key     string `json:"key"`
	Action  string `json:"act"`
	Data    []byte `json:"data,omitempty"`
	Version int64  `json:"vrs"`
}

func (CacheUpdateMessage) MarshalJSON

func (c CacheUpdateMessage) MarshalJSON() ([]byte, error)

逻辑跟joinMdData一样

func (*CacheUpdateMessage) UnmarshalJSON

func (c *CacheUpdateMessage) UnmarshalJSON(data []byte) error

type DistributedCache

type DistributedCache struct {
	*redis.ClusterClient
}

func MustNewDistributedCache

func MustNewDistributedCache(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 {
	EnableTracing bool
	//该Expire只能为distributedCache设置,localCache的超时一旦设置之后无法改变
	ExpireTime time.Duration
	//redlock    *redlock.RedLock
	//使用版本控制将带来更高的一致性,但是需要Set系列的函数传入的data是可以得到version字段的
	UseVersionControll bool
	RocketmqConsumer   rocketmq.PushConsumer
	//这个mtx只是保证本地缓存更新时是一致的,防止老版本更新慢于新版本更新
	Mtx *sync.RWMutex

	Logger *otelzap.SugaredLogger
	// contains filtered or unexported fields
}

func MustNewMultiCache

func MustNewMultiCache(dc *redis.ClusterOptions, lc *bigcache.Config, opts ...OptionFunc) *MultiCache

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

func (*MultiCache) DelWithPubSub

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

TODO: 后续可以在返回error里记录哪几个区间的key没被删除成功

func (*MultiCache) DelWithVersion

func (c *MultiCache) DelWithVersion(ctx context.Context, keys []string, vrs []int64) error

func (*MultiCache) GetData

func (c *MultiCache) GetData(data []byte) []byte

func (*MultiCache) GetDistributedCache

func (c *MultiCache) GetDistributedCache() *DistributedCache

func (*MultiCache) GetInPubSub

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

func (*MultiCache) GetLocalCache

func (c *MultiCache) GetLocalCache() *LocalCache

func (*MultiCache) GetVersion

func (c *MultiCache) GetVersion(data []byte) int64

默认情况下如果没有version字段则默认认为字段version为0

func (*MultiCache) GetVersionInString

func (c *MultiCache) GetVersionInString(data string) int64

与getVersion无异,只是参数换为string

func (*MultiCache) SetWithPubSub

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

func (*MultiCache) SetWithVersion

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

func (*MultiCache) Stats

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

func (*MultiCache) SubscribeUpdate

func (c *MultiCache) SubscribeUpdate(ctx context.Context)

type OptionFunc

type OptionFunc func(*MultiCache)

func WithExpireTime

func WithExpireTime(t time.Duration) OptionFunc

func WithLogger

func WithLogger(logger *otelzap.Logger) OptionFunc

WithLogger 配置日志记录器

func WithVersionControl

func WithVersionControl(enable bool) OptionFunc

Jump to

Keyboard shortcuts

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