feat(stores/redis): use GETEX on Redis >= 6.2 for get+renew - #4996
feat(stores/redis): use GETEX on Redis >= 6.2 for get+renew#4996Karunasagar12 wants to merge 2 commits into
Conversation
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
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
sobolevn
left a comment
There was a problem hiding this comment.
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
|
Good call — updated. Dropped the Lua script and version detection entirely. Also fixed a pre-existing bug while I was in there: the old code used |
|
This would require a deprecation message in 2.x branch |
I'm still stuck at 6.0 at work.. 😬 |
Summary
Closes #4993 — on Redis 6.2+,
GETEXatomically gets a value and sets its expiry in a single command, eliminating the Lua script overhead.Changes
litestar/stores/redis.py:_check_getex_support()— lazily detects Redis server version viaINFO serveron firstget()call withrenew_for. Result is cached for the lifetime of the store instance.get()now usesGETEX key EX <seconds>when Redis >= 6.2, falling back to the existing Lua script on older versions.renew_for.secondstoint(renew_for.total_seconds()). The old code usedtimedelta.secondswhich only returns the seconds component, not total seconds —timedelta(days=1, seconds=5).seconds == 5, not 86405.tests/unit/test_stores.py:test_get_and_renew_redis_uses_getextest_get_and_renew_redis_lua_fallbacktest_get_renew_for_timedelta_uses_total_secondsBackward Compatibility
INFO servercall (negligible cost)