Skip to content

Commit

Permalink
eth/filters: add generic LRU implementation (ethereum#26162)
Browse files Browse the repository at this point in the history
  • Loading branch information
JukLee0ira authored and gzliudan committed Aug 3, 2024
1 parent 09d3ede commit 17dafcc
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions eth/filters/filter_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ import (

ethereum "github.com/XinFinOrg/XDPoSChain"
"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/common/lru"
"github.com/XinFinOrg/XDPoSChain/core"
"github.com/XinFinOrg/XDPoSChain/core/bloombits"
"github.com/XinFinOrg/XDPoSChain/core/types"
"github.com/XinFinOrg/XDPoSChain/ethdb"
"github.com/XinFinOrg/XDPoSChain/event"
"github.com/XinFinOrg/XDPoSChain/log"
"github.com/XinFinOrg/XDPoSChain/rpc"
lru "github.com/hashicorp/golang-lru"
)

// Config represents the configuration of the filter system.
Expand Down Expand Up @@ -74,21 +74,16 @@ type Backend interface {
// FilterSystem holds resources shared by all filters.
type FilterSystem struct {
backend Backend
logsCache *lru.Cache
logsCache *lru.Cache[common.Hash, [][]*types.Log]
cfg *Config
}

// NewFilterSystem creates a filter system.
func NewFilterSystem(backend Backend, config Config) *FilterSystem {
config = config.withDefaults()

cache, err := lru.New(config.LogCacheSize)
if err != nil {
panic(err)
}
return &FilterSystem{
backend: backend,
logsCache: cache,
logsCache: lru.NewCache[common.Hash, [][]*types.Log](config.LogCacheSize),
cfg: &config,
}
}
Expand All @@ -97,7 +92,7 @@ func NewFilterSystem(backend Backend, config Config) *FilterSystem {
func (sys *FilterSystem) cachedGetLogs(ctx context.Context, blockHash common.Hash, number uint64) ([][]*types.Log, error) {
cached, ok := sys.logsCache.Get(blockHash)
if ok {
return cached.([][]*types.Log), nil
return cached, nil
}

logs, err := sys.backend.GetLogs(ctx, blockHash, number)
Expand Down

0 comments on commit 17dafcc

Please sign in to comment.