Skip to content

Commit

Permalink
Fix Redis 3.4.0 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
bbayles committed Feb 17, 2020
1 parent 1cf7da1 commit d15b29d
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 60 deletions.
24 changes: 16 additions & 8 deletions redis_collections/dicts.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import collections
import operator

from redis.client import Pipeline

from .base import RedisCollection


Expand Down Expand Up @@ -110,6 +112,7 @@ def __eq__(self, other):
return False

def eq_trans(pipe):
pipe.multi()
self_items = self.iteritems(pipe)
other_items = other.items(pipe) if use_redis else other.items()

Expand Down Expand Up @@ -195,7 +198,11 @@ def _data(self, pipe=None):
(without checking the local cache).
"""
pipe = self.redis if pipe is None else pipe
items = pipe.hgetall(self.key).items()
if isinstance(pipe, Pipeline):
pipe.hgetall(self.key)
items = pipe.execute()[-1].items()
else:
items = self.redis.hgetall(self.key).items()

return {self._unpickle_key(k): self._unpickle(v) for k, v in items}

Expand Down Expand Up @@ -243,7 +250,9 @@ def pop(self, key, default=__marker):
return self.cache.pop(key)

def pop_trans(pipe):
pickled_value = pipe.hget(self.key, pickled_key)
pipe.multi()
pipe.hget(self.key, pickled_key)
pickled_value = pipe.execute()[-1]
if pickled_value is None:
if default is self.__marker:
raise KeyError(key)
Expand Down Expand Up @@ -295,12 +304,10 @@ def setdefault(self, key, default=None):
return self.cache[key]

def setdefault_trans(pipe):
pickled_key = self._pickle_key(key)

pipe.multi()
pickled_key = self._pickle_key(key)
pipe.hsetnx(self.key, pickled_key, self._pickle_value(default))
pipe.hget(self.key, pickled_key)

__, pickled_value = pipe.execute()

return self._unpickle(pickled_value)
Expand All @@ -313,8 +320,8 @@ def setdefault_trans(pipe):

def _update_helper(self, other, use_redis=False):
def _update_helper_trans(pipe):
pipe.multi()
data = {}

if isinstance(other, Dict):
data.update(other.iteritems(pipe))
elif isinstance(other, RedisCollection):
Expand Down Expand Up @@ -490,8 +497,8 @@ def fromkeys(cls, iterable, v=None):

def _update_helper(self, other, op, use_redis=False):
def _update_helper_trans(pipe):
pipe.multi()
data = {}

if isinstance(other, Dict):
data.update(other.iteritems(pipe))
elif isinstance(other, RedisCollection):
Expand Down Expand Up @@ -560,6 +567,8 @@ def __delitem__(self, key):

def _op_helper(self, other, op, swap_args=False, inplace=False):
def op_trans(pipe):
pipe.multi()

# Get a collections.Counter copy of `self`
self_counter = collections.Counter(
{k: v for k, v in self.iteritems(pipe=pipe)}
Expand Down Expand Up @@ -595,7 +604,6 @@ def op_trans(pipe):
pickled_value = self._pickle_value(value)
pickled_data[pickled_key] = pickled_value

pipe.multi()
pipe.delete(self.key)
if pickled_data:
pipe.hmset(self.key, pickled_data)
Expand Down
Loading

0 comments on commit d15b29d

Please sign in to comment.