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

Use node views cache #5338

Merged
merged 2 commits into from
Apr 18, 2019
Merged

Conversation

alonpeer
Copy link
Contributor

@alonpeer alonpeer commented Apr 1, 2019

Fixes #1196

This should improve performance of various page loads, as it avoids
running COUNT() queries on the impressions table, and instead uses the
views value, which the impressions gem keeps up-to-date.

Notice that I update the tests to .reload the node before reading values,
because ActiveRecord caches those, which is why tests fail without it.


Make sure these boxes are checked before your pull request (PR) is ready to be reviewed and merged. Thanks!

  • PR is descriptively titled 📑 and links the original issue above 🔗
  • tests pass -- look for a green checkbox ✔️ a few minutes after opening your PR -- or run tests locally with rake test
  • code is in uniquely-named feature branch and has no merge conflicts 📁
  • screenshots/GIFs are attached 📎 in case of UI updation
  • ask @publiclab/reviewers for help, in a comment below

We're happy to help you get this ready -- don't be afraid to ask for help, and don't be discouraged if your tests fail at first!

If tests do fail, click on the red X to learn why by reading the logs.

Please be sure you've reviewed our contribution guidelines at https://publiclab.org/contributing-to-public-lab-software

Thanks!

Alon Pe'er added 2 commits April 1, 2019 21:30
This should improve performance of various page loads, as it avoids
running COUNT() queries on the impressions table, and instead uses the
views value, which the impressions gem keeps up-to-date.
@alonpeer
Copy link
Contributor Author

alonpeer commented Apr 1, 2019

@publiclab/reviewers @gauravano @jywarren I accidentally deleted my branch and closed PR #5248. This PR replaces it and fixes the conflict.

@plotsbot
Copy link
Collaborator

plotsbot commented Apr 1, 2019

1 Warning
⚠️ New migrations added. Please update schema.rb.example by overwriting it with a copy of the up-to-date db/schema.rb. Also, be aware to preserve the MySQL-specific conditions for full-text indices.
1 Message
📖 @alonpeer Thank you for your pull request! I’m here to help with some tips and recommendations. Please take a look at the list provided and help us review and accept your contribution! And don’t be discouraged if you see errors – we’re here to help.

Generated by 🚫 Danger

Copy link
Member

@SidharthBansal SidharthBansal left a comment

Choose a reason for hiding this comment

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

Hi, changes looks good to me. Thanks for your work.
@gauravano can you please review this and merge this?

@@ -81,7 +81,7 @@ def teardown
assert_equal '0.0.0.0', Impression.last.ip_address
Impression.last.update_attribute('ip_address', '0.0.0.1')

assert_difference 'note.totalviews', 1 do
assert_difference 'note.reload.totalviews', 1 do
Copy link
Member

Choose a reason for hiding this comment

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

Oh interesting. Can we add a bit of documentation about this usage? I'm not familiar with it! Thanks!

Copy link
Member

Choose a reason for hiding this comment

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

Ah! I see your note above:

Notice that I update the tests to .reload the node before reading values,
because ActiveRecord caches those, which is why tests fail without it.

Does it cache it just within the context of the test? Any other situations where we'd need to flush the cache? Thank you!!!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

From my experience with Rails, this is a tests-specific hack. The cache is on request level, so it shouldn't affect any production paths.
In this specific test, we're reading the note, then changing the impressions, then trying to read a value from the note. Since the note is cached (i.e. already read), we have to force it to reload from DB, so that we get the most recent value of totalviews.

@jywarren
Copy link
Member

All right -- this is great then! Merging now!!! 🎉 Thanks for your patience!!!

@jywarren jywarren merged commit 6eb5897 into publiclab:master Apr 18, 2019
SrinandanPai pushed a commit to SrinandanPai/plots2 that referenced this pull request May 5, 2019
* Restore using node.views for unique impressions count

This should improve performance of various page loads, as it avoids
running COUNT() queries on the impressions table, and instead uses the
views value, which the impressions gem keeps up-to-date.

* Added a migration which updates node.views to the count of unique visits
digitaldina pushed a commit to digitaldina/plots2 that referenced this pull request May 12, 2019
* Restore using node.views for unique impressions count

This should improve performance of various page loads, as it avoids
running COUNT() queries on the impressions table, and instead uses the
views value, which the impressions gem keeps up-to-date.

* Added a migration which updates node.views to the count of unique visits
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Impressionist counter_cache not working
4 participants