Skip to content

feat(stores/redis): use GETEX on Redis >= 6.2 for get+renew - #4996

Open
Karunasagar12 wants to merge 2 commits into
litestar-org:mainfrom
Karunasagar12:feat/redis-store-getex
Open

feat(stores/redis): use GETEX on Redis >= 6.2 for get+renew#4996
Karunasagar12 wants to merge 2 commits into
litestar-org:mainfrom
Karunasagar12:feat/redis-store-getex

Conversation

@Karunasagar12

Copy link
Copy Markdown

Summary

Closes #4993 — on Redis 6.2+, GETEX atomically gets a value and sets its expiry in a single command, eliminating the Lua script overhead.

Changes

litestar/stores/redis.py:

  1. Added _check_getex_support() — lazily detects Redis server version via INFO server on first get() call with renew_for. Result is cached for the lifetime of the store instance.
  2. get() now uses GETEX key EX <seconds> when Redis >= 6.2, falling back to the existing Lua script on older versions.
  3. Bug fix: Changed renew_for.seconds to int(renew_for.total_seconds()). The old code used timedelta.seconds which only returns the seconds component, not total seconds — timedelta(days=1, seconds=5).seconds == 5, not 86405.

tests/unit/test_stores.py:

Test What it verifies
test_get_and_renew_redis_uses_getex Version detection works, GETEX path is taken, TTL is renewed
test_get_and_renew_redis_lua_fallback Lua script still works when GETEX is unsupported
test_get_renew_for_timedelta_uses_total_seconds timedelta conversion uses total_seconds()

Backward Compatibility

  • Redis < 6.2: falls back to existing Lua script (no behavior change)
  • Redis >= 6.2: same semantics, fewer round-trips
  • Version check uses a single cached INFO server call (negligible cost)

On Redis 6.2+, GETEX atomically gets a value and sets its expiry in a
single command — no Lua script overhead. This change:

1. Lazily detects Redis server version on first get() call with renew_for
2. Uses GETEX when version >= 6.2 (result cached after first check)
3. Falls back to the existing Lua script on older Redis versions

Also fixes a pre-existing bug: timedelta.seconds only returns the seconds
component (not total seconds). timedelta(days=1, seconds=5).seconds == 5,
not 86405. Changed to int(total_seconds()) for correctness.

Closes litestar-org#4993
@Karunasagar12
Karunasagar12 requested review from a team as code owners August 16, 2026 15:55
@codecov

codecov Bot commented Aug 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 67.17%. Comparing base (b18a21f) to head (ce2b4b2).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4996      +/-   ##
==========================================
- Coverage   67.17%   67.17%   -0.01%     
==========================================
  Files         293      293              
  Lines       15354    15352       -2     
  Branches     1744     1744              
==========================================
- Hits        10314    10312       -2     
  Misses       4890     4890              
  Partials      150      150              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@sobolevn sobolevn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think 3.0.0 is the right time to drop older redis versions support.

6.0 https://redis.io/docs/latest/operate/rs/release-notes/rs-6-0-may-2020/ was released 6 years ago.
and 6.1 never existed.

Redis 6.2+ (released May 2020) introduced GETEX which atomically gets a
value and sets its expiry in a single command. Per @sobolevn's feedback,
3.0.0 is the right time to drop older Redis version support entirely.

Changes:
- Remove _get_and_renew_script Lua script from RedisStore.__init__
- Remove _supports_getex version detection (no fallback needed)
- get() with renew_for now calls redis.getex(key, ex=seconds) directly
- Fix pre-existing bug: timedelta.seconds -> int(total_seconds())
  (timedelta(days=1).seconds == 0, not 86400)
- Remove fallback-related tests, keep timedelta total_seconds test
@Karunasagar12

Copy link
Copy Markdown
Author

Good call — updated. Dropped the Lua script and version detection entirely. get() now just calls GETEX directly.

Also fixed a pre-existing bug while I was in there: the old code used timedelta.seconds (which only returns the seconds component) instead of int(total_seconds()). So renew_for=timedelta(days=1) was silently renewing for 0 seconds.

@sobolevn

Copy link
Copy Markdown
Member

This would require a deprecation message in 2.x branch

@provinzkraut

Copy link
Copy Markdown
Member

I think 3.0.0 is the right time to drop older redis versions support.

6.0 https://redis.io/docs/latest/operate/rs/release-notes/rs-6-0-may-2020/ was released 6 years ago. and 6.1 never existed.

I'm still stuck at 6.0 at work.. 😬
And I'm sure I'm not the only one. I'd rather we keep it unless there's a good reason against dropping it?

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.

Enhancement: Use redis GETEX in RedisStore.get for renew_for functionality on redis 6.2+

3 participants