Skip to content

Commit 8ebbcf3

Browse files
committed
处理 report 功能
1 parent 8d141dd commit 8ebbcf3

7 files changed

Lines changed: 182 additions & 57 deletions

File tree

_examples/report.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@ import (
2323
"github.com/FishGoddess/cachego"
2424
)
2525

26-
func reportMissed(key string) {
27-
fmt.Printf("report: missed key %s\n", key)
26+
func reportMissed(reporter *cachego.Reporter, key string) {
27+
fmt.Printf("report: missed key %s, missed rate %.3f\n", key, reporter.MissedRate())
2828
}
2929

30-
func reportHit(key string, value interface{}) {
31-
fmt.Printf("report: hit key %s value %+v\n", key, value)
30+
func reportHit(reporter *cachego.Reporter, key string, value interface{}) {
31+
fmt.Printf("report: hit key %s value %+v, hit rate %.3f\n", key, value, reporter.HitRate())
3232
}
3333

34-
func reportGC(cost time.Duration, cleans int) {
35-
fmt.Printf("report: gc cost %s cleans %d\n", cost, cleans)
34+
func reportGC(reporter *cachego.Reporter, cost time.Duration, cleans int) {
35+
fmt.Printf("report: gc cost %s cleans %d, gc count %d, cache size %d\n", cost, cleans, reporter.CountGC(), reporter.CacheSize())
3636
}
3737

38-
func reportLoad(key string, value interface{}, ttl time.Duration, err error) {
39-
fmt.Printf("report: load key %s value %+v ttl %s, err %+v\n", key, value, ttl, err)
38+
func reportLoad(reporter *cachego.Reporter, key string, value interface{}, ttl time.Duration, err error) {
39+
fmt.Printf("report: load key %s value %+v ttl %s, err %+v, load count %d\n", key, value, ttl, err, reporter.CountLoad())
4040
}
4141

4242
func main() {
@@ -84,6 +84,7 @@ func main() {
8484
fmt.Println("CountMissed:", reporter.CountMissed())
8585
fmt.Println("CountHit:", reporter.CountHit())
8686
fmt.Println("CountGC:", reporter.CountGC())
87+
fmt.Println("CountLoad:", reporter.CountLoad())
8788
fmt.Println("CacheSize:", reporter.CacheSize())
8889
fmt.Println("MissedRate:", reporter.MissedRate())
8990
fmt.Println("HitRate:", reporter.HitRate())

config.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ type config struct {
2727

2828
now func() int64
2929
hash func(key string) int
30-
31-
reportMissed func(key string)
32-
reportHit func(key string, value interface{})
33-
reportGC func(cost time.Duration, cleans int)
34-
reportLoad func(key string, value interface{}, ttl time.Duration, err error)
3530
}
3631

3732
func newDefaultConfig() *config {
@@ -50,15 +45,24 @@ func newDefaultConfig() *config {
5045
type reportConfig struct {
5146
now func() int64
5247

53-
reportMissed func(key string)
54-
reportHit func(key string, value interface{})
55-
reportGC func(cost time.Duration, cleans int)
56-
reportLoad func(key string, value interface{}, ttl time.Duration, err error)
48+
recordMissed bool
49+
recordHit bool
50+
recordGC bool
51+
recordLoad bool
52+
53+
reportMissed func(reporter *Reporter, key string)
54+
reportHit func(reporter *Reporter, key string, value interface{})
55+
reportGC func(reporter *Reporter, cost time.Duration, cleans int)
56+
reportLoad func(reporter *Reporter, key string, value interface{}, ttl time.Duration, err error)
5757
}
5858

5959
func newDefaultReportConfig() *reportConfig {
6060
return &reportConfig{
6161
now: now,
62+
recordMissed: true,
63+
recordHit: true,
64+
recordGC: true,
65+
recordLoad: true,
6266
reportMissed: nil,
6367
reportHit: nil,
6468
reportGC: nil,

config_test.go

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,27 +52,23 @@ func isConfigEquals(conf1 *config, conf2 *config) bool {
5252
return false
5353
}
5454

55-
if fmt.Sprintf("%p", conf1.reportMissed) != fmt.Sprintf("%p", conf2.reportMissed) {
56-
return false
57-
}
55+
return true
56+
}
5857

59-
if fmt.Sprintf("%p", conf1.reportHit) != fmt.Sprintf("%p", conf2.reportHit) {
58+
func isReportConfigEquals(conf1 *reportConfig, conf2 *reportConfig) bool {
59+
if fmt.Sprintf("%p", conf1.now) != fmt.Sprintf("%p", conf2.now) {
6060
return false
6161
}
6262

63-
if fmt.Sprintf("%p", conf1.reportGC) != fmt.Sprintf("%p", conf2.reportGC) {
63+
if conf1.recordMissed != conf2.recordMissed {
6464
return false
6565
}
6666

67-
if fmt.Sprintf("%p", conf1.reportLoad) != fmt.Sprintf("%p", conf2.reportLoad) {
67+
if conf1.recordHit != conf2.recordHit {
6868
return false
6969
}
7070

71-
return true
72-
}
73-
74-
func isReportConfigEquals(conf1 *reportConfig, conf2 *reportConfig) bool {
75-
if fmt.Sprintf("%p", conf1.now) != fmt.Sprintf("%p", conf2.now) {
71+
if conf1.recordGC != conf2.recordGC {
7672
return false
7773
}
7874

@@ -128,17 +124,32 @@ func TestApplyOptions(t *testing.T) {
128124

129125
// go test -v -cover -run=^TestApplyReportOptions$
130126
func TestApplyReportOptions(t *testing.T) {
131-
reportHit := func(key string, value interface{}) {}
127+
reportHit := func(reporter *Reporter, key string, value interface{}) {}
132128

133-
got := &reportConfig{}
129+
got := &reportConfig{
130+
now: nil,
131+
recordMissed: false,
132+
recordHit: false,
133+
recordGC: false,
134+
recordLoad: false,
135+
reportHit: nil,
136+
}
134137

135138
expect := &reportConfig{
136-
now: now,
137-
reportHit: reportHit,
139+
now: now,
140+
recordMissed: true,
141+
recordHit: true,
142+
recordGC: true,
143+
recordLoad: true,
144+
reportHit: reportHit,
138145
}
139146

140147
applyReportOptions(got, []ReportOption{
141148
WithReporterNow(now),
149+
WithRecordMissed(true),
150+
WithRecordHit(true),
151+
WithRecordGC(true),
152+
WithRecordLoad(true),
142153
WithReportHit(reportHit),
143154
})
144155

option.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,29 +131,57 @@ func WithReporterNow(now func() int64) ReportOption {
131131
}
132132
}
133133

134+
// WithRecordMissed returns an option setting the recordMissed of report config.
135+
func WithRecordMissed(recordMissed bool) ReportOption {
136+
return func(conf *reportConfig) {
137+
conf.recordMissed = recordMissed
138+
}
139+
}
140+
141+
// WithRecordHit returns an option setting the recordHit of report config.
142+
func WithRecordHit(recordHit bool) ReportOption {
143+
return func(conf *reportConfig) {
144+
conf.recordHit = recordHit
145+
}
146+
}
147+
148+
// WithRecordGC returns an option setting the recordGC of report config.
149+
func WithRecordGC(recordGC bool) ReportOption {
150+
return func(conf *reportConfig) {
151+
conf.recordGC = recordGC
152+
}
153+
}
154+
155+
// WithRecordLoad returns an option setting the recordLoad of report config.
156+
func WithRecordLoad(recordLoad bool) ReportOption {
157+
return func(conf *reportConfig) {
158+
conf.recordLoad = recordLoad
159+
}
160+
}
161+
134162
// WithReportMissed returns an option setting the reportMissed of report config.
135-
func WithReportMissed(reportMissed func(key string)) ReportOption {
163+
func WithReportMissed(reportMissed func(reporter *Reporter, key string)) ReportOption {
136164
return func(conf *reportConfig) {
137165
conf.reportMissed = reportMissed
138166
}
139167
}
140168

141169
// WithReportHit returns an option setting the reportHit of report config.
142-
func WithReportHit(reportHit func(key string, value interface{})) ReportOption {
170+
func WithReportHit(reportHit func(reporter *Reporter, key string, value interface{})) ReportOption {
143171
return func(conf *reportConfig) {
144172
conf.reportHit = reportHit
145173
}
146174
}
147175

148176
// WithReportGC returns an option setting the reportGC of report config.
149-
func WithReportGC(reportGC func(cost time.Duration, cleans int)) ReportOption {
177+
func WithReportGC(reportGC func(reporter *Reporter, cost time.Duration, cleans int)) ReportOption {
150178
return func(conf *reportConfig) {
151179
conf.reportGC = reportGC
152180
}
153181
}
154182

155183
// WithReportLoad returns an option setting the reportLoad of report config.
156-
func WithReportLoad(reportLoad func(key string, value interface{}, ttl time.Duration, err error)) ReportOption {
184+
func WithReportLoad(reportLoad func(reporter *Reporter, key string, value interface{}, ttl time.Duration, err error)) ReportOption {
157185
return func(conf *reportConfig) {
158186
conf.reportLoad = reportLoad
159187
}

option_test.go

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,53 @@ func TestWithReporterNow(t *testing.T) {
141141
}
142142
}
143143

144+
// go test -v -cover -run=^TestWithRecordMissed$
145+
func TestWithRecordMissed(t *testing.T) {
146+
got := &reportConfig{recordMissed: false}
147+
expect := &reportConfig{recordMissed: true}
148+
149+
WithRecordMissed(true).applyTo(got)
150+
if !isReportConfigEquals(got, expect) {
151+
t.Errorf("got %+v != expect %+v", got, expect)
152+
}
153+
}
154+
155+
// go test -v -cover -run=^TestWithRecordHit$
156+
func TestWithRecordHit(t *testing.T) {
157+
got := &reportConfig{recordHit: false}
158+
expect := &reportConfig{recordHit: true}
159+
160+
WithRecordHit(true).applyTo(got)
161+
if !isReportConfigEquals(got, expect) {
162+
t.Errorf("got %+v != expect %+v", got, expect)
163+
}
164+
}
165+
166+
// go test -v -cover -run=^TestWithRecordGC$
167+
func TestWithRecordGC(t *testing.T) {
168+
got := &reportConfig{recordGC: false}
169+
expect := &reportConfig{recordGC: true}
170+
171+
WithRecordGC(true).applyTo(got)
172+
if !isReportConfigEquals(got, expect) {
173+
t.Errorf("got %+v != expect %+v", got, expect)
174+
}
175+
}
176+
177+
// go test -v -cover -run=^TestWithRecordLoad$
178+
func TestWithRecordLoad(t *testing.T) {
179+
got := &reportConfig{recordLoad: false}
180+
expect := &reportConfig{recordLoad: true}
181+
182+
WithRecordLoad(true).applyTo(got)
183+
if !isReportConfigEquals(got, expect) {
184+
t.Errorf("got %+v != expect %+v", got, expect)
185+
}
186+
}
187+
144188
// go test -v -cover -run=^TestWithReportMissed$
145189
func TestWithReportMissed(t *testing.T) {
146-
reportMissed := func(key string) {}
190+
reportMissed := func(reporter *Reporter, key string) {}
147191

148192
got := &reportConfig{reportMissed: nil}
149193
expect := &reportConfig{reportMissed: reportMissed}
@@ -156,7 +200,7 @@ func TestWithReportMissed(t *testing.T) {
156200

157201
// go test -v -cover -run=^TestWithReportHit$
158202
func TestWithReportHit(t *testing.T) {
159-
reportHit := func(key string, value interface{}) {}
203+
reportHit := func(reporter *Reporter, key string, value interface{}) {}
160204

161205
got := &reportConfig{reportHit: nil}
162206
expect := &reportConfig{reportHit: reportHit}
@@ -169,7 +213,7 @@ func TestWithReportHit(t *testing.T) {
169213

170214
// go test -v -cover -run=^TestWithReportGC$
171215
func TestWithReportGC(t *testing.T) {
172-
reportGC := func(cost time.Duration, cleans int) {}
216+
reportGC := func(reporter *Reporter, cost time.Duration, cleans int) {}
173217

174218
got := &reportConfig{reportGC: nil}
175219
expect := &reportConfig{reportGC: reportGC}
@@ -182,7 +226,7 @@ func TestWithReportGC(t *testing.T) {
182226

183227
// go test -v -cover -run=^TestWithReportLoad$
184228
func TestWithReportLoad(t *testing.T) {
185-
reportLoad := func(key string, value interface{}, ttl time.Duration, err error) {}
229+
reportLoad := func(reporter *Reporter, key string, value interface{}, ttl time.Duration, err error) {}
186230

187231
got := &reportConfig{reportLoad: nil}
188232
expect := &reportConfig{reportLoad: reportLoad}

0 commit comments

Comments
 (0)