cache

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2022 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package cache 缓存对象 更新缓存往往是竞争更新,因此需要使用分布式锁避免重复计算,同时等待更新完成后再取数据.

Index

Constants

This section is empty.

Variables

View Source
var Defaultopt = Options{
	EmptyResCacheMode:           EmptyResCacheMode__IGNORE,
	QueryAutoUpdateCacheTimeout: 300 * time.Millisecond,
	QueryLockTimeout:            300 * time.Millisecond,
	QueryLimiterTimeout:         300 * time.Millisecond,
}

Defaultopt 默认的可选配置

View Source
var ErrAutoUpdateAlreadyStarted = errors.New("已经启动了自动更新任务")

ErrAutoUpdateAlreadyStarted 已经启动了自动更新任务

View Source
var ErrAutoUpdateNeedUpdatePeriod = errors.New("自动更新需要设置UpdatePeriod")

ErrAutoUpdateNeedUpdatePeriod 自动更新需要设置UpdatePeriod

View Source
var ErrLimiterNotAllow = errors.New("限制器不允许执行更新任务")

ErrLimiterNotAllow 限制器不允许执行更新任务

View Source
var ErrUpdateFuncAlreadyRegisted = errors.New("缓存函数已经注册")

ErrUpdateFuncAlreadyRegisted 缓存更新函数已经注册

Functions

This section is empty.

Types

type Cache

type Cache struct {
	*clientkey.ClientKey
	// contains filtered or unexported fields
}

Cache 缓存 缓存对象的锁可以和缓存不使用同一个redis客户端,甚至可以不用redis,只要他满足Canlock接口 缓存设置UpdatePeriod后会自动定时同步数据

func New

func New(k *clientkey.ClientKey, opts ...Option) *Cache

New 创建一个缓存实例 updatePeriod会取最后设置的非空字符串

func (*Cache) ActiveUpdate added in v0.0.2

func (c *Cache) ActiveUpdate()

ActiveUpdate 主动更新函数,可以被用作定时主动更新,也可以通过监听外部信号来实现更新

func (*Cache) AutoUpdate

func (c *Cache) AutoUpdate() error

AutoUpdate 自动更新缓存,自动更新缓存内容也受限流器影响,同时锁会用于限制更新程序的执行

func (*Cache) Get

func (c *Cache) Get(ctx context.Context, force ForceLevelType) ([]byte, error)

Get 获取数据 如果缓存中有就从缓存中获取,如果没有则直接从注册的缓存函数中获取,然后将结果更新到缓存 如果cache的key有设置MaxTTL则在获取到缓存的数据是会刷新key的过期时间 @param force bool 是否强制获取数据

func (*Cache) RegistUpdateFunc

func (c *Cache) RegistUpdateFunc(fn Cachefunc) error

RegistUpdateFunc 注册缓存函数到对象

func (*Cache) StopAutoUpdate

func (c *Cache) StopAutoUpdate() error

StopAutoUpdate 取消自动更新缓存

type Cachefunc

type Cachefunc func() ([]byte, error)

Cachefunc 缓存函数结果

type EmptyResCacheModeType

type EmptyResCacheModeType uint16

EmptyResCacheModeType 处理更新函数返回空值的模式

const (

	//EmptyResCacheMode__IGNORE 当更新函数得到的结果为空时不会放入缓存,而是刷新之前的过期时间保持原有缓存
	EmptyResCacheMode__IGNORE EmptyResCacheModeType = iota
	//EmptyResCacheMode__DELETE 当更新函数得到的结果为空时会删除缓存以便下次再执行更新操作
	EmptyResCacheMode__DELETE
	//EmptyResCacheMode__SAVE 当更新函数得到的结果为空时依然存入作为缓存
	EmptyResCacheMode__SAVE
)

type ForceLevelType

type ForceLevelType uint16

ForceLevelType 强制执行类型枚举

const (

	//ForceLevel__STRICT 严格模式,无论如何只要报错和不满足组件要求就会终止,当更新函数得到的结果为空时不会放入缓存,而是刷新之前的过期时间
	ForceLevel__STRICT ForceLevelType = iota
	//ForceLevel__CONSTRAINT 约束模式,组件自身失效会继续处理,当更新函数得到的结果为空时会删除缓存以便下次再执行更新操作
	ForceLevel__CONSTRAINT
	//ForceLevel__NOCONSTRAINT 无约束模式,无视组件处理,当更新函数得到的结果为空时不会放入缓存,当更新函数得到的结果为空时依然存入作为缓存
	ForceLevel__NOCONSTRAINT
)

type Option

type Option interface {
	Apply(*Options)
}

Option configures how we set up the connection.

func WithLimiter

func WithLimiter(limiter h.CanBeLimiter) Option

WithLimiter 设置分布式限制器,限制器的作用是设置一段时间内的最大更新次数

func WithLimiterTimeout added in v0.0.2

func WithLimiterTimeout(timeout time.Duration) Option

WithLimiterTimeout 设置请求限流器的过期时间,默认300毫秒

func WithLock

func WithLock(lock h.Canlock) Option

WithLock 设置分布式锁防止重复计算,分布式锁的作用是限制最小更新间隔

func WithLockTimeout added in v0.0.2

func WithLockTimeout(timeout time.Duration) Option

WithLockTimeout 设置请求分布式锁的过期时间,默认300毫秒

func WithQueryAutoUpdateCacheTimeout added in v0.0.2

func WithQueryAutoUpdateCacheTimeout(timeout time.Duration) Option

WithQueryAutoUpdateCacheTimeout 设置自动更新缓存内容时写入缓存的过期时间,默认300毫秒

func WithUpdatePeriod

func WithUpdatePeriod(updatePeriod string) Option

WithUpdatePeriod 设置定时更新

type Options

type Options struct {
	UpdatePeriod                string                //使用自动更新,使用crontab格式
	QueryAutoUpdateCacheTimeout time.Duration         //自动更新时写入缓存的过期时间
	Lock                        h.Canlock             //使用的锁
	QueryLockTimeout            time.Duration         //请求锁的超时时间
	Limiter                     h.CanBeLimiter        //使用的限制器
	QueryLimiterTimeout         time.Duration         //请求限流器的超时时间
	EmptyResCacheMode           EmptyResCacheModeType //处理更新函数返回空值的模式
}

Options broker的配置

Jump to

Keyboard shortcuts

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