lib/cachai.rb in cachai-0.0.3 vs lib/cachai.rb in cachai-0.0.4

- old
+ new

@@ -78,11 +78,11 @@ :author_url => data['author_url'], :parent_id => data['parent_id'], :author_ip => request.ip } - post = Post.find_by_path!(data['path']) + post = Post.find_or_create_by_path(data['path']) response = Response.create!(attrs.merge(:post_id => post.id)) @redis.del(redis_key(data['path'])) json({ :status => 'ok', :comment => response }) @@ -121,24 +121,26 @@ def get_comments(path) key = redis_key(path) unless json_list = @redis.get(key) puts "Not cached. Getting from DB: #{path}" - post = Post.find_by_path!(path) - json_list = get_and_sort_comments_for(post).to_json + + if post = Post.find_by_path(path) + json_list = get_and_sort_comments_for(post).to_json + else + json_list = '[]' + end + @redis.set(key, json_list) @redis.expire(key, 60 * 60) # one hour end json_list end def get_and_sort_comments_for(post) result = [] top_level = post.responses.comment.top_level nested = post.responses.comment.nested - - puts top_level.count - puts nested.count top_level.each_with_index do |comment, i| obj = comment.as_json children = nested.select do |nested| nested.parent_id == comment.id