feat(proxyd): change default cache ttl to 2 hours and make it configurable (#9645)

This commit is contained in:
felipe 2024-02-23 13:58:00 -08:00 committed by GitHub
parent a6c9489376
commit f44f147313
4 changed files with 12 additions and 7 deletions

@ -21,8 +21,6 @@ type Cache interface {
const (
// assuming an average RPCRes size of 3 KB
memoryCacheLimit = 4096
// Set a large ttl to avoid expirations. However, a ttl must be set for volatile-lru to take effect.
redisTTL = 30 * 7 * 24 * time.Hour
)
type cache struct {
@ -49,10 +47,11 @@ func (c *cache) Put(ctx context.Context, key string, value string) error {
type redisCache struct {
rdb *redis.Client
prefix string
ttl time.Duration
}
func newRedisCache(rdb *redis.Client, prefix string) *redisCache {
return &redisCache{rdb, prefix}
func newRedisCache(rdb *redis.Client, prefix string, ttl time.Duration) *redisCache {
return &redisCache{rdb, prefix, ttl}
}
func (c *redisCache) namespaced(key string) string {
@ -78,7 +77,7 @@ func (c *redisCache) Get(ctx context.Context, key string) (string, error) {
func (c *redisCache) Put(ctx context.Context, key string, value string) error {
start := time.Now()
err := c.rdb.SetEx(ctx, c.namespaced(key), value, redisTTL).Err()
err := c.rdb.SetEx(ctx, c.namespaced(key), value, c.ttl).Err()
redisCacheDurationSumm.WithLabelValues("SETEX").Observe(float64(time.Since(start).Milliseconds()))
if err != nil {

@ -29,7 +29,8 @@ type ServerConfig struct {
}
type CacheConfig struct {
Enabled bool `toml:"enabled"`
Enabled bool `toml:"enabled"`
TTL TOMLDuration `toml:"ttl"`
}
type RedisConfig struct {

@ -235,7 +235,11 @@ func Start(config *Config) (*Server, func(), error) {
log.Warn("redis is not configured, using in-memory cache")
cache = newMemoryCache()
} else {
cache = newRedisCache(redisClient, config.Redis.Namespace)
ttl := defaultCacheTtl
if config.Cache.TTL != 0 {
ttl = time.Duration(config.Cache.TTL)
}
cache = newRedisCache(redisClient, config.Redis.Namespace, ttl)
}
rpcCache = newRPCCache(newCacheWithCompression(cache))
}

@ -42,6 +42,7 @@ const (
defaultWSHandshakeTimeout = 10 * time.Second
defaultWSReadTimeout = 2 * time.Minute
defaultWSWriteTimeout = 10 * time.Second
defaultCacheTtl = 1 * time.Hour
maxRequestBodyLogLen = 2000
defaultMaxUpstreamBatchSize = 10
defaultRateLimitHeader = "X-Forwarded-For"