Created
May 16, 2013 07:49
-
-
Save hlidotbe/5590097 to your computer and use it in GitHub Desktop.
Closure Table in redis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def insert(item, parent, comment) | |
cid = @redis.incr 'comments_counter' | |
parent_path = @redis.zrangebyscore("asc:#{item}:#{parent}", "0", "+inf", with_scores: true) if parent | |
parent_path ||= [] | |
@redis.multi do |multi| | |
multi.set "comments:#{cid}", { | |
data: comment, | |
item: item, | |
date: Time.now, | |
status: 'saved', | |
edited: false | |
}.to_json | |
multi.rpush "comments_for_item:#{item}", cid | |
if parent | |
asc_key = "asc:#{item}:#{cid}" | |
parent_path.each do |p| | |
multi.zadd asc_key, p[1].to_i+1, p[0] | |
multi.zadd "desc:#{item}:#{p[0]}", p[1].to_i+1, cid | |
end | |
multi.zadd asc_key, 1, parent | |
multi.zadd "desc:#{item}:#{parent}", 1, cid | |
end | |
end | |
cid | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment