Skip to content

Commit

Permalink
Merge 3a1aee0 into 3026642
Browse files Browse the repository at this point in the history
  • Loading branch information
thebjorn committed Sep 30, 2020
2 parents 3026642 + 3a1aee0 commit c32720d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .idea/dkredis.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 16 additions & 8 deletions dkredis/dkredis.py
Expand Up @@ -27,6 +27,8 @@
import redis as _redis
from contextlib import contextmanager

PICLE_PROTOCOL = 1


class Timeout(Exception): # pragma: nocover
"""A timout limit was exceeded.
Expand Down Expand Up @@ -199,6 +201,8 @@ def update(key, fn, cn=None):
break # success, break out of while loop
except _redis.WatchError: # pragma: nocover
pass # someone else got there before us, retry.
if isinstance(newval, bytes):
newval = newval.decode('u8')
return newval


Expand All @@ -209,6 +213,8 @@ def setmax(key, val, cn=None):
returns the maximum value.
"""
if isinstance(val, text):
val = val.encode('u8')
return update(key, lambda v: max(v, val), cn=cn)


Expand All @@ -219,6 +225,8 @@ def setmin(key, val, cn=None):
returns the maximum value.
"""
if isinstance(val, text):
val = val.encode('u8')
return update(key, lambda v: min(v, val), cn=cn)


Expand All @@ -238,7 +246,7 @@ def set_pyval(key, val, secs=None, cn=None):
"""Store any (picleable) value in Redis.
"""
r = cn or connect()
pval = pickle.dumps(val)
pval = pickle.dumps(val, protocol=PICLE_PROTOCOL)
if secs is None:
r.set(key, pval)
else:
Expand Down Expand Up @@ -274,7 +282,7 @@ def set_dict(key, dictval, secs=None, cn=None):
as strings -- use `py_setval` to set dicts with any values.
"""
r = cn or connect()
r.hmset(key, dictval)
r.hset(key, mapping=dictval)
if secs is not None:
r.expire(key, secs)

Expand All @@ -286,7 +294,7 @@ def get_dict(key, cn=None):
res = {}
for fieldname in r.hkeys(key):
res[fieldname] = r.hget(key, fieldname)
return res
return {k.decode('u8'): v.decode('u8') for k, v in res.items()}


def mhkeyget(keypattern, field, cn=None):
Expand All @@ -295,11 +303,11 @@ def mhkeyget(keypattern, field, cn=None):
Usage::
>>> r = connect()
>>> r.hmset('lock.a', {'x': 1})
>>> r.hset('lock.a', {'x': 1})
True
>>> r.hmset('lock.b', {'x': 2})
>>> r.hset('lock.b', {'x': 2})
True
>>> r.hmset('lock.c', {'x': 3})
>>> r.hset('lock.c', {'x': 3})
True
>>> mhkeyget('lock.*', 'x')
{'lock.a': '1', 'lock.c': '3', 'lock.b': '2'}
Expand All @@ -315,8 +323,8 @@ def mhkeyget(keypattern, field, cn=None):
hashes = r.keys(keypattern)
for h in hashes:
result[h] = r.hget(h, field)

return result
# XXX: redis always returns bytes, and we want unicode
return {k.decode('u8'): v.decode('u8') for k, v in result.items()}


def convert_to_bytes(r):
Expand Down
25 changes: 16 additions & 9 deletions tests/test_dkredis_fast.py
Expand Up @@ -19,10 +19,12 @@ def test_mhkeyget(cn):
"Test of mhkeyget-function."
for key in cn.scan_iter("lock.*"):
cn.delete(key)
assert cn.hmset('lock.a', {'x': 1})
assert cn.hmset('lock.b', {'x': 2})
assert cn.hmset('lock.c', {'x': 3})
all_locks = {b'lock.a': b'1', b'lock.c': b'3', b'lock.b': b'2'}
assert cn.hset('lock.a', key='x', value=1)
assert cn.hset('lock.b', key='x', value=2)
assert cn.hset('lock.c', key='x', value=3)
# assert cn.hset('lock.b', {'x': 2})
# assert cn.hset('lock.c', {'x': 3})
all_locks = {'lock.a': '1', 'lock.c': '3', 'lock.b': '2'}
assert dkredis.mhkeyget('lock.*', 'x') == all_locks
assert cn.delete('lock.a', 'lock.b', 'lock.c')

Expand All @@ -35,13 +37,17 @@ def test_update(cn):


def test_setmax(cn):
cn.set('testsetmax', b'hello')
assert dkredis.setmax('testsetmax', b'world', cn=cn) == b'world'
cn.set('testsetmax', 'hello')
assert dkredis.setmax('testsetmax', 'world', cn=cn) == 'world'
# cn.set('testsetmax', 1)
# assert dkredis.setmax('testsetmax', 2, cn=cn) == 2


def test_setmin(cn):
cn.set('testsetmax', b'hello')
assert dkredis.setmin('testsetmax', b'world', cn=cn) == b'hello'
cn.set('testsetmax', 'hello')
assert dkredis.setmin('testsetmax', 'world', cn=cn) == 'hello'
# cn.set('testsetmax', 1)
# assert dkredis.setmin('testsetmax', 2, cn=cn) == 1


def test_pyval():
Expand All @@ -58,4 +64,5 @@ def test_pop_pyval(cn):

def test_dict(cn):
dkredis.set_dict('testdict', dict(hello='world'), secs=5, cn=cn)
assert dkredis.get_dict('testdict', cn=cn) == {b'hello': b'world'}
assert dkredis.get_dict('testdict', cn=cn) == {'hello': 'world'}

0 comments on commit c32720d

Please sign in to comment.