I was thinking how would one implement stackoverflow using redis. The hardest part with this is how i should do comments and have it in a way i don't loop to get each comments upvotes. I decided to modify the binary chunk. UVComment is kind of complex.
Everytime i was thinking of using hashes i ended up using sets or list. I'm pretty new to redis so i won't be surprised if i make a mistake. Is this well designed?
CreatePost/Question (same thing except a flag is set and tags are empty in answers
postId=incr postCount
MULTI
rpush p:postId bin[IsQuestion,authorId,title,body,tags,datetimestamp]
foreach tag in tags
sadd tags:tag postId
EXEC
UpdatePost/Question
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
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
Comment:
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