reus
2011-07-14 01:00:16 +08:00
在redis里面,key是很占空间的,像二楼那样空间效率是很低的,不应该直接k-v,而是用hash
HASH post:author [post_id] -> author_id
HASH post:comments [post_id] -> packed list of comment_id
HASH post:date [post_id]
...
总之用字段名作为hash名,id作为hash的键,可以节省大量空间。这对于redis这样的内存数据库来说是比较重要的。
如果值是集合类型,那就打包一下,对性能没有影响
还有另外一种存储方式,比上面一种占空间略多,但是可以减少一些请求,性能要好些
就是每个post,comment,author作为hash存储,以post:[post_id],comment:[comment_id],author:[author_id]为hash名称,以字段名(author, comments等)作为hash的key。
这种方式可以用一条指令来完成读写,HMSET或者HGETALL/HMGET,上面那种就需要多条HSET/HGET