Skip to content

Commit c8bf334

Browse files
jhawthornkaspth
authored andcommitted
Only clear template caches in dev after changes (rails#35629)
1 parent e4e1218 commit c8bf334

File tree

6 files changed

+61
-7
lines changed

6 files changed

+61
-7
lines changed

actionview/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
* Only clear ActionView cache in development on file changes
2+
3+
To speed up development mode, view caches are only cleared when files in
4+
the view paths have changed. Applications which have implemented custom
5+
ActionView::Resolver subclasses may need to add their own cache clearing.
6+
7+
*John Hawthorn*
8+
19
## Rails 6.0.0.beta3 (March 11, 2019) ##
210

311
* Only accept formats from registered mime types

actionview/lib/action_view.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ module ActionView
8181
end
8282
end
8383

84+
autoload :CacheExpiry
8485
autoload :TestCase
8586

8687
def self.eager_load!
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# frozen_string_literal: true
2+
3+
module ActionView
4+
class CacheExpiry
5+
class Executor
6+
def initialize(watcher:)
7+
@cache_expiry = CacheExpiry.new(watcher: watcher)
8+
end
9+
10+
def before(target)
11+
@cache_expiry.clear_cache_if_necessary
12+
end
13+
end
14+
15+
def initialize(watcher:)
16+
@watched_dirs = nil
17+
@watcher_class = watcher
18+
@watcher = nil
19+
end
20+
21+
def clear_cache_if_necessary
22+
watched_dirs = dirs_to_watch
23+
if watched_dirs != @watched_dirs
24+
@watched_dirs = watched_dirs
25+
@watcher = @watcher_class.new([], watched_dirs) do
26+
clear_cache
27+
end
28+
@watcher.execute
29+
else
30+
@watcher.execute_if_updated
31+
end
32+
end
33+
34+
def clear_cache
35+
ActionView::LookupContext::DetailsKey.clear
36+
end
37+
38+
private
39+
40+
def dirs_to_watch
41+
fs_paths = all_view_paths.grep(FileSystemResolver)
42+
fs_paths.map(&:path).sort.uniq
43+
end
44+
45+
def all_view_paths
46+
ActionView::ViewPaths.all_view_paths.flat_map(&:paths)
47+
end
48+
end
49+
end

actionview/lib/action_view/digestor.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ module ActionView
66
class Digestor
77
@@digest_mutex = Mutex.new
88

9-
module PerExecutionDigestCacheExpiry
10-
def self.before(target)
11-
ActionView::LookupContext::DetailsKey.clear
12-
end
13-
end
14-
159
class << self
1610
# Supported options:
1711
#

actionview/lib/action_view/railtie.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class Railtie < Rails::Engine # :nodoc:
8181
initializer "action_view.per_request_digest_cache" do |app|
8282
ActiveSupport.on_load(:action_view) do
8383
unless ActionView::Resolver.caching?
84-
app.executor.to_run ActionView::Digestor::PerExecutionDigestCacheExpiry
84+
app.executor.to_run ActionView::CacheExpiry::Executor.new(watcher: app.config.file_watcher)
8585
end
8686
end
8787
end

actionview/lib/action_view/template/resolver.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,8 @@ def extract_handler_and_format_and_variant(path)
280280

281281
# A resolver that loads files from the filesystem.
282282
class FileSystemResolver < PathResolver
283+
attr_reader :path
284+
283285
def initialize(path, pattern = nil)
284286
raise ArgumentError, "path already is a Resolver class" if path.is_a?(Resolver)
285287
super(pattern)

0 commit comments

Comments
 (0)