Skip to content

Commit

Permalink
add ability to set ttls by method name
Browse files Browse the repository at this point in the history
  • Loading branch information
dshiell committed Aug 30, 2024
1 parent 7d41e71 commit 0ce4cd2
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 4 deletions.
6 changes: 6 additions & 0 deletions common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type ConnectorConfig struct {
Redis *RedisConnectorConfig `yaml:"redis" json:"redis"`
DynamoDB *DynamoDBConnectorConfig `yaml:"dynamodb" json:"dynamodb"`
PostgreSQL *PostgreSQLConnectorConfig `yaml:"postgresql" json:"postgresql"`
Methods []*MethodCacheConfig `yaml:"methods" json:"methods"`
}

type MemoryConnectorConfig struct {
Expand Down Expand Up @@ -74,6 +75,11 @@ func (r *RedisConnectorConfig) MarshalJSON() ([]byte, error) {
})
}

type MethodCacheConfig struct {
Method string `yaml:"method" json:"method"`
TTL string `yamle:"ttl" json:"ttl"`
}

type DynamoDBConnectorConfig struct {
Table string `yaml:"table" json:"table"`
Region string `yaml:"region" json:"region"`
Expand Down
2 changes: 2 additions & 0 deletions data/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const (
type Connector interface {
Get(ctx context.Context, index, partitionKey, rangeKey string) (string, error)
Set(ctx context.Context, partitionKey, rangeKey, value string) error
SetTTL(method string, ttlStr string) error
HasTTL(method string) bool
Delete(ctx context.Context, index, partitionKey, rangeKey string) error
}

Expand Down
9 changes: 9 additions & 0 deletions data/dynamodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,15 @@ func ensureGlobalSecondaryIndexes(
return err
}

func (d *DynamoDBConnector) SetTTL(_ string, _ string) error {
d.logger.Debug().Msgf("Method TTLs not implemented for DynamoDBConnector")
return nil
}

func (d *DynamoDBConnector) HasTTL(_ string) bool {
return false
}

func (d *DynamoDBConnector) Set(ctx context.Context, partitionKey, rangeKey, value string) error {
d.logger.Debug().Msgf("writing to dynamodb with partition key: %s and range key: %s", partitionKey, rangeKey)

Expand Down
9 changes: 9 additions & 0 deletions data/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ func NewMemoryConnector(ctx context.Context, logger *zerolog.Logger, cfg *common
}, nil
}

func (m *MemoryConnector) SetTTL(_ string, _ string) error {
m.logger.Debug().Msgf("Method TTLs not implemented for MemoryConnector")
return nil
}

func (d *MemoryConnector) HasTTL(_ string) bool {
return false
}

func (m *MemoryConnector) Set(ctx context.Context, partitionKey, rangeKey, value string) error {
key := fmt.Sprintf("%s:%s", partitionKey, rangeKey)
m.cache.Add(key, value)
Expand Down
9 changes: 9 additions & 0 deletions data/postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ func NewPostgreSQLConnector(ctx context.Context, logger *zerolog.Logger, cfg *co
}, nil
}

func (p *PostgreSQLConnector) SetTTL(_ string, _ string) error {
p.logger.Debug().Msgf("Method TTLs not implemented for PostgresSQLConnector")
return nil
}

func (p *PostgreSQLConnector) HasTTL(_ string) bool {
return false
}

func (p *PostgreSQLConnector) Set(ctx context.Context, partitionKey, rangeKey, value string) error {
p.logger.Debug().Msgf("writing to PostgreSQL with partition key: %s and range key: %s", partitionKey, rangeKey)

Expand Down
26 changes: 24 additions & 2 deletions data/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"os"
"strings"
"time"

"github.com/erpc/erpc/common"
"github.com/redis/go-redis/v9"
Expand All @@ -23,6 +24,7 @@ var _ Connector = (*RedisConnector)(nil)
type RedisConnector struct {
logger *zerolog.Logger
client *redis.Client
ttls map[string]time.Duration
}

func NewRedisConnector(
Expand Down Expand Up @@ -62,6 +64,7 @@ func NewRedisConnector(
return &RedisConnector{
logger: logger,
client: client,
ttls: make(map[string]time.Duration),
}, nil
}

Expand Down Expand Up @@ -91,14 +94,33 @@ func createTLSConfig(tlsCfg *common.TLSConfig) (*tls.Config, error) {
return config, nil
}

func (r *RedisConnector) SetTTL(method string, ttlStr string) error {
ttl, err := time.ParseDuration(ttlStr)
if err != nil {
return err
}
r.ttls[strings.ToLower(method)] = ttl
return nil
}

func (r *RedisConnector) HasTTL(method string) bool {
_, found := r.ttls[strings.ToLower(method)]
return found
}

func (r *RedisConnector) Set(ctx context.Context, partitionKey, rangeKey, value string) error {
r.logger.Debug().Msgf("writing to Redis with partition key: %s and range key: %s", partitionKey, rangeKey)
method := strings.ToLower(strings.Split(rangeKey, ":")[0])
ttl, found := r.ttls[method]
if !found {
ttl = time.Duration(0)
}
key := fmt.Sprintf("%s:%s", partitionKey, rangeKey)
rs := r.client.Set(ctx, key, value, 0)
rs := r.client.Set(ctx, key, value, ttl)
return rs.Err()
}

func (r *RedisConnector) Get(ctx context.Context, index, partitionKey, rangeKey string) (string, error) {
func (r *RedisConnector) Get(ctx context.Context, _, partitionKey, rangeKey string) (string, error) {
var err error
var value string

Expand Down
15 changes: 13 additions & 2 deletions erpc/evm_json_rpc_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ func NewEvmJsonRpcCache(ctx context.Context, logger *zerolog.Logger, cfg *common
return nil, err
}

// set TTL method overrides
for _, cacheInfo := range cfg.Methods {
if err := c.SetTTL(cacheInfo.Method, cacheInfo.TTL); err != nil {
return nil, err
}
}

return &EvmJsonRpcCache{
conn: c,
logger: logger,
Expand All @@ -56,11 +63,13 @@ func (c *EvmJsonRpcCache) Get(ctx context.Context, req *common.NormalizedRequest
return nil, err
}

hasTTL := c.conn.HasTTL(rpcReq.Method)

blockRef, blockNumber, err := common.ExtractEvmBlockReferenceFromRequest(rpcReq)
if err != nil {
return nil, err
}
if blockRef == "" && blockNumber == 0 {
if blockRef == "" && blockNumber == 0 && !hasTTL {
return nil, nil
}
if blockNumber != 0 {
Expand Down Expand Up @@ -124,7 +133,9 @@ func (c *EvmJsonRpcCache) Set(ctx context.Context, req *common.NormalizedRequest
return err
}

if blockRef == "" && blockNumber == 0 {
hasTTL := c.conn.HasTTL(rpcReq.Method)

if blockRef == "" && blockNumber == 0 && !hasTTL {
// Do not cache if we can't resolve a block reference (e.g. latest block requests)
lg.Debug().
Str("blockRef", blockRef).
Expand Down

0 comments on commit 0ce4cd2

Please sign in to comment.