PERF: Memoize allowed user fields more efficiently (#8968)

Previously we were caching by user_id, but the there are only two possible outcomes. Therefore we only need to cache two values.

This removes another N+1 query when serializing multiple user cards.
This commit is contained in:
David Taylor 2020-02-14 14:47:16 +00:00 committed by GitHub
parent 2c7d32e783
commit b37d2f09ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -114,9 +114,13 @@ module UserGuardian
def allowed_user_field_ids(user)
@allowed_user_field_ids ||= {}
@allowed_user_field_ids[user.id] ||=
is_staff_or_is_me = is_staff? || is_me?(user)
cache_key = is_staff_or_is_me ? :staff_or_me : :other
@allowed_user_field_ids[cache_key] ||=
begin
if is_staff? || is_me?(user)
if is_staff_or_is_me
UserField.pluck(:id)
else
UserField.where("show_on_profile OR show_on_user_card").pluck(:id)