Skip to content

Commit

Permalink
Use IdentityMap middleware to flush map on each request.
Browse files Browse the repository at this point in the history
  • Loading branch information
miloops committed Nov 19, 2010
1 parent 4da31d2 commit f3722a3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
12 changes: 12 additions & 0 deletions activerecord/lib/active_record/identity_map.rb
Expand Up @@ -95,5 +95,17 @@ def identity_map
ActiveRecord::IdentityMap ActiveRecord::IdentityMap
end end
end end

class Middleware
def initialize(app)
@app = app
end

def call(env)
@app.call(env)
ensure
ActiveRecord::IdentityMap.clear
end
end
end end
end end
3 changes: 3 additions & 0 deletions activerecord/lib/active_record/railtie.rb
Expand Up @@ -22,6 +22,9 @@ class Railtie < Rails::Railtie
config.app_middleware.insert_after "::ActionDispatch::Callbacks", config.app_middleware.insert_after "::ActionDispatch::Callbacks",
"ActiveRecord::ConnectionAdapters::ConnectionManagement" "ActiveRecord::ConnectionAdapters::ConnectionManagement"


config.app_middleware.insert_after "::ActionDispatch::Callbacks",
"ActiveRecord::IdentityMap::Middleware"

rake_tasks do rake_tasks do
load "active_record/railties/databases.rake" load "active_record/railties/databases.rake"
end end
Expand Down
2 changes: 1 addition & 1 deletion railties/test/application/initializers/frameworks_test.rb
Expand Up @@ -166,7 +166,7 @@ def from_bar_helper


require "#{app_path}/config/environment" require "#{app_path}/config/environment"


expects = [ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActiveRecord::SessionStore] expects = [ActiveRecord::IdentityMap::Middleware, ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActiveRecord::SessionStore]
middleware = Rails.application.config.middleware.map { |m| m.klass } middleware = Rails.application.config.middleware.map { |m| m.klass }
assert_equal expects, middleware & expects assert_equal expects, middleware & expects
end end
Expand Down
2 changes: 2 additions & 0 deletions railties/test/application/middleware_test.rb
Expand Up @@ -28,6 +28,7 @@ def app
"ActionDispatch::RemoteIp", "ActionDispatch::RemoteIp",
"Rack::Sendfile", "Rack::Sendfile",
"ActionDispatch::Callbacks", "ActionDispatch::Callbacks",
"ActiveRecord::IdentityMap::Middleware",
"ActiveRecord::ConnectionAdapters::ConnectionManagement", "ActiveRecord::ConnectionAdapters::ConnectionManagement",
"ActiveRecord::QueryCache", "ActiveRecord::QueryCache",
"ActionDispatch::Cookies", "ActionDispatch::Cookies",
Expand Down Expand Up @@ -55,6 +56,7 @@ def app
boot! boot!
assert !middleware.include?("ActiveRecord::ConnectionAdapters::ConnectionManagement") assert !middleware.include?("ActiveRecord::ConnectionAdapters::ConnectionManagement")
assert !middleware.include?("ActiveRecord::QueryCache") assert !middleware.include?("ActiveRecord::QueryCache")
assert !middleware.include?("ActiveRecord::IdentityMap::Middleware")
end end


test "removes lock if allow concurrency is set" do test "removes lock if allow concurrency is set" do
Expand Down

0 comments on commit f3722a3

Please sign in to comment.