Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: use map instead of fastcache (#4089)

rebelice9f7d1d998e9b
diff --git a/go.mod b/go.mod
index 6adcefc724..a98a296a17 100644
--- a/go.mod
+++ b/go.mod
@@ -4,7 +4,6 @@ go 1.19
require (
github.com/ClickHouse/clickhouse-go/v2 v2.3.0
- github.com/VictoriaMetrics/fastcache v1.12.0
github.com/aws/aws-sdk-go-v2 v1.17.1
github.com/aws/aws-sdk-go-v2/config v1.17.10
github.com/aws/aws-sdk-go-v2/credentials v1.12.23
diff --git a/go.sum b/go.sum
index 208bd4c4e2..88118cd1fa 100644
--- a/go.sum
+++ b/go.sum
@@ -73,8 +73,6 @@ github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6Xge
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
-github.com/VictoriaMetrics/fastcache v1.12.0 h1:vnVi/y9yKDcD9akmc4NqAoqgQhJrOwUF+j9LTgn4QDE=
-github.com/VictoriaMetrics/fastcache v1.12.0/go.mod h1:tjiYeEfYXCqacuvYw/7UoDIeJaNxq6132xHICNP77w8=
github.com/VividCortex/ewma v1.1.1 h1:MnEK4VOv6n0RSY4vtRe3h11qjxL3+t0B8yOL8iMXdcM=
github.com/agiledragon/gomonkey/v2 v2.3.1/go.mod h1:ap1AmDzcVOAz1YpeJ3TCzIgstoaWLA6jbbgxfB4w2iY=
github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
@@ -84,8 +82,6 @@ github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRF
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1581 h1:Q/yk4z/cHUVZfgTqtD09qeYBxHwshQAjVRX73qs8UH0=
-github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156 h1:eMwmnE/GDgah4HI848JfFxHt+iPb26b4zyfspmqY0/8=
-github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM=
github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY=
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/antihax/optional v0.0.0-20180407024304-ca021399b1a6/go.mod h1:V8iCPQYkqmusNa815XgQio277wI47sdRh1dUOLdyC6Q=
@@ -951,7 +947,6 @@ golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
-golang.org/x/sys v0.0.0-20220405052023-b1e9470b6e64/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
diff --git a/store/cache.go b/store/cache.go
index 2954b528ca..1da5b7bc9d 100644
--- a/store/cache.go
+++ b/store/cache.go
@@ -2,18 +2,13 @@ package store
import (
"bytes"
- "encoding/binary"
"encoding/gob"
+ "fmt"
+ "sync"
- "github.com/VictoriaMetrics/fastcache"
"github.com/pkg/errors"
)
-var (
- // 32 MiB.
- cacheSize = 1024 * 1024 * 32
-)
-
// cacheNamespace is the type of a cache.
type cacheNamespace string
@@ -50,51 +45,60 @@ const (
// CacheService implements a cache.
type CacheService struct {
- cache *fastcache.Cache
+ sync.Mutex
+ cache map[string][]byte
}
// newCacheService creates a cache service.
func newCacheService() *CacheService {
return &CacheService{
- cache: fastcache.New(cacheSize),
+ cache: make(map[string][]byte),
}
}
// FindCache finds the value in cache.
func (s *CacheService) FindCache(namespace cacheNamespace, id int, entry interface{}) (bool, error) {
- buf1 := []byte{0, 0, 0, 0, 0, 0, 0, 0}
- binary.LittleEndian.PutUint64(buf1, uint64(id))
+ key := generateKey(namespace, id)
- buf2, has := s.cache.HasGet(nil, append([]byte(namespace), buf1...))
- if has {
- dec := gob.NewDecoder(bytes.NewReader(buf2))
+ s.Lock()
+ defer s.Unlock()
+ value, exists := s.cache[key]
+ if exists {
+ dec := gob.NewDecoder(bytes.NewReader(value))
if err := dec.Decode(entry); err != nil {
return false, errors.Wrapf(err, "failed to decode entry for cache namespace: %s", namespace)
}
return true, nil
}
-
return false, nil
}
// UpsertCache upserts the value to cache.
func (s *CacheService) UpsertCache(namespace cacheNamespace, id int, entry interface{}) error {
- buf1 := []byte{0, 0, 0, 0, 0, 0, 0, 0}
- binary.LittleEndian.PutUint64(buf1, uint64(id))
+ key := generateKey(namespace, id)
- var buf2 bytes.Buffer
- enc := gob.NewEncoder(&buf2)
+ var buf bytes.Buffer
+ enc := gob.NewEncoder(&buf)
if err := enc.Encode(entry); err != nil {
return errors.Wrapf(err, "failed to encode entry for cache namespace: %s", namespace)
}
- s.cache.Set(append([]byte(namespace), buf1...), buf2.Bytes())
+
+ s.Lock()
+ defer s.Unlock()
+ s.cache[key] = buf.Bytes()
return nil
}
// DeleteCache deletes the key from cache.
func (s *CacheService) DeleteCache(namespace cacheNamespace, id int) {
- buf1 := []byte{0, 0, 0, 0, 0, 0, 0, 0}
- binary.LittleEndian.PutUint64(buf1, uint64(id))
- s.cache.Del(append([]byte(namespace), buf1...))
+ key := generateKey(namespace, id)
+
+ s.Lock()
+ defer s.Unlock()
+ delete(s.cache, key)
+}
+
+func generateKey(namespace cacheNamespace, id int) string {
+ return fmt.Sprintf("%s%d", namespace, id)
}