eth-tracker/internal/cache/cache.go
Mohamed Sohail 天明 efc84970cc
refactor: Decouple router and handlers (#31)
* refactor: decouple router to allow adding custom log and input data handlers

* feat: refactor handlers

* devops: update service build args

* chore: cleanup unecessary files/code
2024-09-18 17:35:57 +03:00

44 lines
718 B
Go

package cache
import (
"context"
"log/slog"
)
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 {
Logg *slog.Logger
RedisDSN string
CacheType string
}
)
func New(o CacheOpts) (Cache, error) {
var cache Cache
switch o.CacheType {
case "map":
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)")
}
return cache, nil
}