eth-tracker/internal/cache/cache.go
Mohammed Sohail 75b402bbf6
feat: self-bootstrapping tracker, jetstream updates
* This removes redis as a hard dependency
* add profiler utils (temp)
2024-10-31 14:52:51 +03:00

61 lines
1.0 KiB
Go

package cache
import (
"context"
"log/slog"
"github.com/grassrootseconomics/eth-tracker/internal/chain"
)
type (
Cache interface {
Add(context.Context, string) error
Remove(context.Context, string) error
Exists(context.Context, ...string) (bool, error)
Size(context.Context) (int64, error)
}
CacheOpts struct {
RedisDSN string
CacheType string
Registries []string
Watchlist []string
Blacklist []string
Chain chain.Chain
Logg *slog.Logger
}
)
func New(o CacheOpts) (Cache, error) {
var cache Cache
switch o.CacheType {
case "internal":
cache = NewMapCache()
case "redis":
redisCache, err := NewRedisCache(redisOpts{
DSN: o.RedisDSN,
})
if err != nil {
return nil, err
}
cache = redisCache
default:
cache = NewMapCache()
o.Logg.Warn("invalid cache type, using default type (map)")
}
if err := bootstrapCache(
o.Chain,
cache,
o.Registries,
o.Watchlist,
o.Blacklist,
o.Logg,
); err != nil {
return cache, err
}
return cache, nil
}