1
0
Fork 0
mirror of https://github.com/anyproto/anytype-heart.git synced 2025-06-10 18:10:49 +09:00

GO-3579: Object store: use hashicorp's lru cache

This commit is contained in:
Sergey 2024-06-14 15:35:56 +02:00
parent 6c46d3e360
commit 9d912529bd
No known key found for this signature in database
GPG key ID: 3B6BEF79160221C6
5 changed files with 12 additions and 14 deletions

2
go.mod
View file

@ -174,7 +174,7 @@ require (
github.com/gorilla/css v1.0.0 // indirect
github.com/gosimple/unidecode v1.0.1 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.5 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/holiman/uint256 v1.2.4 // indirect

2
go.sum
View file

@ -554,6 +554,8 @@ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hashicorp/golang-lru/v2 v2.0.5 h1:wW7h1TG88eUIJ2i69gaE3uNVtEPIagzhGvHgwfx2Vm4=
github.com/hashicorp/golang-lru/v2 v2.0.5/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=

View file

@ -21,7 +21,7 @@ func (s *dsObjectStore) DeleteDetails(ids ...string) error {
for _, chunk := range lo.Chunk(ids, 100) {
err := s.updateTxn(func(txn *badger.Txn) error {
for _, id := range chunk {
s.cache.Del(pagesDetailsBase.ChildString(id).Bytes())
s.cache.Remove(pagesDetailsBase.ChildString(id).String())
for _, key := range []ds.Key{
pagesDetailsBase.ChildString(id),

View file

@ -11,9 +11,9 @@ import (
"github.com/anyproto/any-sync/app"
"github.com/anyproto/any-sync/coordinator/coordinatorproto"
"github.com/dgraph-io/badger/v4"
"github.com/dgraph-io/ristretto"
"github.com/gogo/protobuf/proto"
"github.com/gogo/protobuf/types"
lru "github.com/hashicorp/golang-lru/v2"
ds "github.com/ipfs/go-datastore"
"github.com/anyproto/anytype-heart/core/domain"
@ -87,11 +87,7 @@ func (s *dsObjectStore) Init(a *app.App) (err error) {
}
func (s *dsObjectStore) initCache() error {
cache, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 10_000_000,
MaxCost: 100_000_000,
BufferItems: 64,
})
cache, err := lru.New[string, *model.ObjectDetails](50000)
if err != nil {
return fmt.Errorf("init cache: %w", err)
}
@ -194,7 +190,7 @@ var ErrNotAnObject = fmt.Errorf("not an object")
type dsObjectStore struct {
sourceService SourceDetailsFromID
cache *ristretto.Cache
cache *lru.Cache[string, *model.ObjectDetails]
db *badger.DB
fts ftsearch.FTSearch
@ -365,8 +361,8 @@ func (s *dsObjectStore) FTSearch() ftsearch.FTSearch {
func (s *dsObjectStore) extractDetailsFromItem(it *badger.Item) (*model.ObjectDetails, error) {
key := it.Key()
if v, ok := s.cache.Get(key); ok {
return v.(*model.ObjectDetails), nil
if v, ok := s.cache.Get(string(key)); ok {
return v, nil
}
return s.unmarshalDetailsFromItem(it)
}
@ -379,7 +375,7 @@ func (s *dsObjectStore) unmarshalDetailsFromItem(it *badger.Item) (*model.Object
if err != nil {
return fmt.Errorf("unmarshal details: %w", err)
}
s.cache.Set(it.Key(), details, int64(details.Size()))
s.cache.Add(string(it.Key()), details)
return nil
})
if err != nil {

View file

@ -47,7 +47,7 @@ func (s *dsObjectStore) UpdateObjectDetails(id string, details *types.Struct) er
if txErr != nil {
return txErr
}
s.cache.Set(key, newDetails, int64(newDetails.Size()))
s.cache.Add(string(key), newDetails)
return nil
}
@ -152,7 +152,7 @@ func (s *dsObjectStore) ModifyObjectDetails(id string, proc func(details *types.
}); err != nil {
return err
}
s.cache.Set(key, payload, int64(payload.Size()))
s.cache.Add(string(key), payload)
return nil
}