Permalink
Browse files

* added dm-core gem dependency

* fill the cache from data from the database when database reads are needed

* delete session by setting empty data, i.e. setting {}
  • Loading branch information...
1 parent 6fbf4b3 commit 14a8b349b4a7d7178a44340eb31e2d81411e0e89 @mkristian committed Sep 27, 2009
Showing with 45 additions and 4 deletions.
  1. +1 −0 Rakefile
  2. +7 −3 lib/rack_datamapper/session/abstract/store.rb
  3. +37 −1 spec/datamapper_store_spec.rb
View
@@ -11,6 +11,7 @@ require 'yard'
Hoe.new('rack_datamapper', Rack::DataMapper::VERSION) do |p|
p.developer('mkristian', 'm.kristian@web.de')
+ p.extra_deps = ['dm-core']
end
desc 'Install the package as a gem.'
@@ -38,7 +38,7 @@ def get_session(env, sid)
if sid
session =
if @@cache
- @@cache[sid] || @@session_class.get(sid)
+ @@cache[sid] || @@cache[sid] = @@session_class.get(sid)
else
@@session_class.get(sid)
end
@@ -61,7 +61,7 @@ def set_session(env, sid, session_data, options)
@mutex.lock if env['rack.multithread']
session =
if @@cache
- @@cache[sid] || @@session_class.get(sid)
+ @@cache[sid] || @@cache[sid] = @@session_class.get(sid)
else
@@session_class.get(sid)
end
@@ -77,7 +77,11 @@ def set_session(env, sid, session_data, options)
# old_session = new_session.instance_variable_get('@old') || {}
# session = merge_sessions session_id, old_session, new_session, session
session.data = session_data
- if session.save
+ if session_data.empty?
+ @@cache.delete(sid) if @@cache
+ session.destroy
+ false
+ elsif session.save
session.session_id
else
false
@@ -48,6 +48,17 @@ def mock_session(stubs={})
result[0].should_not be_nil
result[1].should == {:id => 123}
end
+
+ it 'should delete empty sessions' do
+ DataMapper::Session::Abstract::Session.should_receive(:create).and_return(mock_session)
+ DataMapper::Session::Abstract::Session.should_receive(:get).and_return(mock_session)
+ mock_session.should_receive(:data).and_return({})
+ mock_session.should_receive(:data=).with({})
+ mock_session.should_receive(:destroy).and_return(true)
+
+ session_id = @store.get_session({}, nil)[0]
+ @store.set_session({}, session_id, {}, {}).should be_false
+ end
end
describe 'with cache' do
@@ -74,7 +85,9 @@ def mock_session(stubs={})
it 'should get the session data from storage' do
DataMapper::Session::Abstract::Session.stub!(:get).and_return(mock_session)
- mock_session.should_receive(:data).and_return({:id => "id"})
+ mock_session.should_receive(:data).twice.and_return({:id => "id"})
+ @store.get_session({}, "sid").should == ["sid",{:id => "id"}]
+ # second get should use the cache
@store.get_session({}, "sid").should == ["sid",{:id => "id"}]
end
@@ -88,6 +101,19 @@ def mock_session(stubs={})
result[1].should == {}
end
+ it 'should set the session data with empty cache' do
+ DataMapper::Session::Abstract::Session.should_receive(:get).and_return(mock_session)
+ mock_session.should_receive(:data=).with({:id => 432})
+ mock_session.should_receive(:save).and_return(true)
+ mock_session.should_receive(:session_id).and_return("sid")
+ mock_session.should_receive(:data).and_return({:id => 123})
+ @store.set_session({}, "sid", {:id => 432},{}).should == "sid"
+ result = @store.get_session({}, "sid")
+
+ result[0].should_not be_nil
+ result[1].should == {:id => 123}
+ end
+
it 'should set the session data' do
DataMapper::Session::Abstract::Session.should_receive(:create).and_return(mock_session)
mock_session.should_receive(:data).and_return({})
@@ -103,5 +129,15 @@ def mock_session(stubs={})
result[0].should_not be_nil
result[1].should == {:id => 123}
end
+
+ it 'should delete empty sessions' do
+ DataMapper::Session::Abstract::Session.should_receive(:create).and_return(mock_session)
+ mock_session.should_receive(:data).and_return({})
+ mock_session.should_receive(:data=).with({})
+ mock_session.should_receive(:destroy).and_return(true)
+
+ session_id = @store.get_session({}, nil)[0]
+ @store.set_session({}, session_id, {}, {}).should be_false
+ end
end
end

0 comments on commit 14a8b34

Please sign in to comment.