mirror of
https://github.com/grafana/grafana.git
synced 2025-01-10 08:03:58 -06:00
Use fnv64 for InmemCacheService (#58468)
This commit is contained in:
parent
4d2be7a277
commit
72275e97d2
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user