Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions ctxcache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ func (c *contextCache[T]) get(ctx context.Context, cacheKey string, builder func
c.lock.Lock()
defer c.lock.Unlock()
if cached, exists := c.data[cacheKey]; exists {
logger.Debug(ctx, "context cache hit")
if verboseLogsEnabled(ctx) {
logger.Debug(ctx, "context cache hit")
}
return cached
}
logger.Debug(ctx, "context cache miss")
if verboseLogsEnabled(ctx) {
logger.Debug(ctx, "context cache miss")
}
promise := sync.OnceValues(
func() (T, error) {
return errtrace.Wrap2(builder())
Expand All @@ -54,7 +58,9 @@ func (c *contextCache[T]) ctxWithAttributes(ctx context.Context, cacheKey string
func ContextWithCache[T any](ctx context.Context) context.Context {
cache, hasCache := ctx.Value(contextCacheCtxKey[T]{}).(*contextCache[T])
if hasCache {
logger.Debug(cache.ctxWithAttributes(ctx, ""), "context cache already exists")
if verboseLogsEnabled(ctx) {
logger.Debug(cache.ctxWithAttributes(ctx, ""), "context cache already exists")
}
return ctx
}
cache = &contextCache[T]{
Expand All @@ -67,7 +73,9 @@ func ContextWithCache[T any](ctx context.Context) context.Context {
func GetFromContextCache[T any](ctx context.Context, cacheKey string, builder func() (T, error)) (T, error) {
cache, hasCache := ctx.Value(contextCacheCtxKey[T]{}).(*contextCache[T])
if !hasCache {
logger.Debug(cache.ctxWithAttributes(ctx, ""), "context cache does not exists")
if verboseLogsEnabled(ctx) {
logger.Debug(cache.ctxWithAttributes(ctx, ""), "context cache does not exists")
}
return errtrace.Wrap2(builder())
}

Expand All @@ -80,13 +88,17 @@ func GetFromContextCache[T any](ctx context.Context, cacheKey string, builder fu
func PutInContextCache[T any](ctx context.Context, cacheKey string, value T) {
cache, hasCache := ctx.Value(contextCacheCtxKey[T]{}).(*contextCache[T])
if !hasCache {
logger.Debug(cache.ctxWithAttributes(ctx, ""), "context cache does not exists")
if verboseLogsEnabled(ctx) {
logger.Debug(cache.ctxWithAttributes(ctx, ""), "context cache does not exists")
}
return
}
ctx = cache.ctxWithAttributes(ctx, cacheKey)
cache.lock.Lock()
defer cache.lock.Unlock()
logger.Debug(ctx, "context cache put")
if verboseLogsEnabled(ctx) {
logger.Debug(ctx, "context cache put")
}
cache.data[cacheKey] = func() (T, error) { //nolint:unparam // matches the cache signature
return value, nil
}
Expand Down
18 changes: 18 additions & 0 deletions ctxcache/verbose_logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ctxcache

import "context"

type verboseLogsCtxKey struct{}

func CtxWithVerboseLogs(ctx context.Context) context.Context {
return context.WithValue(ctx, verboseLogsCtxKey{}, true)
}

func CtxWithoutVerboseLogs(ctx context.Context) context.Context {
return context.WithValue(ctx, verboseLogsCtxKey{}, false)
}

func verboseLogsEnabled(ctx context.Context) bool {
enabled, _ := ctx.Value(verboseLogsCtxKey{}).(bool)
return enabled
}
Loading