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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions app/models/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,10 @@ def setup

public

is_impressionable counter_cache: true, column_name: :views
is_impressionable counter_cache: true, column_name: :views, unique: :ip_address

def totalviews
# this doesn't filter out duplicate ip addresses as the line below does:
# self.views + self.legacy_views
impressionist_count(filter: :ip_address) + legacy_views
views + legacy_views
end

def self.weekly_tallies(type = 'note', span = 52, time = Time.now)
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20190401093400_update_node_view_count.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class UpdateNodeViewCount < ActiveRecord::Migration[5.2]
def up
Node.ids.each do |id|
node = Node.find(id)
node.update_columns(views: node.impressionist_count(filter: :ip_address))
end
end

def down
end
end
2 changes: 1 addition & 1 deletion db/schema.rb.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_03_30_043340) do
ActiveRecord::Schema.define(version: 2019_04_01_093400) do

create_table "answer_selections", force: true do |t|
t.integer "user_id"
Expand Down
6 changes: 3 additions & 3 deletions test/functional/notes_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.

get :show,
params: {
author: note.author.name,
Expand All @@ -90,10 +90,10 @@ def teardown
}
end

assert_equal 2, note.totalviews
assert_equal 2, note.reload.totalviews

# same IP won't add to views twice
assert_difference 'note.totalviews', 0 do
assert_difference 'note.reload.totalviews', 0 do
get :show,
params: {
author: note.author.name,
Expand Down
4 changes: 2 additions & 2 deletions test/integration/node_unique_views_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class NodeInsertExtrasTest < ActionDispatch::IntegrationTest
assert_response :success
end

assert_difference 'nodes(:about).totalviews', 0 do
assert_difference 'nodes(:about).reload.totalviews', 0 do
assert_difference 'Impression.count', 0 do
get "/wiki/#{nodes(:about).slug}"
assert_response :success
Expand All @@ -22,7 +22,7 @@ class NodeInsertExtrasTest < ActionDispatch::IntegrationTest
assert_equal '127.0.0.1', Impression.last.ip_address
assert Impression.last.update_attributes(ip_address: '0.0.0.0')

assert_difference 'nodes(:about).totalviews', 1 do
assert_difference 'nodes(:about).reload.totalviews', 1 do
assert_difference 'Impression.count', 1 do
get "/wiki/#{nodes(:about).slug}"
assert_response :success
Expand Down