users: drop id_to_name, name_to_id caching.

Changes:

* Drop Users.id_to_name.
* Don't cache Users.name_to_id.
* Replace calls to name_to_id with find_by_name when possible.
* Don't autodefine creator_name in belongs_to_creator.
* Don't autodefine updater_name in belongs_to_updater.
* Instead manually define creator_name / updater_name only on models that need
  to return these fields in the api.

id_to_name was cached to reduce the impact of N+1 query patterns in
certain places, especially in api responses that return creator_name /
updater_name fields. But it still meant we were doing N calls to
memcache. Using `includes` to prefetch users avoids this N+1 pattern.

name_to_id had no need be cached, it was never used in any performance-
sensitive contexts.

Avoiding caching also avoids the need to keep these caches consistent.
This commit is contained in:
evazion
2019-08-18 03:50:43 -05:00
parent 7871dced00
commit 59b277ead1
39 changed files with 70 additions and 91 deletions

View File

@@ -23,7 +23,7 @@ class NotesControllerTest < ActionDispatch::IntegrationTest
is_active: true,
post_id: @note.post_id,
post_tags_match: @note.post.tag_array.first,
creator_name: @note.creator_name,
creator_name: @note.creator.name,
creator_id: @note.creator_id,
}
}

View File

@@ -148,7 +148,7 @@ class UploadsControllerTest < ActionDispatch::IntegrationTest
context "with search parameters" do
should "render" do
search_params = {
uploader_name: @upload.uploader_name,
uploader_name: @upload.uploader.name,
source_matches: @upload.source,
rating: @upload.rating,
has_post: "yes",

View File

@@ -288,7 +288,7 @@ class CommentTest < ActiveSupport::TestCase
assert_equal(<<-EOS.strip_heredoc, comment.quoted_response)
[quote]
#{comment.creator_name} said:
#{comment.creator.name} said:
paragraph one

View File

@@ -1793,7 +1793,7 @@ class PostTest < ActiveSupport::TestCase
post.uploader_id = user2.id
assert_equal(user2.id, post.uploader_id)
assert_equal(user2.id, post.uploader_id)
assert_equal(user2.name, post.uploader_name)
assert_equal(user2.name, post.uploader.name)
end
context "tag post counts" do

View File

@@ -123,10 +123,6 @@ class UserTest < ActiveSupport::TestCase
end
context "name" do
should "be #{Danbooru.config.default_guest_name} given an invalid user id" do
assert_equal(Danbooru.config.default_guest_name, User.id_to_name(-1))
end
should "not contain whitespace" do
# U+2007: https://en.wikipedia.org/wiki/Figure_space
user = FactoryBot.build(:user, :name => "foo\u2007bar")
@@ -152,15 +148,9 @@ class UserTest < ActiveSupport::TestCase
assert_equal(["Name cannot begin or end with an underscore"], user.errors.full_messages)
end
should "be fetched given a user id" do
@user = FactoryBot.create(:user)
assert_equal(@user.name, User.id_to_name(@user.id))
end
should "be updated" do
@user = FactoryBot.create(:user)
@user.update_attribute(:name, "danzig")
assert_equal(@user.name, User.id_to_name(@user.id))
end
end