I've been using the redis-cli to get the hang of how redis works. I understand that using this tool I can do this:
127.0.0.1:6379> set post:1:title "Redis is cool!"
OK
127.0.0.1:6379> set post:1:author "haye321"
OK
127.0.0.1:6379> get post:1:title
"Redis is cool!"
What I cannot seem to figure out is how I would accomplish this with redis-py. It doesn't seem the set
commanded provided allows for an object-type or id. Thanks for your help.
You are setting individual fields of a Redis hash one by one (a hash is the common data structure in Redis to store objects).
A better way to do this is to use Redis HMSET command, that allows to set multiple fields of a given hash in one operation. Using Redis-py it will look like this:
import redis
redisdb = redis.Redis(host="localhost",db=1)
redisdb.hmset('post:1', {'title':'Redis is cool!', 'author':'haye321'})
update:
Of course you could have set Hash field members one by one using the HSET command, but it's less efficient as it requires one request per field:
import redis
redisdb = redis.Redis(host="localhost",db=1)
redisdb.hset('post:1', 'title', 'Redis is cool!')
redisdb.hset('post:1', 'author', 'haye321'})