Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve cache #1866

Merged
merged 5 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tools/walletextension/cache/RistrettoCache.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ func (c *ristrettoCache) EvictShortLiving() {
c.lastEviction = time.Now()
}

func (c *ristrettoCache) IsEvicted(key any) bool {
func (c *ristrettoCache) IsEvicted(key any, ttl time.Duration) bool {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: could maybe call this originalTTL for clarity.

remainingTTL, notExpired := c.cache.GetTTL(key)
if !notExpired {
return true
}
cachedTime := time.Now().Add(-remainingTTL)
cachedTime := time.Now().Add(remainingTTL).Add(-ttl)
// ... LE...Cached...Now - Valid
// ... Cached...LE...Now - Evicted
return c.lastEviction.After(cachedTime)
Expand Down
2 changes: 1 addition & 1 deletion tools/walletextension/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

type Cache interface {
EvictShortLiving()
IsEvicted(key any) bool
IsEvicted(key any, ttl time.Duration) bool
Set(key []byte, value any, ttl time.Duration) bool
Get(key []byte) (value any, ok bool)
Remove(key []byte)
Expand Down
2 changes: 1 addition & 1 deletion tools/walletextension/rpcapi/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func withCache[R any](cache cache.Cache, cfg *CacheCfg, cacheKey []byte, onCache
}

cachedValue, foundInCache := cache.Get(cacheKey)
if foundInCache && !cache.IsEvicted(cacheKey) {
if foundInCache && !cache.IsEvicted(cacheKey, ttl) {
returnValue, ok := cachedValue.(*R)
if !ok {
return nil, fmt.Errorf("unexpected error. Invalid format cached. %v", cachedValue)
Expand Down
8 changes: 4 additions & 4 deletions tools/walletextension/rpcapi/wallet_extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

subscriptioncommon "github.com/ten-protocol/go-ten/go/common/subscription"

common2 "github.com/ten-protocol/go-ten/go/common"
tencommon "github.com/ten-protocol/go-ten/go/common"
"github.com/ten-protocol/go-ten/go/rpc"

"github.com/ten-protocol/go-ten/go/obsclient"
Expand Down Expand Up @@ -50,7 +50,7 @@ type Services struct {
}

type NewHeadNotifier interface {
onNewHead(header *common2.BatchHeader)
onNewHead(header *tencommon.BatchHeader)
}

func NewServices(hostAddrHTTP string, hostAddrWS string, storage storage.Storage, stopControl *stopcontrol.StopControl, version string, logger gethlog.Logger, config *common.Config) *Services {
Expand Down Expand Up @@ -110,14 +110,14 @@ func NewServices(hostAddrHTTP string, hostAddrWS string, storage storage.Storage
}

rpcClient := connectionObj.(rpc.Client)
ch := make(chan *common2.BatchHeader)
ch := make(chan *tencommon.BatchHeader)
clientSubscription, err := rpcClient.Subscribe(context.Background(), rpc.SubscribeNamespace, ch, rpc.SubscriptionTypeNewHeads)
if err != nil {
panic(fmt.Errorf("cannot subscribe to new heads to the backend %w", err))
}

services.backendNewHeadsSubscription = clientSubscription
services.NewHeadsService = subscriptioncommon.NewNewHeadsService(ch, true, logger, func(newHead *common2.BatchHeader) error {
services.NewHeadsService = subscriptioncommon.NewNewHeadsService(ch, true, logger, func(newHead *tencommon.BatchHeader) error {
services.Cache.EvictShortLiving()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed this on a previous review but could you alias that common2 import while you're here?

return nil
})
Expand Down
Loading