66 lines
1.3 KiB
Go
66 lines
1.3 KiB
Go
|
|
package cache
|
||
|
|
|
||
|
|
import "sync/atomic"
|
||
|
|
|
||
|
|
// cacheMetrics 缓存指标
|
||
|
|
type cacheMetrics struct {
|
||
|
|
hit atomic.Int64
|
||
|
|
miss atomic.Int64
|
||
|
|
decodeError atomic.Int64
|
||
|
|
setError atomic.Int64
|
||
|
|
invalidate atomic.Int64
|
||
|
|
}
|
||
|
|
|
||
|
|
// metrics 全局缓存指标实例
|
||
|
|
var metrics cacheMetrics
|
||
|
|
|
||
|
|
// MetricsSnapshot 指标快照
|
||
|
|
type MetricsSnapshot struct {
|
||
|
|
Hit int64
|
||
|
|
Miss int64
|
||
|
|
DecodeError int64
|
||
|
|
SetError int64
|
||
|
|
Invalidate int64
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetMetricsSnapshot 获取当前指标快照
|
||
|
|
func GetMetricsSnapshot() MetricsSnapshot {
|
||
|
|
return MetricsSnapshot{
|
||
|
|
Hit: metrics.hit.Load(),
|
||
|
|
Miss: metrics.miss.Load(),
|
||
|
|
DecodeError: metrics.decodeError.Load(),
|
||
|
|
SetError: metrics.setError.Load(),
|
||
|
|
Invalidate: metrics.invalidate.Load(),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// recordHit 记录缓存命中
|
||
|
|
func recordHit() {
|
||
|
|
metrics.hit.Add(1)
|
||
|
|
}
|
||
|
|
|
||
|
|
// recordMiss 记录缓存未命中
|
||
|
|
func recordMiss() {
|
||
|
|
metrics.miss.Add(1)
|
||
|
|
}
|
||
|
|
|
||
|
|
// recordDecodeError 记录解码错误
|
||
|
|
func recordDecodeError() {
|
||
|
|
metrics.decodeError.Add(1)
|
||
|
|
}
|
||
|
|
|
||
|
|
// recordSetError 记录设置错误
|
||
|
|
func recordSetError() {
|
||
|
|
metrics.setError.Add(1)
|
||
|
|
}
|
||
|
|
|
||
|
|
// recordInvalidate 记录缓存失效
|
||
|
|
func recordInvalidate() {
|
||
|
|
metrics.invalidate.Add(1)
|
||
|
|
}
|
||
|
|
|
||
|
|
// recordInvalidateMultiple 记录多个缓存失效
|
||
|
|
func recordInvalidateMultiple(count int64) {
|
||
|
|
metrics.invalidate.Add(count)
|
||
|
|
}
|