[deploy] Use User reactions_count for Reaction Cache Keys (#7939)
This commit is contained in:
parent
a5e6555469
commit
a326019ba4
4 changed files with 22 additions and 19 deletions
|
|
@ -96,7 +96,7 @@ class ReactionsController < ApplicationController
|
|||
end
|
||||
|
||||
def cached_user_positive_reactions(user)
|
||||
Rails.cache.fetch("cached_user_reactions-#{user.id}-#{user.updated_at}", expires_in: 24.hours) do
|
||||
Rails.cache.fetch("cached_user_reactions-#{user.id}-#{user.positive_reactions_count}", expires_in: 24.hours) do
|
||||
user.reactions.positive
|
||||
end
|
||||
end
|
||||
|
|
@ -115,7 +115,6 @@ class ReactionsController < ApplicationController
|
|||
end
|
||||
|
||||
def destroy_reaction(reaction)
|
||||
current_user.touch
|
||||
reaction.destroy
|
||||
Moderator::SinkArticles.call(reaction.reactable_id) if reaction.vomit_on_user?
|
||||
Notification.send_reaction_notification_without_delay(reaction, reaction.target_user)
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ class ReadingList
|
|||
end
|
||||
|
||||
def cached_ids_of_articles
|
||||
Rails.cache.fetch("reading_list_ids_of_articles_#{user.id}_#{user.updated_at.rfc3339}") do
|
||||
Rails.cache.fetch("reading_list_ids_of_articles_#{user.id}_#{user.positive_reactions_count}") do
|
||||
ids_of_articles
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -35,7 +35,6 @@ class Reaction < ApplicationRecord
|
|||
after_commit :bust_reactable_cache, :update_reactable, on: %i[create update]
|
||||
after_commit :index_to_elasticsearch, if: :indexable?, on: %i[create update]
|
||||
after_commit :remove_from_elasticsearch, if: :indexable?, on: [:destroy]
|
||||
after_save :touch_user
|
||||
|
||||
before_destroy :update_reactable_without_delay, unless: :destroyed_by_association
|
||||
before_destroy :bust_reactable_cache_without_delay
|
||||
|
|
@ -54,7 +53,7 @@ class Reaction < ApplicationRecord
|
|||
|
||||
def cached_any_reactions_for?(reactable, user, category)
|
||||
class_name = reactable.class.name == "ArticleDecorator" ? "Article" : reactable.class.name
|
||||
cache_name = "any_reactions_for-#{class_name}-#{reactable.id}-#{user.updated_at&.rfc3339}-#{category}"
|
||||
cache_name = "any_reactions_for-#{class_name}-#{reactable.id}-#{user.reactions_count}-#{user.positive_reactions_count}-#{category}"
|
||||
Rails.cache.fetch(cache_name, expires_in: 24.hours) do
|
||||
Reaction.where(reactable_id: reactable.id, reactable_type: class_name, user: user, category: category).any?
|
||||
end
|
||||
|
|
@ -97,10 +96,6 @@ class Reaction < ApplicationRecord
|
|||
category == "readinglist" && reactable && reactable.published
|
||||
end
|
||||
|
||||
def touch_user
|
||||
user.touch
|
||||
end
|
||||
|
||||
def update_reactable
|
||||
Reactions::UpdateReactableWorker.perform_async(id)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -13,6 +13,25 @@ RSpec.describe Reaction, type: :model do
|
|||
it { is_expected.to validate_uniqueness_of(:user_id).scoped_to(%i[reactable_id reactable_type category]) }
|
||||
end
|
||||
|
||||
describe "counter_culture" do
|
||||
context "when a reaction is created" do
|
||||
it "increments reaction count on user" do
|
||||
expect do
|
||||
create(:reaction, user: user)
|
||||
end.to change { user.reload.reactions_count }.by(1)
|
||||
end
|
||||
end
|
||||
|
||||
context "when a reaction is destroyed" do
|
||||
it "decrements reaction count on user" do
|
||||
reaction = create(:reaction, user: user)
|
||||
expect do
|
||||
reaction.destroy
|
||||
end.to change { user.reload.reactions_count }.by(-1)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "validations" do
|
||||
it "allows like reaction for users without trusted role" do
|
||||
reaction.category = "like"
|
||||
|
|
@ -235,16 +254,6 @@ RSpec.describe Reaction, type: :model do
|
|||
expect(comment.reload.updated_at).to be > updated_at
|
||||
end
|
||||
end
|
||||
|
||||
it "updates updated_at for the user" do
|
||||
sidekiq_perform_enqueued_jobs do
|
||||
updated_at = user.updated_at
|
||||
Timecop.travel(1.day.from_now) do
|
||||
reaction.save
|
||||
expect(user.reload.updated_at).to be > updated_at
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "when callbacks are called before destroy" do
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue