Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reading list counter #10763

Merged
merged 7 commits into from Oct 19, 2020
Merged

Conversation

takmar
Copy link
Contributor

@takmar takmar commented Oct 10, 2020

What type of PR is this? (check all applicable)

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Documentation Update

Description

I found that a wrong reading list count was caused by caching. The cache of cached_reading_list_article_ids is changed only when public_reactions_count is updated, but we must also recache it when user archives and unarchives an article.

To achieve this, I added the last_changed_archive_at column to the users table and cache keys.
I could not come up with an idea to fix this without adding an extra column. Will you let me know If you have better thoughts, I'll give it a try.

Related Tickets & Documents

Closes #9770

QA Instructions, Screenshots, Recordings

Please replace this line with instructions on how to test your changes, as well
as any relevant images for UI changes.

Added tests?

  • yes
  • no, because they aren't needed
  • no, because I need help

Added to documentation?

  • docs.forem.com
  • readme
  • no documentation needed

[optional] Are there any post deployment tasks we need to perform?

[optional] What gif best describes this PR or how it makes you feel?

tenor

@takmar takmar requested a review from a team as a code owner October 10, 2020 10:33
@pr-triage pr-triage bot added the PR: unreviewed bot applied label for PR's with no review label Oct 10, 2020
@rhymes rhymes requested review from mstruve and removed request for a team October 12, 2020 08:41
Copy link
Contributor

@mstruve mstruve left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for tackling this bug. Code looks pretty good, I left one suggestion, let me know what you think!

@@ -0,0 +1,5 @@
class AddLastChangedArchiveAtToUser < ActiveRecord::Migration[6.0]
def change
add_column :users, :last_changed_archive_at, :datetime, default: "2017-01-01 05:00:00"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not a huge fan of adding another "cache" focused column to user but I cant think about a better way to do this.

What do you think of making this a more general column so we could use it for other caches as well? I was thinking last_reacted_at which would fit nicely with our pattern of last_followed_at, last_article_at etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your comments.

I am not a huge fan of adding another "cache" focused column to user

Neither am I. There are already a bunch of cache keys.

What do you think of making this a more general column so we could use it for other caches as well? I was thinking last_reacted_at which would fit nicely with our pattern of last_followed_at, last_article_at etc.

That’s a new idea for me. At least, I think that it is better than mine as we do not need to add a column whenever something related to a user needs to be cached.

Copy link
Contributor

@mstruve mstruve left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small suggestion for the spec but otherwise this LGTM!!!!

@@ -22,11 +22,13 @@
it "returns archives item if no param" do
put "/reading_list_items/#{reaction.id}"
expect(reaction.reload.status).to eq("archived")
expect(user.last_reacted_at).not_to eq(user.reload.last_reacted_at)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than relying on ActiveRecord to local memoize this here, I think a cleaner approach would be.

original_reacted_at = user.last_reacted_at
put "/reading_list_items/#{reaction.id}"
expect(reaction.reload.status).to eq("archived")
expect(original_reacted_at).not_to eq(user.reload.last_reacted_at)

Same with the example below.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also use an actual change assertion here:

expect do 
  put "/reading_list_items/#{reaction.id}"
end.to change { user.reload.last_reacted_at }
expect(reaction.reload.status).to eq("archived")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have never thought of being able to use change by giving put "..." to expect as a block. Thank you!

@pr-triage pr-triage bot added PR: partially-approved bot applied label for PR's where a single reviewer approves changes and removed PR: unreviewed bot applied label for PR's with no review labels Oct 17, 2020
@pr-triage pr-triage bot added PR: unreviewed bot applied label for PR's with no review and removed PR: partially-approved bot applied label for PR's where a single reviewer approves changes labels Oct 17, 2020
@takmar takmar force-pushed the takmar/fix-readinglist-counter branch from 3cda0a2 to 8382369 Compare October 19, 2020 10:28
Copy link
Contributor

@atsmith813 atsmith813 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like there's a small conflict that needs to be resolved. Otherwise, LGTM! Thank you so much, I'm looking forward to this fix 🎉 .

@pr-triage pr-triage bot added PR: partially-approved bot applied label for PR's where a single reviewer approves changes and removed PR: unreviewed bot applied label for PR's with no review labels Oct 19, 2020
@pr-triage pr-triage bot added PR: unreviewed bot applied label for PR's with no review and removed PR: partially-approved bot applied label for PR's where a single reviewer approves changes labels Oct 19, 2020
@mstruve mstruve merged commit 1f17451 into forem:master Oct 19, 2020
@pr-triage pr-triage bot added PR: merged bot applied label for PR's that are merged and removed PR: unreviewed bot applied label for PR's with no review labels Oct 19, 2020
mstruve added a commit that referenced this pull request Oct 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: merged bot applied label for PR's that are merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Reading list counter
4 participants