Skip to content

Commit 754ff4a

Browse files
feature(ctxcache): disable verbose logs by default (#113)
1 parent ac371a1 commit 754ff4a

2 files changed

Lines changed: 36 additions & 6 deletions

File tree

ctxcache/cache.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@ func (c *contextCache[T]) get(ctx context.Context, cacheKey string, builder func
2727
c.lock.Lock()
2828
defer c.lock.Unlock()
2929
if cached, exists := c.data[cacheKey]; exists {
30-
logger.Debug(ctx, "context cache hit")
30+
if verboseLogsEnabled(ctx) {
31+
logger.Debug(ctx, "context cache hit")
32+
}
3133
return cached
3234
}
33-
logger.Debug(ctx, "context cache miss")
35+
if verboseLogsEnabled(ctx) {
36+
logger.Debug(ctx, "context cache miss")
37+
}
3438
promise := sync.OnceValues(
3539
func() (T, error) {
3640
return errtrace.Wrap2(builder())
@@ -54,7 +58,9 @@ func (c *contextCache[T]) ctxWithAttributes(ctx context.Context, cacheKey string
5458
func ContextWithCache[T any](ctx context.Context) context.Context {
5559
cache, hasCache := ctx.Value(contextCacheCtxKey[T]{}).(*contextCache[T])
5660
if hasCache {
57-
logger.Debug(cache.ctxWithAttributes(ctx, ""), "context cache already exists")
61+
if verboseLogsEnabled(ctx) {
62+
logger.Debug(cache.ctxWithAttributes(ctx, ""), "context cache already exists")
63+
}
5864
return ctx
5965
}
6066
cache = &contextCache[T]{
@@ -67,7 +73,9 @@ func ContextWithCache[T any](ctx context.Context) context.Context {
6773
func GetFromContextCache[T any](ctx context.Context, cacheKey string, builder func() (T, error)) (T, error) {
6874
cache, hasCache := ctx.Value(contextCacheCtxKey[T]{}).(*contextCache[T])
6975
if !hasCache {
70-
logger.Debug(cache.ctxWithAttributes(ctx, ""), "context cache does not exists")
76+
if verboseLogsEnabled(ctx) {
77+
logger.Debug(cache.ctxWithAttributes(ctx, ""), "context cache does not exists")
78+
}
7179
return errtrace.Wrap2(builder())
7280
}
7381

@@ -80,13 +88,17 @@ func GetFromContextCache[T any](ctx context.Context, cacheKey string, builder fu
8088
func PutInContextCache[T any](ctx context.Context, cacheKey string, value T) {
8189
cache, hasCache := ctx.Value(contextCacheCtxKey[T]{}).(*contextCache[T])
8290
if !hasCache {
83-
logger.Debug(cache.ctxWithAttributes(ctx, ""), "context cache does not exists")
91+
if verboseLogsEnabled(ctx) {
92+
logger.Debug(cache.ctxWithAttributes(ctx, ""), "context cache does not exists")
93+
}
8494
return
8595
}
8696
ctx = cache.ctxWithAttributes(ctx, cacheKey)
8797
cache.lock.Lock()
8898
defer cache.lock.Unlock()
89-
logger.Debug(ctx, "context cache put")
99+
if verboseLogsEnabled(ctx) {
100+
logger.Debug(ctx, "context cache put")
101+
}
90102
cache.data[cacheKey] = func() (T, error) { //nolint:unparam // matches the cache signature
91103
return value, nil
92104
}

ctxcache/verbose_logs.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package ctxcache
2+
3+
import "context"
4+
5+
type verboseLogsCtxKey struct{}
6+
7+
func CtxWithVerboseLogs(ctx context.Context) context.Context {
8+
return context.WithValue(ctx, verboseLogsCtxKey{}, true)
9+
}
10+
11+
func CtxWithoutVerboseLogs(ctx context.Context) context.Context {
12+
return context.WithValue(ctx, verboseLogsCtxKey{}, false)
13+
}
14+
15+
func verboseLogsEnabled(ctx context.Context) bool {
16+
enabled, _ := ctx.Value(verboseLogsCtxKey{}).(bool)
17+
return enabled
18+
}

0 commit comments

Comments
 (0)