PERF: Cache user summary data

This commit is contained in:
Rafael dos Santos Silva 2020-07-07 18:57:42 -03:00
parent a4d5d1f51d
commit e0f97c707e
1 changed files with 10 additions and 3 deletions

View File

@ -349,15 +349,18 @@ class UsersController < ApplicationController
@user = fetch_user_from_params(include_inactive: current_user.try(:staff?) || (current_user && SiteSetting.show_inactive_accounts)) @user = fetch_user_from_params(include_inactive: current_user.try(:staff?) || (current_user && SiteSetting.show_inactive_accounts))
raise Discourse::NotFound unless guardian.can_see_profile?(@user) raise Discourse::NotFound unless guardian.can_see_profile?(@user)
summary = UserSummary.new(@user, guardian)
serializer = UserSummarySerializer.new(summary, scope: guardian)
respond_to do |format| respond_to do |format|
format.html do format.html do
@restrict_fields = guardian.restrict_user_fields?(@user) @restrict_fields = guardian.restrict_user_fields?(@user)
render :show render :show
end end
format.json do format.json do
render_json_dump(serializer) summary_json = Discourse.cache.fetch(summary_cache_key(@user), expires_in: 1.hour) do
summary = UserSummary.new(@user, guardian)
serializer = UserSummarySerializer.new(summary, scope: guardian)
MultiJson.dump(serializer)
end
render json: summary_json
end end
end end
end end
@ -1651,4 +1654,8 @@ class UsersController < ApplicationController
def secure_session_confirmed? def secure_session_confirmed?
secure_session["confirmed-password-#{current_user.id}"] == "true" secure_session["confirmed-password-#{current_user.id}"] == "true"
end end
def summary_cache_key(user)
"user_summary:#{user.id}:#{current_user ? current_user.id : 0}"
end
end end