Documentation ¶
Overview ¶
Package cache 缓存对象 更新缓存往往是竞争更新,因此需要使用分布式锁避免重复计算,同时等待更新完成后再取数据.
Index ¶
- Variables
- type Cache
- type Cachefunc
- type EmptyResCacheModeType
- type ForceLevelType
- type Option
- func WithLimiter(limiter h.CanBeLimiter) Option
- func WithLimiterTimeout(timeout time.Duration) Option
- func WithLock(lock h.Canlock) Option
- func WithLockTimeout(timeout time.Duration) Option
- func WithQueryAutoUpdateCacheTimeout(timeout time.Duration) Option
- func WithUpdatePeriod(updatePeriod string) Option
- type Options
Constants ¶
This section is empty.
Variables ¶
var Defaultopt = Options{ EmptyResCacheMode: EmptyResCacheMode__IGNORE, QueryAutoUpdateCacheTimeout: 300 * time.Millisecond, QueryLockTimeout: 300 * time.Millisecond, QueryLimiterTimeout: 300 * time.Millisecond, }
Defaultopt 默认的可选配置
var ErrAutoUpdateAlreadyStarted = errors.New("已经启动了自动更新任务")
ErrAutoUpdateAlreadyStarted 已经启动了自动更新任务
var ErrAutoUpdateNeedUpdatePeriod = errors.New("自动更新需要设置UpdatePeriod")
ErrAutoUpdateNeedUpdatePeriod 自动更新需要设置UpdatePeriod
var ErrLimiterNotAllow = errors.New("限制器不允许执行更新任务")
ErrLimiterNotAllow 限制器不允许执行更新任务
var ErrUpdateFuncAlreadyRegisted = errors.New("缓存函数已经注册")
ErrUpdateFuncAlreadyRegisted 缓存更新函数已经注册
Functions ¶
This section is empty.
Types ¶
type Cache ¶
Cache 缓存 缓存对象的锁可以和缓存不使用同一个redis客户端,甚至可以不用redis,只要他满足Canlock接口 缓存设置UpdatePeriod后会自动定时同步数据
func (*Cache) ActiveUpdate ¶ added in v0.0.2
func (c *Cache) ActiveUpdate()
ActiveUpdate 主动更新函数,可以被用作定时主动更新,也可以通过监听外部信号来实现更新
func (*Cache) AutoUpdate ¶
AutoUpdate 自动更新缓存,自动更新缓存内容也受限流器影响,同时锁会用于限制更新程序的执行
func (*Cache) Get ¶
Get 获取数据 如果缓存中有就从缓存中获取,如果没有则直接从注册的缓存函数中获取,然后将结果更新到缓存 如果cache的key有设置MaxTTL则在获取到缓存的数据是会刷新key的过期时间 @param force bool 是否强制获取数据
func (*Cache) RegistUpdateFunc ¶
RegistUpdateFunc 注册缓存函数到对象
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
WithLimiterTimeout 设置请求限流器的过期时间,默认300毫秒
func WithLockTimeout ¶ added in v0.0.2
WithLockTimeout 设置请求分布式锁的过期时间,默认300毫秒
func WithQueryAutoUpdateCacheTimeout ¶ added in v0.0.2
WithQueryAutoUpdateCacheTimeout 设置自动更新缓存内容时写入缓存的过期时间,默认300毫秒
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的配置