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

@@ -1015,7 +1015,7 @@ class Post < ApplicationRecord
end
def uploader_name
User.id_to_name(uploader_id)
uploader.name
end
end
@@ -1507,7 +1507,7 @@ class Post < ApplicationRecord
"created_at" => created_at.to_formatted_s(:db),
"has_notes" => has_notes?,
"rating" => rating,
"author" => uploader_name,
"author" => uploader.name,
"creator_id" => uploader_id,
"width" => image_width,
"source" => source,