Skip to content

Commit

Permalink
Use mem_cache_store instead of dalli_store
Browse files Browse the repository at this point in the history
`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
  • Loading branch information
javierm committed Aug 15, 2021
1 parent 1d594d1 commit e01a94d
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 36 deletions.
4 changes: 0 additions & 4 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ class Application < Rails::Application
# 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
Expand Down
2 changes: 1 addition & 1 deletion config/environments/preproduction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
# 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"
Expand Down
2 changes: 1 addition & 1 deletion config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
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
Expand Down
2 changes: 1 addition & 1 deletion config/environments/staging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
# 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"
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
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
12 changes: 6 additions & 6 deletions spec/models/comment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,33 +86,33 @@

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

Expand Down
18 changes: 9 additions & 9 deletions spec/models/debate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -351,48 +351,48 @@

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

Expand Down
18 changes: 9 additions & 9 deletions spec/models/proposal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -386,48 +386,48 @@

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

Expand Down
6 changes: 3 additions & 3 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -484,18 +484,18 @@

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

Expand Down
2 changes: 1 addition & 1 deletion spec/shared/models/map_validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
mappable.save!

expect { map_location.update(latitude: 12.34) }
.to change { mappable.reload.updated_at }
.to change { mappable.reload.cache_version }
end
end
end

0 comments on commit e01a94d

Please sign in to comment.