Change cache logic
This commit is contained in:
1 parent
2347591c69
commit
b59edc4b6b
8 files changed
+63
-41
No files matched your search
@@ -3,8 +3,10 @@ package nodecache
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/app"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/app/logger"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/cache"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/node/nodespace"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/ocache"
|
||||
"time"
|
||||
)
|
||||
@@ -12,9 +14,45 @@ import (
|
||||
var log = logger.NewNamed("treecache")
|
||||
var ErrCacheObjectWithoutTree = errors.New("cache object contains no tree")
|
||||
|
||||
type ctxKey int
|
||||
|
||||
const spaceKey ctxKey = 0
|
||||
|
||||
type treeCache struct {
|
||||
gcttl int
|
||||
cache ocache.OCache
|
||||
gcttl int
|
||||
cache ocache.OCache
|
||||
nodeService nodespace.Service
|
||||
}
|
||||
|
||||
func (c *treeCache) Run(ctx context.Context) (err error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *treeCache) Close(ctx context.Context) (err error) {
|
||||
return c.cache.Close()
|
||||
}
|
||||
|
||||
func (c *treeCache) Init(a *app.App) (err error) {
|
||||
c.nodeService = a.MustComponent(nodespace.CName).(nodespace.Service)
|
||||
c.cache = ocache.New(
|
||||
func(ctx context.Context, id string) (value ocache.Object, err error) {
|
||||
spaceId := ctx.Value(spaceKey).(string)
|
||||
space, err := c.nodeService.GetSpace(ctx, spaceId)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return space.BuildTree(ctx, id, nil)
|
||||
},
|
||||
ocache.WithLogger(log.Sugar()),
|
||||
ocache.WithGCPeriod(time.Minute),
|
||||
ocache.WithTTL(time.Duration(c.gcttl)*time.Second),
|
||||
ocache.WithRefCounter(false),
|
||||
)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *treeCache) Name() (name string) {
|
||||
return cache.CName
|
||||
}
|
||||
|
||||
func NewNodeCache(ttl int) cache.TreeCache {
|
||||
@@ -23,24 +61,9 @@ func NewNodeCache(ttl int) cache.TreeCache {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *treeCache) SetBuildFunc(buildFunc cache.BuildFunc) {
|
||||
c.cache = ocache.New(
|
||||
func(ctx context.Context, id string) (value ocache.Object, err error) {
|
||||
return buildFunc(ctx, id, nil)
|
||||
},
|
||||
ocache.WithLogger(log.Sugar()),
|
||||
ocache.WithGCPeriod(time.Minute),
|
||||
ocache.WithTTL(time.Duration(c.gcttl)*time.Second),
|
||||
ocache.WithRefCounter(false),
|
||||
)
|
||||
}
|
||||
|
||||
func (c *treeCache) Close() (err error) {
|
||||
return c.cache.Close()
|
||||
}
|
||||
|
||||
func (c *treeCache) GetTree(ctx context.Context, id string) (res cache.TreeResult, err error) {
|
||||
func (c *treeCache) GetTree(ctx context.Context, spaceId, id string) (res cache.TreeResult, err error) {
|
||||
var cacheRes ocache.Object
|
||||
ctx = context.WithValue(ctx, spaceKey, spaceId)
|
||||
cacheRes, err = c.cache.Get(ctx, id)
|
||||
if err != nil {
|
||||
return cache.TreeResult{}, err
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/storage"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/rpc/server"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/config"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/node/nodespace/nodecache"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/ocache"
|
||||
"time"
|
||||
)
|
||||
@@ -40,7 +39,7 @@ func (s *service) Init(a *app.App) (err error) {
|
||||
s.spaceStorageProvider = a.MustComponent(storage.CName).(storage.SpaceStorageProvider)
|
||||
s.spaceCache = ocache.New(
|
||||
func(ctx context.Context, id string) (value ocache.Object, err error) {
|
||||
return s.commonSpace.GetSpace(ctx, id, nodecache.NewNodeCache(s.conf.GCTTL))
|
||||
return s.commonSpace.GetSpace(ctx, id)
|
||||
},
|
||||
ocache.WithLogger(log.Sugar()),
|
||||
ocache.WithGCPeriod(time.Minute),
|
||||
|
||||
Reference in new issue
Block a user