Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Improve the readability of the Resolver change detection code

  • Loading branch information...
commit 67bf728a1f53305c935f29e7ff3e23c5ac58eda5 1 parent 63f3393
@pinetops pinetops authored
Showing with 19 additions and 11 deletions.
  1. +19 −11 actionpack/lib/action_view/template/resolver.rb
View
30 actionpack/lib/action_view/template/resolver.rb
@@ -56,21 +56,14 @@ def cache(key, name, prefix, partial, locals)
# on the CacheEntry itself
cache_entry.synchronize do
if Resolver.caching?
- # all templates are cached forever the first time they are accessed
cache_entry.templates ||= yield
else
- # templates are still cached, but are only returned if they are
- # all still current
- fresh = yield
+ fresh_templates = yield
- mtime = cache_entry.templates && cache_entry.templates.map(&:updated_at).max
-
- newer = !mtime || fresh.empty? || fresh.any? { |t| t.updated_at > mtime }
-
- if newer
- cache_entry.templates = fresh
+ if templates_have_changed?(cache_entry.templates, fresh_templates)
+ cache_entry.templates = fresh_templates
else
- cache_entry.templates
+ cache_entry.templates ||= []
end
end
end
@@ -81,6 +74,21 @@ def clear
@data.clear
end
end
+
+ private
+
+ def templates_have_changed?(cached_templates, fresh_templates)
+ # if either the old or new template list is empty, we don't need to (and can't)
+ # compare modification times, and instead just check whether the lists are different
+ if cached_templates.blank? || fresh_templates.blank?
+ return fresh_templates.blank? != cached_templates.blank?
+ end
+
+ cached_templates_max_updated_at = cached_templates.map(&:updated_at).max
+
+ # if a template has changed, it will be now be newer than all the cached templates
+ fresh_templates.any? { |t| t.updated_at > cached_templates_max_updated_at }
+ end
end
cattr_accessor :caching
Please sign in to comment.
Something went wrong with that request. Please try again.