mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Authn: Add caching for anon service (#63521)
add local cache to protect multiple writes to DB cache
This commit is contained in:
parent
45e478182b
commit
1e84d5d93c
@ -9,6 +9,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/infra/localcache"
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/infra/network"
|
"github.com/grafana/grafana/pkg/infra/network"
|
||||||
"github.com/grafana/grafana/pkg/infra/remotecache"
|
"github.com/grafana/grafana/pkg/infra/remotecache"
|
||||||
@ -40,12 +41,14 @@ func (a *AnonSession) Key() (string, error) {
|
|||||||
type AnonSessionService struct {
|
type AnonSessionService struct {
|
||||||
remoteCache remotecache.CacheStorage
|
remoteCache remotecache.CacheStorage
|
||||||
log log.Logger
|
log log.Logger
|
||||||
|
localCache *localcache.CacheService
|
||||||
}
|
}
|
||||||
|
|
||||||
func ProvideAnonymousSessionService(remoteCache remotecache.CacheStorage, usageStats usagestats.Service) *AnonSessionService {
|
func ProvideAnonymousSessionService(remoteCache remotecache.CacheStorage, usageStats usagestats.Service) *AnonSessionService {
|
||||||
a := &AnonSessionService{
|
a := &AnonSessionService{
|
||||||
remoteCache: remoteCache,
|
remoteCache: remoteCache,
|
||||||
log: log.New("anonymous-session-service"),
|
log: log.New("anonymous-session-service"),
|
||||||
|
localCache: localcache.New(29*time.Minute, 15*time.Minute),
|
||||||
}
|
}
|
||||||
|
|
||||||
usageStats.RegisterMetricsFunc(a.UsageStatFn)
|
usageStats.RegisterMetricsFunc(a.UsageStatFn)
|
||||||
@ -87,5 +90,11 @@ func (a *AnonSessionService) TagSession(ctx context.Context, httpReq *http.Reque
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if _, ok := a.localCache.Get(key); ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
a.localCache.SetDefault(key, struct{}{})
|
||||||
|
|
||||||
return a.remoteCache.Set(ctx, key, key, thirtyDays)
|
return a.remoteCache.Set(ctx, key, key, thirtyDays)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user