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
This commit is contained in:
Ben Halpern 2019-01-29 13:27:22 -05:00 committed by GitHub
parent 043a21494c
commit 7c7c39149c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 29 additions and 5 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -262,6 +262,7 @@ class Comment < ApplicationRecord
def touch_user
user.touch
user.touch(:last_comment_at)
end
handle_asynchronously :touch_user

View file

@ -222,7 +222,7 @@
</div>
</div>
<% 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 %>

View file

@ -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 %>
<style>
.index-comments header {
color: <%= HexComparer.new([user_colors(@user)[:bg], user_colors(@user)[:text]]).brightness(0.88) %>

View file

@ -0,0 +1,7 @@
class AddLastArticleAtToUsers < ActiveRecord::Migration[5.1]
def change
add_column :users, :last_article_at, :datetime, default: "2017-01-01 05:00:00"
add_column :users, :last_comment_at, :datetime, default: "2017-01-01 05:00:00"
add_column :organizations, :last_article_at, :datetime, default: "2017-01-01 05:00:00"
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20190115155656) do
ActiveRecord::Schema.define(version: 20190129173611) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -488,6 +488,7 @@ ActiveRecord::Schema.define(version: 20190115155656) do
t.string "github_username"
t.string "jobs_email"
t.string "jobs_url"
t.datetime "last_article_at", default: "2017-01-01 05:00:00"
t.string "location"
t.string "name"
t.string "nav_image"
@ -754,6 +755,8 @@ ActiveRecord::Schema.define(version: 20190115155656) do
t.string "github_username"
t.string "gitlab_url"
t.jsonb "language_settings", default: {}, null: false
t.datetime "last_article_at", default: "2017-01-01 05:00:00"
t.datetime "last_comment_at", default: "2017-01-01 05:00:00"
t.datetime "last_followed_at"
t.datetime "last_moderation_notification", default: "2017-01-01 05:00:00"
t.datetime "last_notification_activity"