From 72275e97d281f6b4dfb2cd1188db1e7ec5cbb198 Mon Sep 17 00:00:00 2001 From: George Robinson Date: Tue, 8 Nov 2022 22:05:15 +0000 Subject: [PATCH] Use fnv64 for InmemCacheService (#58468) --- pkg/services/screenshot/cache.go | 6 +++--- pkg/services/screenshot/option.go | 12 ++++++++++++ pkg/services/screenshot/option_test.go | 24 ++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/pkg/services/screenshot/cache.go b/pkg/services/screenshot/cache.go index 83447efbc3b..516cc886dfa 100644 --- a/pkg/services/screenshot/cache.go +++ b/pkg/services/screenshot/cache.go @@ -2,7 +2,7 @@ package screenshot import ( "context" - "fmt" + "encoding/base64" "time" gocache "github.com/patrickmn/go-cache" @@ -46,7 +46,7 @@ func NewInmemCacheService(expiration time.Duration, r prometheus.Registerer) Cac } func (s *InmemCacheService) Get(_ context.Context, opts ScreenshotOptions) (*Screenshot, bool) { - k := fmt.Sprintf("%s-%d-%s", opts.DashboardUID, opts.PanelID, opts.Theme) + k := base64.StdEncoding.EncodeToString(opts.Hash()) if v, ok := s.cache.Get(k); ok { defer s.cacheHits.Inc() return v.(*Screenshot), true @@ -56,7 +56,7 @@ func (s *InmemCacheService) Get(_ context.Context, opts ScreenshotOptions) (*Scr } func (s *InmemCacheService) Set(_ context.Context, opts ScreenshotOptions, screenshot *Screenshot) error { - k := fmt.Sprintf("%s-%d-%s", opts.DashboardUID, opts.PanelID, opts.Theme) + k := base64.StdEncoding.EncodeToString(opts.Hash()) s.cache.Set(k, screenshot, 0) return nil } diff --git a/pkg/services/screenshot/option.go b/pkg/services/screenshot/option.go index 063b1d850d5..e84d2ed3a55 100644 --- a/pkg/services/screenshot/option.go +++ b/pkg/services/screenshot/option.go @@ -1,6 +1,8 @@ package screenshot import ( + "hash/fnv" + "strconv" "time" "github.com/grafana/grafana/pkg/models" @@ -41,3 +43,13 @@ func (s ScreenshotOptions) SetDefaults() ScreenshotOptions { } return s } + +func (s ScreenshotOptions) Hash() []byte { + h := fnv.New64() + _, _ = h.Write([]byte(s.DashboardUID)) + _, _ = h.Write([]byte(strconv.FormatInt(s.PanelID, 10))) + _, _ = h.Write([]byte(strconv.FormatInt(int64(s.Width), 10))) + _, _ = h.Write([]byte(strconv.FormatInt(int64(s.Height), 10))) + _, _ = h.Write([]byte(s.Theme)) + return h.Sum(nil) +} diff --git a/pkg/services/screenshot/option_test.go b/pkg/services/screenshot/option_test.go index f2c8c542fff..50552c1431b 100644 --- a/pkg/services/screenshot/option_test.go +++ b/pkg/services/screenshot/option_test.go @@ -53,3 +53,27 @@ func TestScreenshotOptions(t *testing.T) { Timeout: DefaultTimeout + 1, }, o) } + +func TestScreenshotOptions_Hash(t *testing.T) { + o := ScreenshotOptions{} + assert.Equal(t, []byte{0xd9, 0x83, 0x82, 0x18, 0x6c, 0x3d, 0x7d, 0x47}, o.Hash()) + + o = o.SetDefaults() + assert.Equal(t, []byte{0x6, 0x7, 0x97, 0x6, 0x53, 0xf, 0x8b, 0xf1}, o.Hash()) + + o.Width = 100 + o = o.SetDefaults() + assert.Equal(t, []byte{0x25, 0x50, 0xb4, 0x4b, 0x43, 0xcd, 0x3, 0x49}, o.Hash()) + + o.Height = 100 + o = o.SetDefaults() + assert.Equal(t, []byte{0x51, 0xe2, 0x6f, 0x2c, 0x62, 0x7b, 0x3b, 0xc5}, o.Hash()) + + o.Theme = "Not a theme" + o = o.SetDefaults() + assert.Equal(t, []byte{0x51, 0xe2, 0x6f, 0x2c, 0x62, 0x7b, 0x3b, 0xc5}, o.Hash()) + + // the timeout should not change the sum + o.Timeout = DefaultTimeout + 1 + assert.Equal(t, []byte{0x51, 0xe2, 0x6f, 0x2c, 0x62, 0x7b, 0x3b, 0xc5}, o.Hash()) +}