Skip to content

Commit

Permalink
Make it work with Redis 6.x and older again
Browse files Browse the repository at this point in the history
Fixes Suor#449
  • Loading branch information
Suor committed Apr 4, 2023
1 parent 8b3a79d commit eb8218b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cacheops/lua/cache_thing.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ for db_table, disj in pairs(dnfs) do
redis.call('sadd', conj_key, key)
-- NOTE: an invalidator should live longer than any key it references.
-- So we update its ttl on every key if needed.
-- REDIS_7
redis.call('expire', conj_key, timeout, 'gt')
-- /REDIS_7
-- REDIS_6
local conj_ttl = redis.call('ttl', conj_key)
if conj_ttl < timeout then
-- We set conj_key life with a margin over key life to call expire rarer
-- And add few extra seconds to be extra safe
redis.call('expire', conj_key, timeout * 2 + 10)
end
-- /REDIS_6
end
end
10 changes: 10 additions & 0 deletions cacheops/lua/cache_thing_insideout.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,17 @@ for _, conj_key in ipairs(conj_keys) do
table.insert(stamps, stamp)
-- NOTE: an invalidator should live longer than any key it references.
-- So we update its ttl on every key if needed.
-- REDIS_7
redis.call('expire', conj_key, timeout, 'gt')
-- /REDIS_7
-- REDIS_6
local conj_ttl = redis.call('ttl', conj_key)
if conj_ttl < timeout then
-- We set conj_key life with a margin over key life to call expire rarer
-- And add few extra seconds to be extra safe
redis.call('expire', conj_key, timeout * 2 + 10)
end
-- /REDIS_6
end

-- Write data to cache along with a checksum of the stamps to see if any of them changed
Expand Down
11 changes: 11 additions & 0 deletions cacheops/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,22 @@ def redis_client():
### Lua script loader

import os.path
import re


@memoize
def load_script(name):
filename = os.path.join(os.path.dirname(__file__), 'lua/%s.lua' % name)
with open(filename) as f:
code = f.read()
if is_redis_7():
code = re.sub(r'REDIS_6.*?/REDIS_6', '', code, flags=re.S)
else:
code = re.sub(r'REDIS_7.*?/REDIS_7', '', code, flags=re.S)
return redis_client.register_script(code)


@memoize
def is_redis_7():
redis_version = redis_client.info('server')['redis_version']
return int(redis_version.split('.')[0]) >= 7

0 comments on commit eb8218b

Please sign in to comment.