Documentation ¶
Index ¶
- Constants
- Variables
- type CachedValue
- type Encoder
- type Event
- type EventCacheExpired
- type EventCacheInvalid
- type FnResult
- type GaugeMetricType
- type HaCache
- func (hc *HaCache) Do(args ...interface{}) (interface{}, error)
- func (hc *HaCache) FnRun(background bool, args ...interface{}) (*FnResult, error)
- func (hc *HaCache) GenCacheKey(args ...interface{}) string
- func (hc *HaCache) Get(key string) (*CachedValue, error)
- func (hc *HaCache) Set(key string, data interface{}) error
- func (hc *HaCache) Trigger(event Event)
- type HaEncoder
- type MetricType
- type Options
- type Stats
- type Storage
Constants ¶
View Source
const ( // MHit 命中有效缓存 MHit MetricType = "hit" // MMiss 完全 miss MMiss MetricType = "miss" // MMissExpired 命中过期缓存,但是在可接受过期范围内 MMissExpired MetricType = "miss-expired" // MMissInvalid 命中过期缓存,在最大可接受失效时间范围外 MMissInvalid MetricType = "miss-invalid" // MInvalidReturned 强制返回过期缓存 MInvalidReturned MetricType = "invalid-returned" // MFnRun 执行原函数 MFnRun MetricType = "fn-run" // MFnRunErr 原函数执行出错 MFnRunErr MetricType = "fn-run-err" // MFnRunLimited 原函数执行被限流 MFnRunLimited MetricType = "fn-run-limited" // MEventChanBlocked 事件 channel block 住 MEventChanBlocked MetricType = "event-chan-blocked" // MSkip 不缓存 MSkip MetricType = "skip" // MWorkerPanic worker goroutine panic times MWorkerPanic MetricType = "worker-panic" // GMFnRunConcurrency 原函数执行并发度 GMFnRunConcurrency GaugeMetricType = "fn-run-concurrency" )
View Source
const SkipCache = "__hacache_skip_cache__"
SkipCache 当缓存 key 为 SkipCache 值时,跳过缓存
Variables ¶
View Source
var ( // ErrorFnRunLimited 达到原函数执行并发限制 ErrorFnRunLimited = errors.New("ha-cache fn run rate limited") // ErrorInvalidCacheKey 无效的缓存 key ErrorInvalidCacheKey = errors.New("invalid cache key") )
View Source
var CurrentStats = new(Stats)
CurrentStats 全局统计实例
Functions ¶
This section is empty.
Types ¶
type CachedValue ¶
type CachedValue struct { // protobuf message 序列化之后的 bytes Bytes []byte // 缓存创建的时间戳/s CreateTS int64 }
CachedValue 缓存值类型
type Encoder ¶
type Encoder interface { Encode(v interface{}) ([]byte, error) Decode(b []byte) (interface{}, error) NewValue() interface{} }
Encoder encoder of ha-cache
type EventCacheExpired ¶
type EventCacheExpired struct {
Args []interface{}
}
EventCacheExpired 缓存过期,但是可以接受,需要执行原始函数进行更新
type EventCacheInvalid ¶
type EventCacheInvalid struct { // Bytes 原始函数返回的结果,需要放到缓存里 Data interface{} // Key 缓存 key Key string }
EventCacheInvalid 缓存无效,需要立即更新
type FnResult ¶ added in v2.2.0
type FnResult struct { // Val 原函数返回值 Val interface{} // Err 原函数返回的 error Err error // Ignore 忽略返回值,不设置回缓存 Ignore bool }
FnResult 被缓存函数返回值的通用结构
type HaCache ¶
type HaCache struct {
// contains filtered or unexported fields
}
HaCache ha-cache struct
func (*HaCache) FnRun ¶
FnRun 执行原函数,原函数执行时,受并发限制, 如果是缓存过期异步更新,触发限流直接跳过; 如果是缓存失效同步更新,触发限流服务报错 被缓存的函数签名为: func(args ...interface{}) (*FnResult)
func (*HaCache) GenCacheKey ¶
GenCacheKey 生成缓存 key
type HaEncoder ¶
type HaEncoder struct { // 函数返回缓存中存的值类型,空值指针 // 用于将缓存中的值进行反序列化 NewValueFn func() interface{} }
HaEncoder default encoder of ha-cache 对于 protobuf message 使用 protobuf 序列化,其他 struct 使用 msgpack
type Options ¶
type Options struct { // 缓存穿透、更新时,执行原函数的最大并发数 // 达到了这个最大并发数,说明原函数处理时间比较长, // 有可能是出现某些故障。 FnRunLimit int32 // 最大可接受的过期时间 // 如果缓存过期,并且过期的时间 > MaxAcceptableExpiration,认为该过期的缓存为可接受的数据, // 直接返回并触发异步更新缓存任务。如果 过期时间 < MaxAcceptableExpiration ,则认为改数据 // 不可用,需要立即更新缓存,并将更新后的数据返回。 MaxAcceptableExpiration time.Duration // 缓存过期时间 Expiration time.Duration // 缓存使用的 storage Storage Storage // 生成缓存 key 的函数,函数参数必须与 Fn 一致 GenKeyFn interface{} // 被缓存的原函数 Fn interface{} // 事件 channel size EventBufferSize int32 // message encoder Encoder Encoder // logger Logger *zap.Logger }
Options ha-cache options struct
type Stats ¶
type Stats struct { // 命中有效缓存次数 Hit int32 // 命中失效缓存次数 MissExpired int32 // 命中在最大可接受失效时间范围外的次数 MissInvalid int32 // 强制返回过期缓存次数 InvalidReturned int32 // 完全 miss Miss int32 // 原函数执行次数 FnRun int32 // 原函数执行被限流 FnRunLimited int32 FnRunErr int32 EventChanBlocked int32 Skip int32 WorkerPanic int32 // FnRun 当前执行并发度 FnRunConcurrency int32 Exporter *statsd.Client }
Stats 缓存统计数据
func (*Stats) ExportGauge ¶ added in v2.3.0
func (s *Stats) ExportGauge() map[GaugeMetricType]int32
ExportGauge 获取 Gauge 数据
func (*Stats) Gauge ¶ added in v2.3.0
func (s *Stats) Gauge(m GaugeMetricType, i int32)
Gauge 设置某项指标 gauge 数据
Click to show internal directories.
Click to hide internal directories.