@@ -11,13 +11,34 @@ type cacheLogger struct {
1111
1212 hit atomic.Int64
1313 miss atomic.Int64
14+
15+ // cacheSize is a callback that returns the cache size as a
16+ // human-readable string.
17+ cacheSize func () string
18+ }
19+
20+ // cacheLoggerOption defines a functional option for configuring a cacheLogger.
21+ type cacheLoggerOption func (* cacheLogger )
22+
23+ // withCacheSizeFunc sets a callback that returns the cache size as a human
24+ // readable string.
25+ func withCacheSizeFunc (sizeFunc func () string ) cacheLoggerOption {
26+ return func (c * cacheLogger ) {
27+ c .cacheSize = sizeFunc
28+ }
1429}
1530
1631// newCacheLogger returns a new cacheLogger with the given name.
17- func newCacheLogger (name string ) * cacheLogger {
18- return & cacheLogger {
32+ func newCacheLogger (name string , opts ... cacheLoggerOption ) * cacheLogger {
33+ logger := & cacheLogger {
1934 name : name ,
2035 }
36+
37+ for _ , opt := range opts {
38+ opt (logger )
39+ }
40+
41+ return logger
2142}
2243
2344// Hit increments the hit counter for the cacheLogger. Every 100th call to this
@@ -46,6 +67,11 @@ func (c *cacheLogger) log() {
4667 total := hit + miss
4768 ratio := float64 (hit ) / float64 (total ) * 100
4869
49- log .Infof ("cacheLogger(name=%s, hits=%d, misses=%d, hit_ratio=%.2f%%)" ,
50- c .name , hit , miss , ratio )
70+ size := ""
71+ if c .cacheSize != nil {
72+ size = c .cacheSize ()
73+ }
74+
75+ log .Infof ("cacheLogger(name=%s, hits=%d, misses=%d, " +
76+ "hit_ratio=%.2f%%, size=%s)" , c .name , hit , miss , ratio , size )
5177}
0 commit comments