@@ -330,36 +330,32 @@ Package cachego provides an easy way to use foundation for your caching operatio
330330
3313318. report:
332332
333- func reportMissed(key string) {
334- fmt.Printf("report: missed key %s\n", key)
333+ func reportMissed(reporter *cachego.Reporter, key string) {
334+ fmt.Printf("report: missed key %s, missed rate %.3f \n", key, reporter.MissedRate() )
335335 }
336336
337- func reportHit(key string, value interface{}) {
338- fmt.Printf("report: hit key %s value %+v\n", key, value)
337+ func reportHit(reporter *cachego.Reporter, key string, value interface{}) {
338+ fmt.Printf("report: hit key %s value %+v, hit rate %.3f \n", key, value, reporter.HitRate() )
339339 }
340340
341- func reportGC(cost time.Duration, cleans int) {
342- fmt.Printf("report: gc cost %s cleans %d\n", cost, cleans)
341+ func reportGC(reporter *cachego.Reporter, cost time.Duration, cleans int) {
342+ fmt.Printf("report: gc cost %s cleans %d, gc count %d, cache size %d \n", cost, cleans, reporter.CountGC(), reporter.CacheSize() )
343343 }
344344
345- func reportLoad(key string, value interface{}, ttl time.Duration, err error) {
346- fmt.Printf("report: load key %s value %+v ttl %s, err %+v\n", key, value, ttl, err)
345+ func reportLoad(reporter *cachego.Reporter, key string, value interface{}, ttl time.Duration, err error) {
346+ fmt.Printf("report: load key %s value %+v ttl %s, err %+v, load count %d \n", key, value, ttl, err, reporter.CountLoad() )
347347 }
348348
349- // Create a cache as usual.
350- cache := cachego.NewCache(
351- cachego.WithMaxEntries(3),
352- cachego.WithGC(100*time.Millisecond),
353- )
354-
355- // Use Report function to wrap a cache with reporting logics.
356349 // We provide some reporting points for monitor cache.
357350 // ReportMissed reports the missed key getting from cache.
358351 // ReportHit reports the hit entry getting from cache.
359352 // ReportGC reports the status of cache gc.
360353 // ReportLoad reports the result of loading.
361- cache, reporter := cachego.Report(
362- cache,
354+ // Use NewCacheWithReport to create a cache with report.
355+ cache, reporter := cachego.NewCacheWithReport(
356+ cachego.WithMaxEntries(3),
357+ cachego.WithGC(100*time.Millisecond),
358+
363359 cachego.WithReportMissed(reportMissed),
364360 cachego.WithReportHit(reportHit),
365361 cachego.WithReportGC(reportGC),
@@ -390,6 +386,7 @@ Package cachego provides an easy way to use foundation for your caching operatio
390386 fmt.Println("CountMissed:", reporter.CountMissed())
391387 fmt.Println("CountHit:", reporter.CountHit())
392388 fmt.Println("CountGC:", reporter.CountGC())
389+ fmt.Println("CountLoad:", reporter.CountLoad())
393390 fmt.Println("CacheSize:", reporter.CacheSize())
394391 fmt.Println("MissedRate:", reporter.MissedRate())
395392 fmt.Println("HitRate:", reporter.HitRate())
@@ -454,4 +451,4 @@ Package cachego provides an easy way to use foundation for your caching operatio
454451package cachego // import "github.com/FishGoddess/cachego"
455452
456453// Version is the version string representation of cachego.
457- const Version = "v0.4.4 -alpha"
454+ const Version = "v0.4.5 -alpha"
0 commit comments