From f4eb991a60dd00e0f85db00bbbe4019775d74107 Mon Sep 17 00:00:00 2001 From: Badichan Dmitriy Date: Wed, 20 Jul 2016 12:01:49 +0300 Subject: [PATCH] Cache question --- Gemfile | 1 + Gemfile.lock | 17 ++++++++ app/helpers/application_helper.rb | 6 +++ app/models/answer.rb | 2 +- app/models/comment.rb | 2 +- app/views/answers/_answer.html.slim | 40 +++++++++---------- app/views/questions/_comments.html.slim | 3 +- app/views/questions/index.html.slim | 9 +++-- app/views/questions/show.html.slim | 52 +++++++++++++------------ config/application.rb | 1 + config/environments/development.rb | 2 +- 11 files changed, 82 insertions(+), 53 deletions(-) diff --git a/Gemfile b/Gemfile index f476e30..bf32765 100644 --- a/Gemfile +++ b/Gemfile @@ -54,6 +54,7 @@ gem 'thinking-sphinx' gem 'dotenv' gem 'dotenv-deployment', require: 'dotenv/deployment' gem 'unicorn' +gem 'redis-rails' group :doc do # bundle exec rake doc:rails generates the API under doc/api. diff --git a/Gemfile.lock b/Gemfile.lock index 171c900..d530649 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -310,6 +310,22 @@ GEM rdoc (4.2.2) json (~> 1.4) redis (3.3.0) + redis-actionpack (5.0.0) + actionpack (>= 4.0.0, < 6) + redis-rack (~> 2.0.0.pre) + redis-store (~> 1.2.0.pre) + redis-activesupport (5.0.1) + activesupport (>= 3, < 6) + redis-store (~> 1.2.0) + redis-rack (2.0.0.pre) + rack (> 1.5, < 3) + redis-store (~> 1.2.0.pre) + redis-rails (5.0.1) + redis-actionpack (~> 5.0.0) + redis-activesupport (~> 5.0.0) + redis-store (~> 1.2.0) + redis-store (1.2.0) + redis (>= 2.2) remotipart (1.2.1) responders (2.2.0) railties (>= 4.2.0, < 5.1) @@ -465,6 +481,7 @@ DEPENDENCIES pg private_pub rails (= 4.2) + redis-rails remotipart responders rspec-rails diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index de6be79..a9b1c9d 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,2 +1,8 @@ module ApplicationHelper + def collection_cache_key_for(model) + klass = model.to_s.capitalize.constantize + count = klass.count + max_updated_at = klass.maximum(:updated_at).try(:utc).try(:to_s, :number) + "#{model.to_s.pluralize}/collection-#{count}-#{max_updated_at}" + end end diff --git a/app/models/answer.rb b/app/models/answer.rb index 9e4d61c..98b8868 100644 --- a/app/models/answer.rb +++ b/app/models/answer.rb @@ -2,7 +2,7 @@ class Answer < ActiveRecord::Base include Voteable default_scope { order('accepted DESC') } - belongs_to :question + belongs_to :question, touch: true belongs_to :user has_many :attachments, as: :attachmentable has_many :comments, as: :commentable diff --git a/app/models/comment.rb b/app/models/comment.rb index f65936f..69232c5 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -1,5 +1,5 @@ class Comment < ActiveRecord::Base belongs_to :user - belongs_to :commentable, polymorphic: true + belongs_to :commentable, polymorphic: true, touch: true validates :body, presence: true end diff --git a/app/views/answers/_answer.html.slim b/app/views/answers/_answer.html.slim index 16983ea..d96f323 100644 --- a/app/views/answers/_answer.html.slim +++ b/app/views/answers/_answer.html.slim @@ -5,27 +5,27 @@ div { data-answer = answer.id } span = 'Accepted' - if answer.persisted? + - cache answer do + = render 'questions/vote', voteable: answer + = render 'questions/comments', commentable: answer - = render 'questions/vote', voteable: answer - = render 'questions/comments', commentable: answer + ul.answer-attachments + - answer.attachments.each do |a| + li { data-id = a.id } + = link_to a.file.filename, a.file.url + = ' ' + - if current_user && current_user.author_of?(answer) + = link_to 'Remove attachment', destroy_attachment_path(a), method: 'delete', remote: true - ul.answer-attachments - - answer.attachments.each do |a| - li { data-id = a.id } - = link_to a.file.filename, a.file.url - = ' ' - - if current_user && current_user.author_of?(answer) - = link_to 'Remove attachment', destroy_attachment_path(a), method: 'delete', remote: true + - if current_user + - if current_user.author_of?(answer) + p = link_to 'Remove answer', question_answer_path(@question, answer), method: 'delete', remote: true + p = link_to 'Edit answer', '', class: 'edit-answer-link', data: { answer_id: answer.id } - - if current_user - - if current_user.author_of?(answer) - p = link_to 'Remove answer', question_answer_path(@question, answer), method: 'delete', remote: true - p = link_to 'Edit answer', '', class: 'edit-answer-link', data: { answer_id: answer.id } + - if current_user.author_of?(@question) + p = link_to 'Accept answer', accept_question_answer_path(@question, answer), class: 'accept-answer-link', method: 'post', remote: true - - if current_user.author_of?(@question) - p = link_to 'Accept answer', accept_question_answer_path(@question, answer), class: 'accept-answer-link', method: 'post', remote: true - - = form_for [@question, answer], remote: true, html: {id: "edit-answer-#{answer.id}"} do |f| - = f.label :body, 'Answer' - = f.text_area :body - = f.submit 'Save' + = form_for [@question, answer], remote: true, html: {id: "edit-answer-#{answer.id}"} do |f| + = f.label :body, 'Answer' + = f.text_area :body + = f.submit 'Save' diff --git a/app/views/questions/_comments.html.slim b/app/views/questions/_comments.html.slim index d43b144..b3ffacf 100644 --- a/app/views/questions/_comments.html.slim +++ b/app/views/questions/_comments.html.slim @@ -1,6 +1,7 @@ .comments - commentable.comments.each do |comment| - p= comment.body + - cache comment do + p= comment.body .comments-form = form_for [commentable, commentable.comments.build], remote: true do |f| diff --git a/app/views/questions/index.html.slim b/app/views/questions/index.html.slim index 27eed22..2b527e4 100644 --- a/app/views/questions/index.html.slim +++ b/app/views/questions/index.html.slim @@ -4,9 +4,10 @@ p = 'Is no questions' - else table.questions - - @questions.each do |question| - tr - td= link_to question.title, question - td= question.body + - cache collection_cache_key_for :question do + - @questions.each do |question| + tr + td= link_to question.title, question + td= question.body = subscribe_to "/questions" \ No newline at end of file diff --git a/app/views/questions/show.html.slim b/app/views/questions/show.html.slim index acc46de..d4dcbdf 100644 --- a/app/views/questions/show.html.slim +++ b/app/views/questions/show.html.slim @@ -1,28 +1,30 @@ -.question - = render @question - = render 'vote', voteable: @question - = render 'comments', commentable: @question - - if current_user - - if @subscription = current_user.subscriptions.find_by(question_id: @question.id) - = link_to 'Cancel subscription', question_subscription_path(@question, @subscription), method: 'delete', remote: true, class: 'cancel_subscribe' - - else - = link_to 'Create subscription', question_subscriptions_path(@question), method: 'post', remote: true, class: 'create_subscribe' +- cache @question do + .question + = render @question + = render 'vote', voteable: @question + = render 'comments', commentable: @question + - if current_user + - if @subscription = current_user.subscriptions.find_by(question_id: @question.id) + = link_to 'Cancel subscription', question_subscription_path(@question, @subscription), method: 'delete', remote: true, class: 'cancel_subscribe' + - else + = link_to 'Create subscription', question_subscriptions_path(@question), method: 'post', remote: true, class: 'create_subscribe' -- if current_user && current_user.author_of?(@question) - = link_to 'Remove', question_path(@question), method: 'delete' + - if current_user && current_user.author_of?(@question) + = link_to 'Remove', question_path(@question), method: 'delete' -.answers data={question_id: "#{@question.id}"} - = render @question.answers -= subscribe_to "/questions/#{@question.id}/answers" -= subscribe_to "/questions/#{@question.id}/comments" + .answers data={question_id: "#{@question.id}"} + = render @question.answers + + .answer_form + = form_for [@question, @answer], remote: true do |f| + = f.label :body, 'Your Answer' + .answer-errors + = f.text_area :body + ul.files + = f.fields_for :attachments, @answer.attachments.build do |a| + = render 'attachment_fields', f: a + li = link_to_add_association 'Add file', f, :attachments + = f.submit 'Post Your Answer' -.answer_form - = form_for [@question, @answer], remote: true do |f| - = f.label :body, 'Your Answer' - .answer-errors - = f.text_area :body - ul.files - = f.fields_for :attachments, @answer.attachments.build do |a| - = render 'attachment_fields', f: a - li = link_to_add_association 'Add file', f, :attachments - = f.submit 'Post Your Answer' += subscribe_to "/questions/#{@question.id}/answers" += subscribe_to "/questions/#{@question.id}/comments" \ No newline at end of file diff --git a/config/application.rb b/config/application.rb index 381bae0..8dfa1f5 100644 --- a/config/application.rb +++ b/config/application.rb @@ -32,6 +32,7 @@ class Application < Rails::Application request_specs: false, controller_specs: true g.fixture_replacement :factory_girl, dir: 'spec/factories' + config.cache_store = :redis_store, 'redis://localhost:6379/0/cache', { expares_in: 90.minutes } end end end diff --git a/config/environments/development.rb b/config/environments/development.rb index f76e407..c027a54 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -11,7 +11,7 @@ # Show full error reports and disable caching. config.consider_all_requests_local = true - config.action_controller.perform_caching = false + config.action_controller.perform_caching = true # Don't care if the mailer can't send. config.action_mailer.raise_delivery_errors = false