2024-02-06 16:00:45 -08:00
|
|
|
package builder
|
|
|
|
|
|
|
|
import (
|
2024-03-06 06:59:09 -08:00
|
|
|
"time"
|
|
|
|
|
2024-02-06 16:00:45 -08:00
|
|
|
"github.com/dgraph-io/ristretto"
|
|
|
|
"github.com/eko/gocache/v3/cache"
|
2024-03-06 06:59:09 -08:00
|
|
|
"github.com/eko/gocache/v3/metrics"
|
2024-02-06 16:00:45 -08:00
|
|
|
"github.com/eko/gocache/v3/store"
|
2024-03-06 06:59:09 -08:00
|
|
|
"github.com/wormhole-foundation/wormhole-explorer/fly/config"
|
2024-02-06 16:00:45 -08:00
|
|
|
"github.com/wormhole-foundation/wormhole-explorer/fly/deduplicator"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2024-03-06 06:59:09 -08:00
|
|
|
func NewCache[T any](name string, numKeys, maxCost int64) (cache.CacheInterface[T], error) {
|
2024-02-06 16:00:45 -08:00
|
|
|
c, err := ristretto.NewCache(&ristretto.Config{
|
2024-03-06 06:59:09 -08:00
|
|
|
NumCounters: numKeys, // Num keys to track frequency of (1000).
|
|
|
|
MaxCost: maxCost * (1 << 20), // Maximum cost of cache (10 MB).
|
|
|
|
BufferItems: 64, // Number of keys per Get buffer.
|
2024-02-06 16:00:45 -08:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
store := store.NewRistretto(c)
|
2024-03-06 06:59:09 -08:00
|
|
|
return cache.NewMetric[T](
|
|
|
|
metrics.NewPrometheus(name),
|
|
|
|
cache.New[T](store),
|
|
|
|
), nil
|
2024-02-06 16:00:45 -08:00
|
|
|
}
|
|
|
|
|
2024-03-06 06:59:09 -08:00
|
|
|
func NewDeduplicator(name string, cfg config.Cache, logger *zap.Logger) (*deduplicator.Deduplicator, error) {
|
2024-02-06 16:00:45 -08:00
|
|
|
// Creates a deduplicator to discard VAA messages that were processed previously
|
2024-03-06 06:59:09 -08:00
|
|
|
deduplicatorCache, err := NewCache[bool](name, cfg.NumKeys, cfg.MaxCostsInMB)
|
2024-02-06 16:00:45 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-03-06 06:59:09 -08:00
|
|
|
expiration := time.Duration(cfg.ExpirationInSeconds) * time.Second
|
|
|
|
return deduplicator.New(deduplicatorCache, logger, deduplicator.WithExpiration(expiration)), nil
|
2024-02-06 16:00:45 -08:00
|
|
|
}
|