7

我在想如何使用redis实现stackoverflow。最困难的部分是我应该如何发表评论并以一种我不会循环的方式来获得每条评论的支持。我决定修改二进制块。UVComment 有点复杂。

每次我考虑使用哈希时,我最终都会使用集合或列表。我对redis很陌生,所以如果我犯了错误我不会感到惊讶。这是精心设计的吗?

 CreatePost/Question(除了设置了标志并且答案中的标签为空外,其他情况相同

postId=incr postCount
MULTI
rpush p:postId bin[IsQuestion,authorId,title,body,tags,datetimestamp]
foreach tag in tags
    sadd tags:tag postId
EXEC

 更新帖子/问题

WATCH p:postId  //dont want a new revision in the meantime
old=lrange p:postId -1 -1 //current but now old
MULTI
rpush p:postId bin[IsQuestion,authorId,title,body,tags,datetimestamp] //new revision
foreach tag in old.tags
    srem tags:tag postId
foreach tag in tags
    sadd tags:tag postId
EXEC

 赞成/反对的帖子

//up
sadd pUV:postId user
srem pDV:postId user
//down
sadd pDV:postId user
srem pUV:postId user
//undo up/down
srem pUV:postId user
srem pDV:postId user

 评论:

commentId=incr commentCount
multi
SET comment_post:commentId postId //for later use when we flag comments. We'll need to know where in the db it is
RPUSH post_comment:postId bin[authorId,text,HasBeenEdited,datetimestamp,commentId, UVCount]
exec

 紫外线评论

watch commentUV:commentId
res=SISMEMBER commentUV:commentId userId
if res>0 //already upvoted
    unwatch
    return
watch post_comment:postId
lrange post_comment:postId 0 -1 //then we find which index matches our commentId
//modify UVCount
MULTI
lset post_comment:postId targetIndex modifiedData
sadd commentUV:commentId userId
EXEC

获取邮政逻辑

lrange p:postId -1 -1 //current revision
scard pUV:postId
scard pDV:postId
comments=lrange post_comment:postId 0 -1 //get all comments
4

0 回答 0