From a00abc00208d8acd561f32c25ef78f30394c7aa3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Mon, 31 Aug 2015 20:26:30 +0200 Subject: [PATCH 1/6] adds touch to debates on tag destroy --- config/initializers/acts_as_taggable_on.rb | 7 +++++++ spec/lib/cache_spec.rb | 14 ++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 spec/lib/cache_spec.rb diff --git a/config/initializers/acts_as_taggable_on.rb b/config/initializers/acts_as_taggable_on.rb index 8b1378917..dcccf9429 100644 --- a/config/initializers/acts_as_taggable_on.rb +++ b/config/initializers/acts_as_taggable_on.rb @@ -1 +1,8 @@ +ActsAsTaggableOn::Tagging.class_eval do + after_destroy :touch_taggable + def touch_taggable + taggable.touch + end + +end \ No newline at end of file diff --git a/spec/lib/cache_spec.rb b/spec/lib/cache_spec.rb new file mode 100644 index 000000000..680799d2e --- /dev/null +++ b/spec/lib/cache_spec.rb @@ -0,0 +1,14 @@ +require 'rails_helper' + +describe 'Cache flow' do + + describe 'Tag destroy' do + it 'invalidates Debate cache keys' do + debate = create(:debate, tag_list: "Good, Bad") + tag = ActsAsTaggableOn::Tag.find_by(name: "Bad") + + expect{tag.destroy}.to change {debate.reload.cache_key} + end + end + +end From 0e68ee21fbcfaf3002a8ca8fc51cd4f63b076602 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Mon, 31 Aug 2015 20:38:28 +0200 Subject: [PATCH 2/6] adds dalli gem for caching --- Gemfile | 1 + Gemfile.lock | 2 ++ 2 files changed, 3 insertions(+) diff --git a/Gemfile b/Gemfile index 21929a806..ce51295bc 100644 --- a/Gemfile +++ b/Gemfile @@ -41,6 +41,7 @@ gem 'initialjs-rails', '0.2.0' gem 'unicorn' gem 'paranoia' gem 'savon' +gem 'dalli' gem 'ahoy_matey', '~> 1.2.1' gem 'groupdate' # group temporary data diff --git a/Gemfile.lock b/Gemfile.lock index 60af34f06..922a30d37 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -111,6 +111,7 @@ GEM simplecov (~> 0.10.0) term-ansicolor (~> 1.3) thor (~> 0.19.1) + dalli (2.7.4) database_cleaner (1.4.1) debug_inspector (0.0.2) devise (3.5.2) @@ -402,6 +403,7 @@ DEPENDENCIES ckeditor coffee-rails (~> 4.1.0) coveralls + dalli database_cleaner devise email_spec From 574133a50bdae6f9bd42fd29904c298c4a96b654 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Mon, 31 Aug 2015 20:38:52 +0200 Subject: [PATCH 3/6] updates environments files to use dalli store --- config/environments/development.rb | 2 +- config/environments/production.rb | 2 +- config/environments/staging.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/config/environments/development.rb b/config/environments/development.rb index 566d4ebc5..1b401544a 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -44,7 +44,7 @@ Rails.application.configure do # Raises error for missing translations # config.action_view.raise_on_missing_translations = true - config.cache_store = :null_store + config.cache_store = :dalli_store config.after_initialize do Bullet.enable = true diff --git a/config/environments/production.rb b/config/environments/production.rb index 7a21a1df4..e24d08985 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -55,7 +55,7 @@ Rails.application.configure do # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new) # Use a different cache store in production. - config.cache_store = :null_store + config.cache_store = :dalli_store # Enable serving of images, stylesheets, and JavaScripts from an asset server. # config.action_controller.asset_host = 'http://assets.example.com' diff --git a/config/environments/staging.rb b/config/environments/staging.rb index 7a21a1df4..e24d08985 100644 --- a/config/environments/staging.rb +++ b/config/environments/staging.rb @@ -55,7 +55,7 @@ Rails.application.configure do # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new) # Use a different cache store in production. - config.cache_store = :null_store + config.cache_store = :dalli_store # Enable serving of images, stylesheets, and JavaScripts from an asset server. # config.action_controller.asset_host = 'http://assets.example.com' From ef9a386b2b4a6e43a0d56ec2392a6eaebecd8bd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Mon, 31 Aug 2015 20:39:54 +0200 Subject: [PATCH 4/6] adds cache key helper for locale+user status --- app/helpers/cache_keys_helper.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 app/helpers/cache_keys_helper.rb diff --git a/app/helpers/cache_keys_helper.rb b/app/helpers/cache_keys_helper.rb new file mode 100644 index 000000000..088b45632 --- /dev/null +++ b/app/helpers/cache_keys_helper.rb @@ -0,0 +1,16 @@ +module CacheKeysHelper + def locale_and_user_status + @cache_key_user ||= calculate_user_status + "#{I18n.locale}/user:#{@cache_key_user}" + end + + def calculate_user_status + user_status = if user_signed_in? && current_user.verified_at.present? + "verified" + elsif user_signed_in? + "signed" + else + "visitor" + end + end +end \ No newline at end of file From 7246c181d5badd079a7d4ee72387ee3a7f110492 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Mon, 31 Aug 2015 20:40:48 +0200 Subject: [PATCH 5/6] adds cache to _debate view --- app/views/debates/_debate.html.erb | 44 ++++++++++++++++-------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/app/views/debates/_debate.html.erb b/app/views/debates/_debate.html.erb index 2d1e2deb4..2d8ad2900 100644 --- a/app/views/debates/_debate.html.erb +++ b/app/views/debates/_debate.html.erb @@ -1,28 +1,30 @@ -
-
-
+<% cache [locale_and_user_status, debate] do %> +
+
+
-
-
- <%= t("debates.debate.debate") %> - -

<%= link_to debate.title, debate %>

-

-   - <%= link_to t("debates.debate.comments", count: debate.comments_count), debate_path(debate, anchor: "comments") %> -

-
- <%= link_to debate.description, debate %> -
+
+
+ <%= t("debates.debate.debate") %> + +

<%= link_to debate.title, debate %>

+

+   + <%= link_to t("debates.debate.comments", count: debate.comments_count), debate_path(debate, anchor: "comments") %> +

+
+ <%= link_to debate.description, debate %> +
+
+ <%= render "shared/tags", debate: debate, limit: 5 %>
- <%= render "shared/tags", debate: debate, limit: 5 %>
-
-
- <%= render 'debates/votes', debate: debate %> -
+
+ <%= render 'debates/votes', debate: debate %> +
+
-
+<% end %> From 8204e51ea2d0d85bca07fc0c4bb81b6372208409 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Mon, 31 Aug 2015 20:47:11 +0200 Subject: [PATCH 6/6] adds cache to featured debates (welcome/index) --- app/views/welcome/_featured_debate.html.erb | 44 +++++++++++---------- app/views/welcome/index.html.erb | 20 +++++----- 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/app/views/welcome/_featured_debate.html.erb b/app/views/welcome/_featured_debate.html.erb index 7ad53608b..00fe7ce73 100644 --- a/app/views/welcome/_featured_debate.html.erb +++ b/app/views/welcome/_featured_debate.html.erb @@ -1,28 +1,30 @@ -
-