Skip to content

Commit 9e3e1aa

Browse files
committed
added HGET/HSET commands
removed the KEYS callback -- 1.34 Redis servers now return KEYS with the multi-bulk protocol, which means they're already in a list
1 parent c83542e commit 9e3e1aa

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

redis/client.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ class Redis(threading.local):
182182
{
183183
'BGSAVE': lambda r: r == 'Background saving started',
184184
'INFO': parse_info,
185-
'KEYS': lambda r: r and r.split(' ') or [],
186185
'LASTSAVE': timestamp_to_datetime,
187186
'PING': lambda r: r == 'PONG',
188187
'RANDOMKEY': lambda r: r and r or None,
@@ -868,6 +867,16 @@ def zscore(self, name, value):
868867
return self.format_bulk('ZSCORE', name, value)
869868

870869

870+
#### HASH COMMANDS ####
871+
def hget(self, name, key):
872+
"Return the value of ``key`` within the hash ``name``"
873+
return self.format_bulk('HGET', name, key)
874+
875+
def hset(self, name, key, value):
876+
"Set ``key`` to ``value`` within hash ``name``"
877+
return self.format_multi_bulk('HSET', name, key, value)
878+
879+
871880
class Pipeline(Redis):
872881
"""
873882
Pipelines provide a way to transmit multiple commands to the Redis server

tests/server_commands.py

+22
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,28 @@ def test_zscore(self):
648648
self.make_zset('a', {'a1' : 1, 'a2' : 2, 'a3' : 3})
649649
self.assertEquals(self.client.zscore('a', 'a2'), 2.0)
650650

651+
# HASHES
652+
def make_hash(self, key, d):
653+
for k,v in d.iteritems():
654+
self.client.hset(key, k, v)
655+
656+
def test_hget_and_hset(self):
657+
# TODO: add these back in, but right now they produce a crash bug.
658+
# key is not a hash
659+
# self.client['a'] = 'a'
660+
# self.assertRaises(redis.ResponseError, self.client.hget, 'a', 'a1')
661+
# del self.client['a']
662+
# no key
663+
self.assertEquals(self.client.hget('a', 'a1'), None)
664+
# real logic
665+
self.make_hash('a', {'a1': 1, 'a2': 2, 'a3': 3})
666+
self.assertEquals(self.client.hget('a', 'a1'), '1')
667+
self.assertEquals(self.client.hget('a', 'a2'), '2')
668+
self.assertEquals(self.client.hget('a', 'a3'), '3')
669+
# TODO: Not sure why these don't wokr
670+
# self.assertEquals(self.client.hset('a', 'a2', 5), True)
671+
# self.assertEquals(self.client.hget('a', 'a2'), '5')
672+
651673
# SORT
652674
def test_sort_bad_key(self):
653675
# key is not set

0 commit comments

Comments
 (0)