Skip to content

Commit 8d30594

Browse files
authored
feat(proxyd): change default cache ttl to 2 hours and make it configurable (ethereum-optimism#9645)
1 parent a58ea47 commit 8d30594

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

proxyd/cache.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ type Cache interface {
2121
const (
2222
// assuming an average RPCRes size of 3 KB
2323
memoryCacheLimit = 4096
24-
// Set a large ttl to avoid expirations. However, a ttl must be set for volatile-lru to take effect.
25-
redisTTL = 30 * 7 * 24 * time.Hour
2624
)
2725

2826
type cache struct {
@@ -49,10 +47,11 @@ func (c *cache) Put(ctx context.Context, key string, value string) error {
4947
type redisCache struct {
5048
rdb *redis.Client
5149
prefix string
50+
ttl time.Duration
5251
}
5352

54-
func newRedisCache(rdb *redis.Client, prefix string) *redisCache {
55-
return &redisCache{rdb, prefix}
53+
func newRedisCache(rdb *redis.Client, prefix string, ttl time.Duration) *redisCache {
54+
return &redisCache{rdb, prefix, ttl}
5655
}
5756

5857
func (c *redisCache) namespaced(key string) string {
@@ -78,7 +77,7 @@ func (c *redisCache) Get(ctx context.Context, key string) (string, error) {
7877

7978
func (c *redisCache) Put(ctx context.Context, key string, value string) error {
8079
start := time.Now()
81-
err := c.rdb.SetEx(ctx, c.namespaced(key), value, redisTTL).Err()
80+
err := c.rdb.SetEx(ctx, c.namespaced(key), value, c.ttl).Err()
8281
redisCacheDurationSumm.WithLabelValues("SETEX").Observe(float64(time.Since(start).Milliseconds()))
8382

8483
if err != nil {

proxyd/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ type ServerConfig struct {
2929
}
3030

3131
type CacheConfig struct {
32-
Enabled bool `toml:"enabled"`
32+
Enabled bool `toml:"enabled"`
33+
TTL TOMLDuration `toml:"ttl"`
3334
}
3435

3536
type RedisConfig struct {

proxyd/proxyd.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,11 @@ func Start(config *Config) (*Server, func(), error) {
235235
log.Warn("redis is not configured, using in-memory cache")
236236
cache = newMemoryCache()
237237
} else {
238-
cache = newRedisCache(redisClient, config.Redis.Namespace)
238+
ttl := defaultCacheTtl
239+
if config.Cache.TTL != 0 {
240+
ttl = time.Duration(config.Cache.TTL)
241+
}
242+
cache = newRedisCache(redisClient, config.Redis.Namespace, ttl)
239243
}
240244
rpcCache = newRPCCache(newCacheWithCompression(cache))
241245
}

proxyd/server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const (
4242
defaultWSHandshakeTimeout = 10 * time.Second
4343
defaultWSReadTimeout = 2 * time.Minute
4444
defaultWSWriteTimeout = 10 * time.Second
45+
defaultCacheTtl = 1 * time.Hour
4546
maxRequestBodyLogLen = 2000
4647
defaultMaxUpstreamBatchSize = 10
4748
defaultRateLimitHeader = "X-Forwarded-For"

0 commit comments

Comments
 (0)