Skip to content

Commit

Permalink
support hmset, hmget
Browse files Browse the repository at this point in the history
  • Loading branch information
yunitto committed Sep 30, 2018
1 parent 9910333 commit 14072d1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 15 deletions.
9 changes: 8 additions & 1 deletion ring/func/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,13 @@ def delete_value(self, key):
def has_value(self, key):
return self.backend.hexists(self.hash_key, key)

def get_many_values(self, keys):
values = self.backend.hmget(self.hash_key, keys)
return [v if v is not None else fbase.NotFound for v in values]

def set_many_values(self, keys, values, expire):
self.backend.hmset(self.hash_key, {k: v for k, v in zip(keys, values)})


class DiskCacheStorage(fbase.CommonMixinStorage, fbase.StorageMixin):

Expand Down Expand Up @@ -530,7 +537,7 @@ def redis_py(


def redis_py_hash(
client, hash_key=None, key_prefix=None, expire=None, coder=None, ignorable_keys=None,
client, hash_key=None, key_prefix=None, coder=None, ignorable_keys=None,
user_interface=(CacheUserInterface, BulkInterfaceMixin),
storage_class=RedisHashStorage,
**kwargs):
Expand Down
60 changes: 46 additions & 14 deletions tests/test_redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,55 @@ def f(a, b):


def test_redis_hash(redis_client):
@ring.redis_hash(redis_client, 'test-hash-key', 'test-field1')
def f1(a, b):
@ring.redis_hash(redis_client, 'test-hash-key', 'test-field')
def f(a, b):
r = a * 100 + b
return str(r).encode('utf-8')

assert f1.key(1, 2) == 'test-field1:1:2'
# delete previous test
f.delete(1, 2)
f.delete(3, 4)
f.delete(5, 6)
f.delete(7, 8)

f1.delete(1, 2)
assert False is f1.has(1, 2)
assert None is f1.get(1, b=2)
assert 102 == int(f1(1, b=2))
assert f.key(1, 2) == 'test-field:1:2'

@ring.redis_hash(redis_client, 'test-hash-key', 'test-field2')
def f2(a, b):
r = a * 200 + b
return str(r).encode('utf-8')
f.delete(1, 2)
assert False is f.has(1, 2)
assert None is f.get(1, b=2)
assert 102 == int(f(1, b=2))

assert f2.key(1, 2) == 'test-field2:1:2'
assert 202 == int(f2(1, b=2))
assert 102 is int(f1.get(1, b=2))
assert f.key(3, 4) == 'test-field:3:4'
assert 102 == int(f.get(1, b=2))
assert 304 == int(f(3, b=4))

mv = f.get_many(
(1, 2),
(3, 4),
)
assert mv == [b'102', b'304']

with pytest.raises(AttributeError):
f.delete_many()
f.delete(1, 2)
f.delete(3, 4)

mv = f.get_many(
(1, 2),
(3, 4),
)
assert mv == [None, None]

mv = f.update_many(
{'a': 5, 'b': 6},
(7, 8),
)
assert mv == [b'506', b'708']

mv = f.get_many(
(1, 2),
(3, 4),
(5, 6),
(7, 8),
)
assert mv == [None, None, b'506', b'708']

0 comments on commit 14072d1

Please sign in to comment.