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

 UVコメント

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
4

0 に答える 0