- Add Google Wire for compile-time dependency injection - Replace concrete service types with interfaces across handlers - Remove global state (DB, cache) in favor of constructor injection - Split monolithic files into focused modules: - config: separate files for each config domain - dto: converters split by domain (user, post, message, group) - cache: separate metrics.go and redis_cache.go - Introduce unified apperrors package with string-based error codes - Add transaction support with context-aware repository methods - Upgrade golang.org/x/crypto from 0.17.0 to 0.26.0
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)
|
|
}
|