-
Notifications
You must be signed in to change notification settings - Fork 4
/
report.go
94 lines (78 loc) · 3.12 KB
/
report.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2023 FishGoddess. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"io"
"strconv"
"time"
"github.com/FishGoddess/cachego"
)
func reportMissed(reporter *cachego.Reporter, key string) {
fmt.Printf("report: missed key %s, missed rate %.3f\n", key, reporter.MissedRate())
}
func reportHit(reporter *cachego.Reporter, key string, value interface{}) {
fmt.Printf("report: hit key %s value %+v, hit rate %.3f\n", key, value, reporter.HitRate())
}
func reportGC(reporter *cachego.Reporter, cost time.Duration, cleans int) {
fmt.Printf("report: gc cost %s cleans %d, gc count %d, cache size %d\n", cost, cleans, reporter.CountGC(), reporter.CacheSize())
}
func reportLoad(reporter *cachego.Reporter, key string, value interface{}, ttl time.Duration, err error) {
fmt.Printf("report: load key %s value %+v ttl %s, err %+v, load count %d\n", key, value, ttl, err, reporter.CountLoad())
}
func main() {
// We provide some ways to report the status of cache.
// Use NewCacheWithReport to create a cache with reporting features.
cache, reporter := cachego.NewCacheWithReport(
// Sometimes you may have several caches in one service.
// You can set each name by WithCacheName and get the name from reporter.
cachego.WithCacheName("test"),
// For testing...
cachego.WithMaxEntries(3),
cachego.WithGC(100*time.Millisecond),
// ReportMissed reports the missed key getting from cache.
// ReportHit reports the hit entry getting from cache.
// ReportGC reports the status of cache gc.
// ReportLoad reports the result of loading.
cachego.WithReportMissed(reportMissed),
cachego.WithReportHit(reportHit),
cachego.WithReportGC(reportGC),
cachego.WithReportLoad(reportLoad),
)
for i := 0; i < 5; i++ {
key := strconv.Itoa(i)
evictedValue := cache.Set(key, key, 10*time.Millisecond)
fmt.Println(evictedValue)
}
for i := 0; i < 5; i++ {
key := strconv.Itoa(i)
value, ok := cache.Get(key)
fmt.Println(value, ok)
}
time.Sleep(200 * time.Millisecond)
value, err := cache.Load("key", time.Second, func() (value interface{}, err error) {
return 666, io.EOF
})
fmt.Println(value, err)
// These are some useful methods of reporter.
fmt.Println("CacheName:", reporter.CacheName())
fmt.Println("CacheType:", reporter.CacheType())
fmt.Println("CountMissed:", reporter.CountMissed())
fmt.Println("CountHit:", reporter.CountHit())
fmt.Println("CountGC:", reporter.CountGC())
fmt.Println("CountLoad:", reporter.CountLoad())
fmt.Println("CacheSize:", reporter.CacheSize())
fmt.Println("MissedRate:", reporter.MissedRate())
fmt.Println("HitRate:", reporter.HitRate())
}