Skip to content

Commit

Permalink
The FlashHash and friends causes a lot of needless session storing, w…
Browse files Browse the repository at this point in the history
…hen we know for a fact that there's no content in the flash. By not storing the empty hash in the session we save a lot of communication with the various session backends, while still keeping the same interface to the flash. [#2703 state:resolved]

Signed-off-by: Joshua Peek <josh@joshpeek.com>
  • Loading branch information
js authored and josh committed May 28, 2009
1 parent 34a1ed0 commit dc94c09
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
14 changes: 11 additions & 3 deletions actionpack/lib/action_controller/flash.rb
Expand Up @@ -120,6 +120,11 @@ def sweep #:nodoc:
(@used.keys - keys).each{ |k| @used.delete(k) }
end

def store(session, key = "flash")
return if self.empty?
session[key] = self
end

private
# Used internally by the <tt>keep</tt> and <tt>discard</tt> methods
# use() # marks the entire flash as used
Expand All @@ -139,7 +144,10 @@ module InstanceMethods #:nodoc:
protected
def perform_action_with_flash
perform_action_without_flash
remove_instance_variable(:@_flash) if defined? @_flash
if defined? @_flash
@_flash.store(session)
remove_instance_variable(:@_flash)
end
end

def reset_session_with_flash
Expand All @@ -151,8 +159,8 @@ def reset_session_with_flash
# read a notice you put there or <tt>flash["notice"] = "hello"</tt>
# to put a new one.
def flash #:doc:
unless defined? @_flash
@_flash = session["flash"] ||= FlashHash.new
if !defined?(@_flash)
@_flash = session["flash"] || FlashHash.new
@_flash.sweep
end

Expand Down
7 changes: 6 additions & 1 deletion actionpack/test/controller/flash_test.rb
Expand Up @@ -121,7 +121,7 @@ def test_update_flash
assert_nil @response.template.assigns["flash_copy"]["that"], "On second flash"
assert_equal "hello again", @response.template.assigns["flash_copy"]["this"], "On second flash"
end

def test_flash_after_reset_session
get :use_flash_after_reset_session
assert_equal "hello", @response.template.assigns["flashy_that"]
Expand All @@ -139,4 +139,9 @@ def test_sweep_after_halted_filter_chain
get :std_action
assert_nil @response.template.assigns["flash_copy"]["foo"]
end

def test_does_not_set_the_session_if_the_flash_is_empty
get :std_action
assert_nil session["flash"]
end
end

0 comments on commit dc94c09

Please sign in to comment.