@@ -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
5458func 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 {
6773func 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
8088func 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 }
0 commit comments