eth-tracker/internal/cache/cache.go

63 lines
1.2 KiB
Go
Raw Permalink Normal View History

2024-09-05 08:48:59 +02:00
package cache
import (
"context"
"log/slog"
"github.com/grassrootseconomics/eth-tracker/internal/chain"
2024-09-05 08:48:59 +02:00
)
type (
Cache interface {
Add(context.Context, string) error
Remove(context.Context, string) error
Exists(context.Context, string) (bool, error)
ExistsNetwork(context.Context, string, ...string) (bool, error)
2024-09-05 08:48:59 +02:00
Size(context.Context) (int64, error)
}
CacheOpts struct {
RedisDSN string
CacheType string
Registries []string
Watchlist []string
Blacklist []string
Chain chain.Chain
Logg *slog.Logger
2024-09-05 08:48:59 +02:00
}
)
func New(o CacheOpts) (Cache, error) {
o.Logg.Info("initializing cache", "registries", o.Registries, "watchlist", o.Watchlist, "blacklist", o.Blacklist)
2024-09-05 08:48:59 +02:00
var cache Cache
switch o.CacheType {
case "internal":
2024-09-05 08:48:59 +02:00
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
}
2024-09-05 08:48:59 +02:00
return cache, nil
}