From 7c7c39149ca44a462da6a96e24aeee8570104773 Mon Sep 17 00:00:00 2001 From: Ben Halpern Date: Tue, 29 Jan 2019 13:27:22 -0500 Subject: [PATCH] Modify cache keys for well-trafficked views. (#1681) * Add cache to sidebar sticky to make it stick longer * Add last_article_at and last_comment_at * Clean up schema.rb --- app/decorators/organization_decorator.rb | 6 ++++++ app/decorators/user_decorator.rb | 7 +++++++ app/models/article.rb | 4 ++-- app/models/comment.rb | 1 + app/views/articles/show.html.erb | 2 +- app/views/users/_comments_section.html.erb | 2 +- db/migrate/20190129173611_add_last_article_at_to_users.rb | 7 +++++++ db/schema.rb | 5 ++++- 8 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20190129173611_add_last_article_at_to_users.rb diff --git a/app/decorators/organization_decorator.rb b/app/decorators/organization_decorator.rb index 348f99730..cdded3b0a 100644 --- a/app/decorators/organization_decorator.rb +++ b/app/decorators/organization_decorator.rb @@ -25,4 +25,10 @@ class OrganizationDecorator < ApplicationDecorator text: "#ffffff" } end + + def key_profile_attributes + "#{bg_color_hex}-#{text_color_hex}-#{path}-#{tag_line}- + #{email}-#{company_size}-#{location}-#{summary}-#{cta_processed_html}- + #{cta_button_url}-#{cta_button_text}-#{profile_image_url}-#{name}" + end end diff --git a/app/decorators/user_decorator.rb b/app/decorators/user_decorator.rb index 96b538c35..c7eecbee1 100644 --- a/app/decorators/user_decorator.rb +++ b/app/decorators/user_decorator.rb @@ -76,4 +76,11 @@ class UserDecorator < ApplicationDecorator ] colors[id % 10] end + + def key_profile_attributes + "#{bg_color_hex}-#{text_color_hex}-#{path}-#{tag_line}- + #{email}-#{email_public}-#{employer_name}-#{employment_title}- + #{employer_url}-#{location}-#{education}-#{summary}- + #{looking_for_work_publicly}-#{created_at}-#{profile_image_url}-#{name}" + end end diff --git a/app/models/article.rb b/app/models/article.rb index 8dca43fb9..331ab48f5 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -494,8 +494,6 @@ class Article < ApplicationRecord def set_published_date if published && published_at.blank? self.published_at = Time.current - user.delay.resave_articles # tack-on functionality HACK - organization&.delay&.resave_articles # tack-on functionality HACK end end @@ -510,6 +508,8 @@ class Article < ApplicationRecord def set_last_comment_at if published_at.present? && last_comment_at == "Sun, 01 Jan 2017 05:00:00 UTC +00:00" self.last_comment_at = published_at + user.touch(:last_article_at) + organization&.touch(:last_article_at) end end diff --git a/app/models/comment.rb b/app/models/comment.rb index dbb2de697..bbdce59c2 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -262,6 +262,7 @@ class Comment < ApplicationRecord def touch_user user.touch + user.touch(:last_comment_at) end handle_asynchronously :touch_user diff --git a/app/views/articles/show.html.erb b/app/views/articles/show.html.erb index 75cae01bd..430db26ef 100644 --- a/app/views/articles/show.html.erb +++ b/app/views/articles/show.html.erb @@ -222,7 +222,7 @@ -<% cache("sticky-sidebar-#{@article.id}-#{(@organization || @user).updated_at}-#{user_signed_in?}-#{@variant_number}", :expires_in => 24.hours) do %> +<% cache("sticky-sidebar-#{@article.id}-#{(@organization || @user).decorate.key_profile_attributes}-#{(@organization || @user).last_article_at}-#{user_signed_in?}-#{@variant_number}", :expires_in => 24.hours) do %> <%= render "articles/sticky_nav" %> <% end %> diff --git a/app/views/users/_comments_section.html.erb b/app/views/users/_comments_section.html.erb index 7a0913896..57bc853eb 100644 --- a/app/views/users/_comments_section.html.erb +++ b/app/views/users/_comments_section.html.erb @@ -1,4 +1,4 @@ -<% cache("user-profile-comments-#{@user.updated_at}-#{@user.id}-#{@comments.first&.id}-#{@comments.size}", :expires_in => 1.day) do %> +<% cache("user-profile-comments-#{@user.last_comment_at}-#{@user.id}-#{@comments.size}", :expires_in => 2.day) do %>