Skip to content

Commit c4c9bc2

Browse files
committed
tapdb: extend newCacheLogger with func option for size callback
* Add support for an optional cache size callback to newCacheLogger. * Enable cache size logging in the universe proof cache.
1 parent b2a58b5 commit c4c9bc2

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

tapdb/cache_logger.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

tapdb/multiverse_cache.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,22 @@ type universeProofCache struct {
154154

155155
// newUniverseProofCache creates a new proof cache.
156156
func newUniverseProofCache(maxCacheByteSize uint64) *universeProofCache {
157+
cache := newProofCache(maxCacheByteSize)
158+
159+
// Formulate a callback function that returns the cache size in a
160+
// human-readable format. This will be called by the cache logger to get
161+
// the current cache size.
162+
cacheSizeLogStr := func() string {
163+
return humanize.Bytes(cache.Size())
164+
}
165+
cacheLogger := newCacheLogger(
166+
"universe_proofs", withCacheSizeFunc(cacheSizeLogStr),
167+
)
168+
157169
return &universeProofCache{
158170
maxCacheByteSize: maxCacheByteSize,
159-
cache: newProofCache(maxCacheByteSize),
160-
cacheLogger: newCacheLogger("universe_proofs"),
171+
cache: cache,
172+
cacheLogger: cacheLogger,
161173
}
162174
}
163175

0 commit comments

Comments
 (0)