diff options
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG | 3 | ||||
-rw-r--r-- | activesupport/lib/active_support/cache/strategy/local_cache.rb | 52 |
2 files changed, 32 insertions, 23 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 99cea2586b..373236ce9a 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,8 @@ *Rails 3.1.0 (unreleased)* +* LocalCache strategy is now a real middleware class, not an anonymous class +posing for pictures. + * ActiveSupport::Dependencies::ClassCache class has been introduced for holding references to reloadable classes. diff --git a/activesupport/lib/active_support/cache/strategy/local_cache.rb b/activesupport/lib/active_support/cache/strategy/local_cache.rb index 4dce35f1c9..99b26b19fe 100644 --- a/activesupport/lib/active_support/cache/strategy/local_cache.rb +++ b/activesupport/lib/active_support/cache/strategy/local_cache.rb @@ -50,34 +50,40 @@ module ActiveSupport end end - # Middleware class can be inserted as a Rack handler to be local cache for the - # duration of request. - def middleware - @middleware ||= begin - klass = Class.new - klass.class_eval(<<-EOS, __FILE__, __LINE__ + 1) - class << self - def name - "ActiveSupport::Cache::Strategy::LocalCache" - end - alias :to_s :name - end + #-- + # This class wraps up local storage for middlewares. Only the middleware method should + # construct them. + class Middleware # :nodoc: + attr_reader :name, :thread_local_key + alias :to_s :name - def initialize(app) - @app = app - end + def initialize(name, thread_local_key) + @name = name + @thread_local_key = thread_local_key + @app = nil + end - def call(env) - Thread.current[:#{thread_local_key}] = LocalStore.new - @app.call(env) - ensure - Thread.current[:#{thread_local_key}] = nil - end - EOS - klass + def new(app) + @app = app + self + end + + def call(env) + Thread.current[thread_local_key] = LocalStore.new + @app.call(env) + ensure + Thread.current[thread_local_key] = nil end end + # Middleware class can be inserted as a Rack handler to be local cache for the + # duration of request. + def middleware + @middleware ||= Middleware.new( + "ActiveSupport::Cache::Strategy::LocalCache", + thread_local_key) + end + def clear(options = nil) # :nodoc: local_cache.clear(options) if local_cache super |