mirror of
https://github.com/grassrootseconomics/eth-tracker.git
synced 2026-05-17 10:15:21 +02:00
release: v1.0.0
This commit is contained in:
61
internal/cache/cache.go
vendored
Normal file
61
internal/cache/cache.go
vendored
Normal file
@@ -0,0 +1,61 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/grassrootseconomics/celo-tracker/internal/chain"
|
||||
)
|
||||
|
||||
type (
|
||||
Cache interface {
|
||||
Purge() error
|
||||
Exists(string) bool
|
||||
Add(string, bool)
|
||||
Remove(string)
|
||||
IsWatchableIndex(string) bool
|
||||
Size() int
|
||||
}
|
||||
CacheOpts struct {
|
||||
Chain chain.Chain
|
||||
Logg *slog.Logger
|
||||
CacheType string
|
||||
Blacklist []string
|
||||
Registries []string
|
||||
Watchlist []string
|
||||
}
|
||||
)
|
||||
|
||||
func New(o CacheOpts) (Cache, error) {
|
||||
var cache Cache
|
||||
|
||||
switch o.CacheType {
|
||||
case "map":
|
||||
cache = NewMapCache()
|
||||
default:
|
||||
cache = NewMapCache()
|
||||
o.Logg.Warn("invalid cache type, using default type (map)")
|
||||
}
|
||||
|
||||
geSmartContracts, err := o.Chain.Provider().GetGESmartContracts(
|
||||
context.Background(),
|
||||
o.Registries,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cache could not bootstrap GE smart contracts: err %v", err)
|
||||
}
|
||||
|
||||
for k, v := range geSmartContracts {
|
||||
cache.Add(k, v)
|
||||
}
|
||||
for _, address := range o.Watchlist {
|
||||
cache.Add(address, false)
|
||||
}
|
||||
for _, address := range o.Blacklist {
|
||||
cache.Remove(address)
|
||||
}
|
||||
o.Logg.Info("cache bootstrap complete", "cached_addresses", cache.Size())
|
||||
|
||||
return cache, nil
|
||||
}
|
||||
48
internal/cache/xmap.go
vendored
Normal file
48
internal/cache/xmap.go
vendored
Normal file
@@ -0,0 +1,48 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"github.com/puzpuzpuz/xsync/v3"
|
||||
)
|
||||
|
||||
type mapCache struct {
|
||||
xmap *xsync.Map
|
||||
}
|
||||
|
||||
func NewMapCache() Cache {
|
||||
return &mapCache{
|
||||
xmap: xsync.NewMap(),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *mapCache) Purge() error {
|
||||
c.xmap.Clear()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *mapCache) Exists(key string) bool {
|
||||
_, ok := c.xmap.Load(key)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (c *mapCache) Add(key string, value bool) {
|
||||
c.xmap.Store(key, value)
|
||||
}
|
||||
|
||||
func (c *mapCache) Remove(key string) {
|
||||
c.xmap.Delete(key)
|
||||
}
|
||||
|
||||
func (c *mapCache) Size() int {
|
||||
return c.xmap.Size()
|
||||
}
|
||||
|
||||
func (c *mapCache) IsWatchableIndex(key string) bool {
|
||||
watchable, ok := c.xmap.Load(key)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
watchableBool, ok := watchable.(bool)
|
||||
if !ok {
|
||||
}
|
||||
return watchableBool
|
||||
}
|
||||
Reference in New Issue
Block a user