Skip to content

Commit

Permalink
Rename ChainID -> CachePrefix and add validation
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeniy-scherbina committed Oct 19, 2023
1 parent e1ed01e commit 667fa36
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
10 changes: 9 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,17 @@ METRIC_PARTITIONING_ROUTINE_DELAY_FIRST_RUN_SECONDS=10
METRIC_PARTITIONINING_PREFILL_PERIOD_DAYS=7
# Used by `ready` script to ensure metric partitions have been created.
MINIMUM_REQUIRED_PARTITIONS=30
# RedisEndpointURL is an url of redis
REDIS_ENDPOINT_URL=redis:6379
REDIS_PASSWORD=
CHAIN_ID=local-chain
# TTL for cached evm requests
# TTL should be specified in seconds
CACHE_TTL=600
# CachePrefix is used as prefix for any key in the cache, key has such structure:
# <cache_prefix>:evm-request:<method_name>:sha256:<sha256(body)>
# Possible values are testnet, mainnet, etc...
# CachePrefix must not contain colon symbol
CACHE_PREFIX=local-chain

##### Database Config
POSTGRES_PASSWORD=password
Expand Down
15 changes: 5 additions & 10 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,8 @@ type Config struct {
MetricPartitioningPrefillPeriodDays int
RedisEndpointURL string
RedisPassword string
// TTL for cached evm requests
CacheTTL time.Duration
// CachePrefix is used as prefix for any key in the cache, key has such structure:
// <cache_prefix>:evm-request:<method_name>:sha256:<sha256(body)>
// Possible values are testnet, mainnet, etc...
CachePrefix string
CacheTTL time.Duration
CachePrefix string
}

const (
Expand Down Expand Up @@ -90,8 +86,7 @@ const (
REDIS_ENDPOINT_URL_ENVIRONMENT_KEY = "REDIS_ENDPOINT_URL"
REDIS_PASSWORD_ENVIRONMENT_KEY = "REDIS_PASSWORD"
CACHE_TTL_ENVIRONMENT_KEY = "CACHE_TTL"
DEFAULT_CACHE_TTL_SECONDS = 600
CHAIN_ID_ENVIRONMENT_KEY = "CHAIN_ID"
CACHE_PREFIX_ENVIRONMENT_KEY = "CACHE_PREFIX"
)

// EnvOrDefault fetches an environment variable value, or if not set returns the fallback value
Expand Down Expand Up @@ -219,7 +214,7 @@ func ReadConfig() Config {
MetricPartitioningPrefillPeriodDays: EnvOrDefaultInt(METRIC_PARTITIONING_PREFILL_PERIOD_DAYS_ENVIRONMENT_KEY, DEFAULT_METRIC_PARTITIONING_PREFILL_PERIOD_DAYS),
RedisEndpointURL: os.Getenv(REDIS_ENDPOINT_URL_ENVIRONMENT_KEY),
RedisPassword: os.Getenv(REDIS_PASSWORD_ENVIRONMENT_KEY),
CacheTTL: time.Duration(EnvOrDefaultInt(CACHE_TTL_ENVIRONMENT_KEY, DEFAULT_CACHE_TTL_SECONDS)) * time.Second,
CachePrefix: os.Getenv(CHAIN_ID_ENVIRONMENT_KEY),
CacheTTL: time.Duration(EnvOrDefaultInt(CACHE_TTL_ENVIRONMENT_KEY, 0)) * time.Second,
CachePrefix: os.Getenv(CACHE_PREFIX_ENVIRONMENT_KEY),
}
}
14 changes: 14 additions & 0 deletions config/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"strconv"
"strings"
)

var (
Expand Down Expand Up @@ -48,5 +49,18 @@ func Validate(config Config) error {
allErrs = errors.Join(allErrs, fmt.Errorf("invalid %s specified %d, must be non-zero and less than or equal to %d", METRIC_PARTITIONING_PREFILL_PERIOD_DAYS_ENVIRONMENT_KEY, config.MetricPartitioningPrefillPeriodDays, MaxMetricPartitioningPrefillPeriodDays))
}

if config.RedisEndpointURL == "" {
allErrs = errors.Join(allErrs, fmt.Errorf("invalid %s specified %s, must not be empty", REDIS_ENDPOINT_URL_ENVIRONMENT_KEY, config.RedisEndpointURL))
}
if config.CacheTTL <= 0 {
allErrs = errors.Join(allErrs, fmt.Errorf("invalid %s specified %s, must be greater than zero", CACHE_TTL_ENVIRONMENT_KEY, config.CacheTTL))
}
if strings.Contains(config.CachePrefix, ":") {
allErrs = errors.Join(allErrs, fmt.Errorf("invalid %s specified %s, must not contain colon symbol", CACHE_PREFIX_ENVIRONMENT_KEY, config.CachePrefix))
}
if config.CachePrefix == "" {
allErrs = errors.Join(allErrs, fmt.Errorf("invalid %s specified %s, must not be empty", CACHE_PREFIX_ENVIRONMENT_KEY, config.CachePrefix))
}

return allErrs
}
5 changes: 3 additions & 2 deletions service/cachemdw/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import (
// ServiceCache is responsible for caching EVM requests and provides corresponding middleware
// ServiceCache can work with any underlying storage which implements simple cache.Cache interface
type ServiceCache struct {
cacheClient cache.Cache
blockGetter decode.EVMBlockGetter
cacheClient cache.Cache
blockGetter decode.EVMBlockGetter
// TTL for cached evm requests
cacheTTL time.Duration
decodedRequestContextKey any
// cachePrefix is used as prefix for any key in the cache
Expand Down

0 comments on commit 667fa36

Please sign in to comment.