|
|
@ -7,8 +7,10 @@ import ( |
|
|
|
"time" |
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/grafana/authlib/cache" |
|
|
|
"github.com/grafana/authlib/cache" |
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/attribute" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log" |
|
|
|
"github.com/grafana/grafana/pkg/infra/log" |
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/infra/tracing" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
func userIdentifierCacheKey(namespace, userUID string) string { |
|
|
|
func userIdentifierCacheKey(namespace, userUID string) string { |
|
|
@ -50,20 +52,24 @@ type cacheWrap[T any] interface { |
|
|
|
type cacheWrapImpl[T any] struct { |
|
|
|
type cacheWrapImpl[T any] struct { |
|
|
|
cache cache.Cache |
|
|
|
cache cache.Cache |
|
|
|
logger log.Logger |
|
|
|
logger log.Logger |
|
|
|
|
|
|
|
tracer tracing.Tracer |
|
|
|
ttl time.Duration |
|
|
|
ttl time.Duration |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// cacheWrap is a wrapper around the authlib Cache that provides typed Get and Set methods
|
|
|
|
// cacheWrap is a wrapper around the authlib Cache that provides typed Get and Set methods
|
|
|
|
// it handles encoding/decoding for a specific type.
|
|
|
|
// it handles encoding/decoding for a specific type.
|
|
|
|
func newCacheWrap[T any](cache cache.Cache, logger log.Logger, ttl time.Duration) cacheWrap[T] { |
|
|
|
func newCacheWrap[T any](cache cache.Cache, logger log.Logger, tracer tracing.Tracer, ttl time.Duration) cacheWrap[T] { |
|
|
|
if ttl == 0 { |
|
|
|
if ttl == 0 { |
|
|
|
logger.Info("cache ttl is 0, using noop cache") |
|
|
|
logger.Info("cache ttl is 0, using noop cache") |
|
|
|
return &noopCache[T]{} |
|
|
|
return &noopCache[T]{} |
|
|
|
} |
|
|
|
} |
|
|
|
return &cacheWrapImpl[T]{cache: cache, logger: logger, ttl: ttl} |
|
|
|
return &cacheWrapImpl[T]{cache: cache, logger: logger, tracer: tracer, ttl: ttl} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (c *cacheWrapImpl[T]) Get(ctx context.Context, key string) (T, bool) { |
|
|
|
func (c *cacheWrapImpl[T]) Get(ctx context.Context, key string) (T, bool) { |
|
|
|
|
|
|
|
ctx, span := c.tracer.Start(ctx, "cacheWrap.Get") |
|
|
|
|
|
|
|
defer span.End() |
|
|
|
|
|
|
|
span.SetAttributes(attribute.Bool("hit", false)) |
|
|
|
logger := c.logger.FromContext(ctx) |
|
|
|
logger := c.logger.FromContext(ctx) |
|
|
|
|
|
|
|
|
|
|
|
var value T |
|
|
|
var value T |
|
|
@ -81,10 +87,13 @@ func (c *cacheWrapImpl[T]) Get(ctx context.Context, key string) (T, bool) { |
|
|
|
return value, false |
|
|
|
return value, false |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
span.SetAttributes(attribute.Bool("hit", true)) |
|
|
|
return value, true |
|
|
|
return value, true |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (c *cacheWrapImpl[T]) Set(ctx context.Context, key string, value T) { |
|
|
|
func (c *cacheWrapImpl[T]) Set(ctx context.Context, key string, value T) { |
|
|
|
|
|
|
|
ctx, span := c.tracer.Start(ctx, "cacheWrap.Set") |
|
|
|
|
|
|
|
defer span.End() |
|
|
|
logger := c.logger.FromContext(ctx) |
|
|
|
logger := c.logger.FromContext(ctx) |
|
|
|
|
|
|
|
|
|
|
|
data, err := json.Marshal(value) |
|
|
|
data, err := json.Marshal(value) |
|
|
|