Skip to content

Conversation

@strobil
Copy link
Contributor

@strobil strobil commented Dec 3, 2025

Summary

Adds OverrideCacheTTL() function to allow setting custom TTL for cache entries based on the data being cached.

Use Case

Enable different TTLs for positive vs negative cache entries:

  • Positive entries (data found): longer TTL (e.g., 5 minutes)
  • Negative entries (data not found): shorter TTL (e.g., 30 seconds)

This prevents stale negative entries when data becomes available shortly after an initial miss.

Usage

val, err := cache.Get(ctx, 5*time.Minute, key, func(ctx context.Context, key string) (string, error) {
    user, err := db.GetByID(ctx, userID)
    if err == sql.ErrNoRows {
        rueidisaside.OverrideCacheTTL(ctx, 30*time.Second)
        return "NOT_FOUND", nil  // cached for 30 seconds
    }
    return user, nil  // cached for 5 minutes
})

Closes #921

@jit-ci
Copy link

jit-ci bot commented Dec 3, 2025

Hi, I’m Jit, a friendly security platform designed to help developers build secure applications from day zero with an MVS (Minimal viable security) mindset.

In case there are security findings, they will be communicated to you as a comment inside the PR.

Hope you’ll enjoy using Jit.

Questions? Comments? Want to learn more? Get in touch with us.

@strobil strobil force-pushed the feat/override-cache-ttl branch from dd669cc to 490616b Compare December 3, 2025 13:35
@rueian rueian merged commit 1a87f15 into redis:main Dec 3, 2025
37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rueidisaside: Same TTL for positive and negative caching may not be optimal

2 participants