From e01a94d7bddb12ed41b88fd37a584db4404b8676 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Sun, 15 Aug 2021 15:17:10 +0200 Subject: [PATCH] Use mem_cache_store instead of dalli_store `dalli_store` is deprecated since dalli 2.7.11. We can now enable cache_versioning. We didn't enable it when upgrading to Rails 5.2 because of possible incompatibility with `dalli_store` [1], even though apparently some the issues were fixed in dalli 2.7.9 and dalli 2.7.10 [2]. Since using cache versioning makes cache expiration more efficient, and I'm not sure whether the options we were passing to the dalli store are valid with memcache store (documentation here is a bit lacking), I'm just removing the option we used to double the default cache size on production. [1] https://www.schneems.com/2018/10/17/cache-invalidation-complexity-rails-52-and-dalli-cache-store [2] https://github.com/petergoldstein/dalli/blob/master/History.md --- config/application.rb | 4 ---- config/environments/preproduction.rb | 2 +- config/environments/production.rb | 2 +- config/environments/staging.rb | 2 +- spec/lib/cache_spec.rb | 2 +- spec/models/comment_spec.rb | 12 ++++++------ spec/models/debate_spec.rb | 18 +++++++++--------- spec/models/proposal_spec.rb | 18 +++++++++--------- spec/models/user_spec.rb | 6 +++--- spec/shared/models/map_validations.rb | 2 +- 10 files changed, 32 insertions(+), 36 deletions(-) diff --git a/config/application.rb b/config/application.rb index 90bf6cd38..650c09369 100644 --- a/config/application.rb +++ b/config/application.rb @@ -28,10 +28,6 @@ module Consul # Use local forms with `form_with`, so it works like `form_for` config.action_view.form_with_generates_remote_forms = false - # Keep disabling cache versioning until we verify it's compatible - # with `:dalli_store` and with the way we cache stats - config.active_record.cache_versioning = false - # Keep using AES-256-CBC for message encryption in case it's used # in any CONSUL installations config.active_support.use_authenticated_message_encryption = false diff --git a/config/environments/preproduction.rb b/config/environments/preproduction.rb index 86926d740..d0b440443 100644 --- a/config/environments/preproduction.rb +++ b/config/environments/preproduction.rb @@ -57,7 +57,7 @@ Rails.application.configure do # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new) # Use a different cache store in production. - config.cache_store = :dalli_store, { value_max_bytes: 2000000 } + config.cache_store = :mem_cache_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/production.rb b/config/environments/production.rb index f0d84f33d..3eb80f2dc 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -56,7 +56,7 @@ Rails.application.configure do config.log_tags = [:request_id] # Use a different cache store in production. - config.cache_store = :dalli_store, { value_max_bytes: 2000000 } + config.cache_store = :mem_cache_store # Use a real queuing backend for Active Job (and separate queues per environment) # config.active_job.queue_adapter = :resque diff --git a/config/environments/staging.rb b/config/environments/staging.rb index c3eb3df2c..d0b440443 100644 --- a/config/environments/staging.rb +++ b/config/environments/staging.rb @@ -57,7 +57,7 @@ Rails.application.configure do # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new) # Use a different cache store in production. - config.cache_store = :dalli_store + config.cache_store = :mem_cache_store # Enable serving of images, stylesheets, and JavaScripts from an asset server. # config.action_controller.asset_host = "http://assets.example.com" diff --git a/spec/lib/cache_spec.rb b/spec/lib/cache_spec.rb index ddc467a69..29408f730 100644 --- a/spec/lib/cache_spec.rb +++ b/spec/lib/cache_spec.rb @@ -6,7 +6,7 @@ describe "Cache flow" do debate = create(:debate, tag_list: "Good, Bad") tag = Tag.find_by(name: "Bad") - expect { tag.destroy }.to change { debate.reload.cache_key } + expect { tag.destroy }.to change { debate.reload.cache_version } end end end diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index ea4ff934f..a08cdb623 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -86,33 +86,33 @@ describe Comment do it "expires cache when it has a new vote" do expect { create(:vote, votable: comment) } - .to change { comment.updated_at } + .to change { comment.cache_version } end it "expires cache when hidden" do expect { comment.hide } - .to change { comment.updated_at } + .to change { comment.cache_version } end it "expires cache when the author is hidden" do expect { comment.user.hide } - .to change { [comment.reload.updated_at, comment.author.updated_at] } + .to change { [comment.reload.cache_version, comment.author.cache_version] } end it "expires cache when the author is erased" do expect { comment.user.erase } - .to change { [comment.reload.updated_at, comment.author.updated_at] } + .to change { [comment.reload.cache_version, comment.author.cache_version] } end it "expires cache when the author changes" do expect { comment.user.update(username: "Isabel") } - .to change { [comment.reload.updated_at, comment.author.updated_at] } + .to change { [comment.reload.cache_version, comment.author.cache_version] } end it "expires cache when the author's organization get verified" do create(:organization, user: comment.user) expect { comment.user.organization.verify } - .to change { [comment.reload.updated_at, comment.author.updated_at] } + .to change { [comment.reload.cache_version, comment.author.cache_version] } end end diff --git a/spec/models/debate_spec.rb b/spec/models/debate_spec.rb index 276b2a21e..318a7377c 100644 --- a/spec/models/debate_spec.rb +++ b/spec/models/debate_spec.rb @@ -351,48 +351,48 @@ describe Debate do it "expires cache when it has a new comment" do expect { create(:comment, commentable: debate) } - .to change { debate.updated_at } + .to change { debate.cache_version } end it "expires cache when it has a new vote" do expect { create(:vote, votable: debate) } - .to change { debate.updated_at } + .to change { debate.cache_version } end it "expires cache when it has a new flag" do expect { create(:flag, flaggable: debate) } - .to change { debate.reload.updated_at } + .to change { debate.reload.cache_version } end it "expires cache when it has a new tag" do expect { debate.update(tag_list: "new tag") } - .to change { debate.updated_at } + .to change { debate.cache_version } end it "expires cache when hidden" do expect { debate.hide } - .to change { debate.updated_at } + .to change { debate.cache_version } end it "expires cache when the author is hidden" do expect { debate.author.hide } - .to change { [debate.reload.updated_at, debate.author.updated_at] } + .to change { [debate.reload.cache_version, debate.author.cache_version] } end it "expires cache when the author is erased" do expect { debate.author.erase } - .to change { [debate.reload.updated_at, debate.author.updated_at] } + .to change { [debate.reload.cache_version, debate.author.cache_version] } end it "expires cache when its author changes" do expect { debate.author.update(username: "Eva") } - .to change { [debate.reload.updated_at, debate.author.updated_at] } + .to change { [debate.reload.cache_version, debate.author.cache_version] } end it "expires cache when the author's organization get verified" do create(:organization, user: debate.author) expect { debate.author.organization.verify } - .to change { [debate.reload.updated_at, debate.author.updated_at] } + .to change { [debate.reload.cache_version, debate.author.cache_version] } end end diff --git a/spec/models/proposal_spec.rb b/spec/models/proposal_spec.rb index e15d796ae..f1f06e96b 100644 --- a/spec/models/proposal_spec.rb +++ b/spec/models/proposal_spec.rb @@ -386,48 +386,48 @@ describe Proposal do it "expires cache when it has a new comment" do expect { create(:comment, commentable: proposal) } - .to change { proposal.updated_at } + .to change { proposal.cache_version } end it "expires cache when it has a new vote" do expect { create(:vote, votable: proposal) } - .to change { proposal.updated_at } + .to change { proposal.cache_version } end it "expires cache when it has a new flag" do expect { create(:flag, flaggable: proposal) } - .to change { proposal.reload.updated_at } + .to change { proposal.reload.cache_version } end it "expires cache when it has a new tag" do expect { proposal.update(tag_list: "new tag") } - .to change { proposal.updated_at } + .to change { proposal.cache_version } end it "expires cache when hidden" do expect { proposal.hide } - .to change { proposal.updated_at } + .to change { proposal.cache_version } end it "expires cache when the author is hidden" do expect { proposal.author.hide } - .to change { [proposal.reload.updated_at, proposal.author.updated_at] } + .to change { [proposal.reload.cache_version, proposal.author.cache_version] } end it "expires cache when the author is erased" do expect { proposal.author.erase } - .to change { [proposal.reload.updated_at, proposal.author.updated_at] } + .to change { [proposal.reload.cache_version, proposal.author.cache_version] } end it "expires cache when its author changes" do expect { proposal.author.update(username: "Eva") } - .to change { [proposal.reload.updated_at, proposal.author.updated_at] } + .to change { [proposal.reload.cache_version, proposal.author.cache_version] } end it "expires cache when the author's organization get verified" do create(:organization, user: proposal.author) expect { proposal.author.organization.verify } - .to change { [proposal.reload.updated_at, proposal.author.updated_at] } + .to change { [proposal.reload.cache_version, proposal.author.cache_version] } end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index a7ecd12b3..262f08063 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -484,18 +484,18 @@ describe User do it "expires cache with becoming a moderator" do expect { create(:moderator, user: user) } - .to change { user.updated_at } + .to change { user.cache_version } end it "expires cache with becoming an admin" do expect { create(:administrator, user: user) } - .to change { user.updated_at } + .to change { user.cache_version } end it "expires cache with becoming a veridied organization" do create(:organization, user: user) expect { user.organization.verify } - .to change { user.reload.updated_at } + .to change { user.reload.cache_version } end end diff --git a/spec/shared/models/map_validations.rb b/spec/shared/models/map_validations.rb index 85ad6b29c..999801fa0 100644 --- a/spec/shared/models/map_validations.rb +++ b/spec/shared/models/map_validations.rb @@ -34,7 +34,7 @@ shared_examples "map validations" do mappable.save! expect { map_location.update(latitude: 12.34) } - .to change { mappable.reload.updated_at } + .to change { mappable.reload.cache_version } end end end