Skip to content

Commit

Permalink
Drop redis prior to 4.0 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Suor committed May 15, 2020
1 parent 035e59c commit 12f61b3
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ And there is more to it:
Requirements
------------

| Python 3.5+, Django 2.1+ and Redis 2.6+ (4.0+ recommended)
| Python 3.5+, Django 2.1+ and Redis 4.0+

Installation
Expand Down
18 changes: 2 additions & 16 deletions cacheops/invalidation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from funcy import memoize, post_processing, ContextDecorator
from django.db import DEFAULT_DB_ALIAS
from django.db.models.expressions import F, Expression
from distutils.version import StrictVersion

from .conf import settings
from .sharding import get_prefix
Expand All @@ -15,27 +14,14 @@
__all__ = ('invalidate_obj', 'invalidate_model', 'invalidate_all', 'no_invalidation')


@memoize
def redis_can_unlink():
redis_version = redis_client.info()['redis_version']
return StrictVersion(redis_version) >= StrictVersion('4.0')


def invalidate_keys(*keys):
if redis_can_unlink():
redis_client.execute_command('UNLINK', *keys)
else:
redis_client.delete(*keys)


@queue_when_in_transaction
@handle_connection_failure
def invalidate_dict(model, obj_dict, using=DEFAULT_DB_ALIAS):
if no_invalidation.active or not settings.CACHEOPS_ENABLED:
return
model = model._meta.concrete_model
prefix = get_prefix(_cond_dnfs=[(model._meta.db_table, list(obj_dict.items()))], dbs=[using])
load_script('invalidate', strip=redis_can_unlink())(keys=[prefix], args=[
load_script('invalidate')(keys=[prefix], args=[
model._meta.db_table,
json.dumps(obj_dict, default=str)
])
Expand Down Expand Up @@ -68,7 +54,7 @@ def invalidate_model(model, using=DEFAULT_DB_ALIAS):
if conjs_keys:
cache_keys = redis_client.sunion(conjs_keys)
keys = list(cache_keys) + conjs_keys
invalidate_keys(*keys)
redis_client.unlink(*keys)
cache_invalidated.send(sender=model, obj_dict=None)


Expand Down
7 changes: 1 addition & 6 deletions cacheops/lua/invalidate.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
local prefix = KEYS[1]
local db_table = ARGV[1]
local obj = cjson.decode(ARGV[2])
local conj_del_fn = 'unlink'
-- If Redis version < 4.0 we can't use UNLINK
-- TOSTRIP
conj_del_fn = 'del'
-- /TOSTRIP

-- Utility functions
local conj_cache_key = function (db_table, scheme, obj)
Expand Down Expand Up @@ -38,7 +33,7 @@ if next(conj_keys) ~= nil then
local cache_keys = redis.call('sunion', unpack(conj_keys))
-- we delete cache keys since they are invalid
-- and conj keys as they will refer only deleted keys
redis.call(conj_del_fn, unpack(conj_keys))
redis.call("unlink", unpack(conj_keys))
if next(cache_keys) ~= nil then
-- NOTE: can't just do redis.call('del', unpack(...)) cause there is limit on number
-- of return values in lua.
Expand Down

0 comments on commit 12f61b3

Please sign in to comment.