我在devise token auth
中使用Rails 5 API
gem进行身份验证。
class V1::HuntsController < V1::MainController
include DeviseTokenAuth::Concerns::SetUserByToken
before_action :authenticate_user!
def index
end
end
但是当我查看我的日志时,我发现有两个数据库查询来查找用户。一个来自before_action :authenticate_user
,另一个来自update_auth_header
来自after_action
模块添加的DeviseTokenAuth
。
Started POST "/v1/hunts" for 192.168.0.103 at 2016-12-07 15:41:03 +0530
Processing by V1::HuntsController#create as JSON
Parameters: {"title"=>"dddd", "clue"=>"dd", "hunt"=>{"title"=>"dddd", "clue"=>"dd"}}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."uid" = ? LIMIT ? [["uid", "raj@email.com"], ["LIMIT", 1]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 15], ["LIMIT", 1]]
(0.1ms) begin transaction
(0.0ms) commit transaction
Completed 204 No Content in 102ms (ActiveRecord: 1.0ms)
为什么在current_user
可用时,它会为用户触发另一个数据库查询?数据库查询似乎也没有缓存,因为它需要相同的时间来加载。
我怀疑这是否是由于每个请求更改令牌的默认行为。我已在configuration
中禁用此功能。