我注意到我的網站上的AJAX調用速度很慢,看着NewRelic RPM
我注意到大部分的成本來自Authlogic/find
。降低authlogic/ajax調用的成本
我想這是因爲rails需要每次查看用戶(即使在AJAX調用中),但147微秒似乎非常慢。
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 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中定義:
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