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:
@@ -21,7 +21,6 @@ class User < ApplicationRecord
|
||||
:approver,
|
||||
:voter,
|
||||
:super_voter,
|
||||
:verified,
|
||||
]
|
||||
|
||||
# candidates for removal:
|
||||
@@ -84,7 +83,6 @@ class User < ApplicationRecord
|
||||
before_validation :normalize_email
|
||||
before_create :encrypt_password_on_create
|
||||
before_update :encrypt_password_on_update
|
||||
after_save :update_cache
|
||||
before_create :promote_to_admin_if_first_user
|
||||
before_create :customize_new_user
|
||||
has_many :feedback, :class_name => "UserFeedback", :dependent => :destroy
|
||||
@@ -131,15 +129,7 @@ class User < ApplicationRecord
|
||||
concerning :NameMethods do
|
||||
class_methods do
|
||||
def name_to_id(name)
|
||||
Cache.get("uni:#{Cache.hash(name)}", 4.hours) do
|
||||
find_by_name(name).try(:id)
|
||||
end
|
||||
end
|
||||
|
||||
def id_to_name(user_id)
|
||||
Cache.get("uin:#{user_id}", 4.hours) do
|
||||
find_by_id(user_id).try(:name) || Danbooru.config.default_guest_name
|
||||
end
|
||||
find_by_name(name).try(:id)
|
||||
end
|
||||
|
||||
# XXX downcasing is the wrong way to do case-insensitive comparison for unicode (should use casefolding).
|
||||
@@ -155,11 +145,6 @@ class User < ApplicationRecord
|
||||
def pretty_name
|
||||
name.gsub(/([^_])_+(?=[^_])/, "\\1 \\2")
|
||||
end
|
||||
|
||||
def update_cache
|
||||
Cache.put("uin:#{id}", name, 4.hours)
|
||||
Cache.put("uni:#{Cache.hash(name)}", id, 4.hours)
|
||||
end
|
||||
end
|
||||
|
||||
module PasswordMethods
|
||||
|
||||
Reference in New Issue
Block a user