Skip to content

Commit

Permalink
Speedup user lookup by also caching if user could not be found
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Jun 6, 2012
1 parent da769c9 commit 525cfe9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
20 changes: 10 additions & 10 deletions Gemfile.lock
@@ -1,25 +1,25 @@
PATH
remote: .
specs:
warden (1.1.1)
warden (1.2.0)
rack (>= 1.0)

GEM
remote: http://rubygems.org/
specs:
diff-lcs (1.1.2)
diff-lcs (1.1.3)
rack (1.3.0)
rack-test (0.6.0)
rack (>= 1.0)
rake (0.8.7)
rspec (2.5.0)
rspec-core (~> 2.5.0)
rspec-expectations (~> 2.5.0)
rspec-mocks (~> 2.5.0)
rspec-core (2.5.1)
rspec-expectations (2.5.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.5.0)
rspec (2.10.0)
rspec-core (~> 2.10.0)
rspec-expectations (~> 2.10.0)
rspec-mocks (~> 2.10.0)
rspec-core (2.10.1)
rspec-expectations (2.10.0)
diff-lcs (~> 1.1.3)
rspec-mocks (2.10.1)

PLATFORMS
ruby
Expand Down
6 changes: 4 additions & 2 deletions lib/warden/proxy.rb
Expand Up @@ -192,10 +192,12 @@ def user(argument = {})
opts = argument.is_a?(Hash) ? argument : { :scope => argument }
scope = (opts[:scope] ||= @config.default_scope)

@users[scope] ||= begin
if @users.has_key?(scope)
@users[scope]
else
user = session_serializer.fetch(scope)
opts[:event] = :fetch
set_user(user, opts) if user
@users[scope] = user ? set_user(user, opts) : nil
end
end

Expand Down
20 changes: 20 additions & 0 deletions spec/warden/proxy_spec.rb
Expand Up @@ -417,6 +417,16 @@
setup_rack(app).call(@env)
end

it "should cache unfound user" do
Warden::SessionSerializer.any_instance.should_receive(:fetch).once
app = lambda do |env|
env['warden'].user.should be_nil
env['warden'].user.should be_nil
valid_response
end
setup_rack(app).call(@env)
end

describe "previously logged in" do
before(:each) do
@env['rack.session']['warden.user.default.key'] = "A Previous User"
Expand All @@ -431,6 +441,16 @@
setup_rack(app).call(@env)
end

it "should cache found user" do
Warden::SessionSerializer.any_instance.should_receive(:fetch).once.and_return "A Previous User"
app = lambda do |env|
env['warden'].user.should == "A Previous User"
env['warden'].user.should == "A Previous User"
valid_response
end
setup_rack(app).call(@env)
end

it "should not run strategies when the user exists in the session" do
app = lambda do |env|
env['warden'].authenticate!(:pass)
Expand Down

0 comments on commit 525cfe9

Please sign in to comment.