我在想一个将如何使用Redis的实现计算器。 这个最难的部分是我应该怎么办的意见,并有它的方式,我不循环得到每个评论upvotes。 我决定修改二进制块。 UVComment是一种复杂的。
每次我在考虑使用哈希我结束了使用集或列表。 我是很新,Redis的,所以我不会感到惊讶,如果我犯了一个错误。 这是精心设计的?
CreatePost /问题(除了标志同样的事情被设置和标签是空的答案
postId=incr postCount
MULTI
rpush p:postId bin[IsQuestion,authorId,title,body,tags,datetimestamp]
foreach tag in tags
sadd tags:tag postId
EXEC
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
给予好评/ Downvote帖子
//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
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
GetPostLogic
lrange p:postId -1 -1 //current revision
scard pUV:postId
scard pDV:postId
comments=lrange post_comment:postId 0 -1 //get all comments