Skip to content

Commit 6e37725

Browse files
committed
Fix conj keys TTL in Redis 7.x
Closes #475
1 parent 0c8cdda commit 6e37725

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

cacheops/lua/cache_thing.lua

+2-5
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,13 @@ for db_table, disj in pairs(dnfs) do
4747
redis.call('sadd', conj_key, key)
4848
-- NOTE: an invalidator should live longer than any key it references.
4949
-- So we update its ttl on every key if needed.
50-
-- REDIS_7
51-
redis.call('expire', conj_key, timeout, 'gt')
52-
-- /REDIS_7
53-
-- REDIS_4
50+
-- NOTE: we also can't use "EXPIRE conj_key timeout GT" because it will have no effect on
51+
-- newly created and thus involatile conj keys.
5452
local conj_ttl = redis.call('ttl', conj_key)
5553
if conj_ttl < timeout then
5654
-- We set conj_key life with a margin over key life to call expire rarer
5755
-- And add few extra seconds to be extra safe
5856
redis.call('expire', conj_key, timeout * 2 + 10)
5957
end
60-
-- /REDIS_4
6158
end
6259
end

cacheops/lua/cache_thing_insideout.lua

+2-5
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,14 @@ for _, conj_key in ipairs(conj_keys) do
2828
table.insert(stamps, stamp)
2929
-- NOTE: an invalidator should live longer than any key it references.
3030
-- So we update its ttl on every key if needed.
31-
-- REDIS_7
32-
redis.call('expire', conj_key, timeout, 'gt')
33-
-- /REDIS_7
34-
-- REDIS_4
31+
-- NOTE: we also can't use "EXPIRE conj_key timeout GT" because it will have no effect on
32+
-- newly created and thus involatile conj keys.
3533
local conj_ttl = redis.call('ttl', conj_key)
3634
if conj_ttl < timeout then
3735
-- We set conj_key life with a margin over key life to call expire rarer
3836
-- And add few extra seconds to be extra safe
3937
redis.call('expire', conj_key, timeout * 2 + 10)
4038
end
41-
-- /REDIS_4
4239
end
4340

4441
-- Write data to cache along with a checksum of the stamps to see if any of them changed

tests/test_low_level.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import pytest
2+
3+
from cacheops.redis import redis_client
4+
5+
from .models import User
6+
from .utils import BaseTestCase
7+
8+
9+
@pytest.fixture()
10+
def base(db):
11+
case = BaseTestCase()
12+
case.setUp()
13+
yield
14+
case.tearDown()
15+
16+
17+
def test_ttl(base, django_assert_num_queries):
18+
user = User.objects.create(username='Suor')
19+
qs = User.objects.cache(timeout=100).filter(pk=user.pk)
20+
list(qs)
21+
assert 90 <= redis_client.ttl(qs._cache_key()) <= 100
22+
assert redis_client.ttl(f'conj:auth_user:id={user.id}') > 100

0 commit comments

Comments
 (0)