@@ -13,15 +13,12 @@ import (
13
13
"github.com/coocood/freecache"
14
14
"github.com/go-redis/redis/v8"
15
15
"github.com/klauspost/compress/s2"
16
- "github.com/prometheus/client_golang/prometheus"
17
16
"github.com/rs/zerolog/log"
18
17
uuid "github.com/satori/go.uuid"
19
18
"github.com/vmihailenco/msgpack/v5"
20
19
"golang.org/x/sync/singleflight"
21
20
)
22
21
23
- var _ = log .Logger
24
-
25
22
const (
26
23
lockSuffix = "_LOCK"
27
24
delimiter = "~|~"
@@ -50,10 +47,6 @@ const (
50
47
s2Compression = 0x1
51
48
)
52
49
53
- var (
54
- getNow = time .Now
55
- )
56
-
57
50
var (
58
51
// ErrTimeout is timeout error
59
52
ErrTimeout = errors .New ("timeout" )
65
58
ErrTypeMismatch = errors .New ("value type mismatches cached type" )
66
59
)
67
60
61
+ var (
62
+ getNow = time .Now
63
+ )
64
+
68
65
// SetNowFunc is a helper function to replace time.Now(), usually used for testing.
69
66
func SetNowFunc (f func () time.Time ) { getNow = f }
70
67
@@ -81,76 +78,6 @@ type ValueBytesExpiredAt struct {
81
78
ExpiredAt int64 `msgpack:"e,omitempty"` // UNIX timestamp in Milliseconds.
82
79
}
83
80
84
- type metricSet struct {
85
- Hit * prometheus.CounterVec
86
- Latency * prometheus.HistogramVec
87
- Error * prometheus.CounterVec
88
- }
89
-
90
- type metricHitLabel string
91
- type metricErrLabel string
92
-
93
- var (
94
- hitLabels = []string {"hit" }
95
- // metrics hit labels
96
- hitLabelMemory metricHitLabel = "mem"
97
- hitLabelRedis metricHitLabel = "redis"
98
- hitLabelDB metricHitLabel = "db"
99
- // The unit is ms.
100
- latencyBucket = []float64 {
101
- 1 , 4 , 8 , 16 , 32 , 64 , 128 , 256 , 512 , 1024 , 2048 , 4096 }
102
-
103
- errLabels = []string {"when" }
104
- // metrics error labels
105
- errLabelSetRedis metricErrLabel = "set_redis"
106
- errLabelSetMemCache metricErrLabel = "set_mem_cache"
107
- errLabelInvalidate metricErrLabel = "invalidate_error"
108
- errLabelMemoryUnmarshalFailed metricErrLabel = "mem_unmarshal_failed"
109
- errLabelRedisUnmarshalFailed metricErrLabel = "redis_unmarshal_failed"
110
- )
111
-
112
- func newMetricSet (appName string ) * metricSet {
113
- return & metricSet {
114
- Hit : prometheus .NewCounterVec (
115
- prometheus.CounterOpts {
116
- Name : fmt .Sprintf ("%s_dcache_hit_total" , appName ),
117
- Help : "how many hits of 3 different operations: {mem, redis, db}." ,
118
- }, hitLabels ),
119
- Latency : prometheus .NewHistogramVec (
120
- prometheus.HistogramOpts {
121
- Name : fmt .Sprintf ("%s_dcache_latency_milliseconds" , appName ),
122
- Help : "Cache read latency in milliseconds" ,
123
- Buckets : latencyBucket ,
124
- }, hitLabels ),
125
- Error : prometheus .NewCounterVec (
126
- prometheus.CounterOpts {
127
- Name : fmt .Sprintf ("%s_dcache_error_total" , appName ),
128
- Help : "how many internal errors happened" ,
129
- }, errLabels ),
130
- }
131
- }
132
-
133
- func (m * metricSet ) Register () {
134
- err := prometheus .Register (m .Hit )
135
- if err != nil {
136
- log .Err (err ).Msgf ("failed to register prometheus Hit counters" )
137
- }
138
- err = prometheus .Register (m .Latency )
139
- if err != nil {
140
- log .Err (err ).Msgf ("failed to register prometheus Latency histogram" )
141
- }
142
- err = prometheus .Register (m .Error )
143
- if err != nil {
144
- log .Err (err ).Msgf ("failed to register prometheus Error counter" )
145
- }
146
- }
147
-
148
- func (m * metricSet ) Unregister () {
149
- prometheus .Unregister (m .Hit )
150
- prometheus .Unregister (m .Error )
151
- prometheus .Unregister (m .Latency )
152
- }
153
-
154
81
// DCache implements cache.
155
82
type DCache struct {
156
83
conn redis.UniversalClient
0 commit comments