내 웹 사이트에서 내 AJAX 호출이 느리고, NewRelic RPM
을보고있는 것을 발견했습니다. 대부분의 비용은 Authlogic/find
에서 발생했습니다.Authlogic/find ajax 호출 비용 절감
나는 레일이 매번 (AJAX 호출에서도) 사용자를 검색해야하기 때문에, 147 마이크로 초는 너무 느린 것 같아요. 여기 또한
class LikesController < ApplicationController
## commented this part out in the hopes of reducing authlogic needs, since it's simple for me to check for current_user myself.
# access_control do
# allow :admin
# allow logged_in, :to => [:toggle]
# end
before_filter :check_if_admin, :except => [:toggle]
def index
@likes = Like.all
end
def toggle
if !current_user
render :nothing => true and return
end
like = Like.find(:first, :conditions => "user_id = #{current_user.id} and likable_id = #{params[:likable_id]} and likable_type = '#{params[:likable_type]}'")
if like != nil
like.destroy
@current_user_likes_likable = false
else
like = Like.new(:likable_id => params[:likable_id], :likable_type => params[:likable_type], :user => current_user)
like.save
@current_user_likes_likable = true
end
@likable_id = params[:likable_id]
@likable_type = params[:likable_type]
#@likable = Kernel.const_get(params[:likable_type]).find(params[:likable_id])
render "shared/like"
end
...
CURRENT_USER가와 ApplicationController에 정의되어 어디 :
Category Segment % Time Avg Calls (per Txn) Avg Exclusive Time (ms)
Custom Authlogic/find 77 1.0 147
Controller LikesController#toggle 13 1.0 25
Database SQL - SHOW 4.9 1.5 9.3
Database SQL - OTHER 1.1 3.7 2.1
ActiveRecord User#find 0.8 1.0 1.5
View application.html.haml Template 0.7 0.2 1.3
View _like.html.haml Template 0.5 0.8 0.89
ActiveRecord ActiveRecord::SessionStore::Session#find 0.4 1.0 0.69
여기 내 코드의
class ApplicationController < ActionController::Base
helper :layout
helper_method :current_user_session, :current_user, :current_user_is_admin
private
def current_user_session
return @current_user_session if defined?(@current_user_session)
@current_user_session = UserSession.find
end
def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.record
end