Skip to content

Commit

Permalink
added HGET/HSET commands
Browse files Browse the repository at this point in the history
removed the KEYS callback -- 1.34 Redis servers now return KEYS with the multi-bulk protocol, which means they're already in a list
  • Loading branch information
andymccurdy committed Mar 10, 2010
1 parent c83542e commit 9e3e1aa
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
11 changes: 10 additions & 1 deletion redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ class Redis(threading.local):
{
'BGSAVE': lambda r: r == 'Background saving started',
'INFO': parse_info,
'KEYS': lambda r: r and r.split(' ') or [],
'LASTSAVE': timestamp_to_datetime,
'PING': lambda r: r == 'PONG',
'RANDOMKEY': lambda r: r and r or None,
Expand Down Expand Up @@ -868,6 +867,16 @@ def zscore(self, name, value):
return self.format_bulk('ZSCORE', name, value)


#### HASH COMMANDS ####
def hget(self, name, key):
"Return the value of ``key`` within the hash ``name``"
return self.format_bulk('HGET', name, key)

def hset(self, name, key, value):
"Set ``key`` to ``value`` within hash ``name``"
return self.format_multi_bulk('HSET', name, key, value)


class Pipeline(Redis):
"""
Pipelines provide a way to transmit multiple commands to the Redis server
Expand Down
22 changes: 22 additions & 0 deletions tests/server_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,28 @@ def test_zscore(self):
self.make_zset('a', {'a1' : 1, 'a2' : 2, 'a3' : 3})
self.assertEquals(self.client.zscore('a', 'a2'), 2.0)

# HASHES
def make_hash(self, key, d):
for k,v in d.iteritems():
self.client.hset(key, k, v)

def test_hget_and_hset(self):
# TODO: add these back in, but right now they produce a crash bug.
# key is not a hash
# self.client['a'] = 'a'
# self.assertRaises(redis.ResponseError, self.client.hget, 'a', 'a1')
# del self.client['a']
# no key
self.assertEquals(self.client.hget('a', 'a1'), None)
# real logic
self.make_hash('a', {'a1': 1, 'a2': 2, 'a3': 3})
self.assertEquals(self.client.hget('a', 'a1'), '1')
self.assertEquals(self.client.hget('a', 'a2'), '2')
self.assertEquals(self.client.hget('a', 'a3'), '3')
# TODO: Not sure why these don't wokr
# self.assertEquals(self.client.hset('a', 'a2', 5), True)
# self.assertEquals(self.client.hget('a', 'a2'), '5')

# SORT
def test_sort_bad_key(self):
# key is not set
Expand Down

0 comments on commit 9e3e1aa

Please sign in to comment.