Permalink
Browse files

Speedup user lookup by also caching if user could not be found

  • Loading branch information...
1 parent da769c9 commit 525cfe925ea63b4b56cbadcbbd6f6d8a3f8245c7 @grosser grosser committed Jun 6, 2012
Showing with 34 additions and 12 deletions.
  1. +10 −10 Gemfile.lock
  2. +4 −2 lib/warden/proxy.rb
  3. +20 −0 spec/warden/proxy_spec.rb
View
@@ -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
View
@@ -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
View
@@ -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"
@@ -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)

0 comments on commit 525cfe9

Please sign in to comment.