eth-blob-indexer/pkg/storage.go

44 lines
976 B
Go
Raw Normal View History

package blob_indexer
import (
"context"
"time"
"github.com/go-redis/redis/v8"
"github.com/protolambda/zrnt/eth2/beacon/common"
)
func BlobKeyFromHash(vhash common.Hash32) string {
return "blob:" + vhash.String()
}
// KvStorage represents a Redis-like KV store
type KvStorage interface {
Set(ctx context.Context, key string, value []byte, expiration time.Duration) error
}
type RedisStorage struct {
rdb *redis.Client
}
func NewRedisStorage(rdb *redis.Client) *RedisStorage {
return &RedisStorage{rdb: rdb}
}
func (rs *RedisStorage) Set(ctx context.Context, key string, value []byte, expiration time.Duration) error {
return rs.rdb.Set(ctx, key, value, expiration).Err()
}
type MapStorage struct {
store map[string][]byte
}
func NewMapStorage() *MapStorage {
return &MapStorage{store: make(map[string][]byte)}
}
func (ms *MapStorage) Set(ctx context.Context, key string, value []byte, expiration time.Duration) error {
ms.store[key] = value
return nil
}