如何使用redis编写SO帖子?

7 redis

我在想如何使用redis实现stackoverflow.最困难的部分是我应该如何做评论,并以一种我不循环的方式让每个评论upvotes.我决定修改二进制块.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
Run Code Online (Sandbox Code Playgroud)

 UpdatePost /问题

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
Run Code Online (Sandbox Code Playgroud)

 Upvote/Downvote Post

//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
Run Code Online (Sandbox Code Playgroud)

 评论:

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
Run Code Online (Sandbox Code Playgroud)

 UVComment

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
Run Code Online (Sandbox Code Playgroud)

GetPostLogic

lrange p:postId -1 -1 //current revision
scard pUV:postId
scard pDV:postId
comments=lrange post_comment:postId 0 -1 //get all comments
Run Code Online (Sandbox Code Playgroud)