开发者

Comment System using Redis Database System

开发者 https://www.devze.com 2023-03-19 05:13 出处:网络
I am trying to build a comment system using Redis database, I am currently using hashes to store the comment data, but the problem I am facing is that after 10 or 12 comments, comments lose their orde

I am trying to build a comment system using Redis database, I am currently using hashes to store the comment data, but the problem I am facing is that after 10 or 12 comments, comments lose their order and start appearing randomly, anyone know what data type should be used for building a commenting system using Redis, currently my hashes are of the form.

postid:comments commentid:userid "Testcomment"

开发者_如何学运维

Thanks, Any help will be appreciated.


Hashes are set up for quick access by key rather than retrieval in order. If you need items in a particular order, try a list or sorted set.

The reason it appears to work at first is an optimization for small sets - when you only have a small number of items a list is the most efficient structure, so that is what redis uses internally. When you get more items, an actual hashmap is needed for efficient querying and redis rearranges the data so that it is ordered by hash rather than by insertion order.


With my web app, I am using a format like this.

(appname):(postid):(comment id) - The hash of the posts
(appname):(postid):count - The latest comment id

And then I query the (appname):(postid):count key to get the amount of times I should run a loop that gets the contents of the (appname):(postid):(comment id) hash.

Example Code

$c = $redis->get('(appname):(postid):count');
for($i = 0; $i<$c; $i++) {
    var_dump($redis->hgetall('(appname):(postid):'.$i));
}
0

精彩评论

暂无评论...
验证码 换一张
取 消