-
Notifications
You must be signed in to change notification settings - Fork 271
/
stats.go
53 lines (44 loc) · 1003 Bytes
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package gcache
import (
"sync/atomic"
)
type statsAccessor interface {
HitCount() uint64
MissCount() uint64
LookupCount() uint64
HitRate() float64
}
// statistics
type stats struct {
hitCount uint64
missCount uint64
}
// increment hit count
func (st *stats) IncrHitCount() uint64 {
return atomic.AddUint64(&st.hitCount, 1)
}
// increment miss count
func (st *stats) IncrMissCount() uint64 {
return atomic.AddUint64(&st.missCount, 1)
}
// HitCount returns hit count
func (st *stats) HitCount() uint64 {
return atomic.LoadUint64(&st.hitCount)
}
// MissCount returns miss count
func (st *stats) MissCount() uint64 {
return atomic.LoadUint64(&st.missCount)
}
// LookupCount returns lookup count
func (st *stats) LookupCount() uint64 {
return st.HitCount() + st.MissCount()
}
// HitRate returns rate for cache hitting
func (st *stats) HitRate() float64 {
hc, mc := st.HitCount(), st.MissCount()
total := hc + mc
if total == 0 {
return 0.0
}
return float64(hc) / float64(total)
}