mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Don't clear view cache during concurrent requests
This updates ActionView::CacheExpiry to hold a lock while inside the executor (ie. inside a request) and to only clear caches when that is done. This is done using Concurrent::ReadWriteLock. This allows any number of parallel requests to hold the read lock, but once we detect a change and begin to acquire the write lock, all future requests will be blocked.
This commit is contained in:
parent
6ebd134a9a
commit
9a4c1e205e
2 changed files with 53 additions and 39 deletions
|
@ -4,31 +4,48 @@ module ActionView
|
||||||
class CacheExpiry
|
class CacheExpiry
|
||||||
class Executor
|
class Executor
|
||||||
def initialize(watcher:)
|
def initialize(watcher:)
|
||||||
@cache_expiry = CacheExpiry.new(watcher: watcher)
|
@execution_lock = Concurrent::ReadWriteLock.new
|
||||||
end
|
@cache_expiry = ViewModificationWatcher.new(watcher: watcher) do
|
||||||
|
clear_cache
|
||||||
def before(target)
|
|
||||||
@cache_expiry.clear_cache_if_necessary
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(watcher:)
|
def run
|
||||||
|
ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
|
||||||
|
@cache_expiry.execute_if_updated
|
||||||
|
@execution_lock.acquire_read_lock
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def complete(_)
|
||||||
|
@execution_lock.release_read_lock
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def clear_cache
|
||||||
|
@execution_lock.with_write_lock do
|
||||||
|
ActionView::LookupContext::DetailsKey.clear
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class ViewModificationWatcher
|
||||||
|
def initialize(watcher:, &block)
|
||||||
@watched_dirs = nil
|
@watched_dirs = nil
|
||||||
@watcher_class = watcher
|
@watcher_class = watcher
|
||||||
@watcher = nil
|
@watcher = nil
|
||||||
@mutex = Mutex.new
|
@mutex = Mutex.new
|
||||||
|
@block = block
|
||||||
end
|
end
|
||||||
|
|
||||||
def clear_cache_if_necessary
|
def execute_if_updated
|
||||||
@mutex.synchronize do
|
@mutex.synchronize do
|
||||||
watched_dirs = dirs_to_watch
|
watched_dirs = dirs_to_watch
|
||||||
return if watched_dirs.empty?
|
return if watched_dirs.empty?
|
||||||
|
|
||||||
if watched_dirs != @watched_dirs
|
if watched_dirs != @watched_dirs
|
||||||
@watched_dirs = watched_dirs
|
@watched_dirs = watched_dirs
|
||||||
@watcher = @watcher_class.new([], watched_dirs) do
|
@watcher = @watcher_class.new([], watched_dirs, &@block)
|
||||||
clear_cache
|
|
||||||
end
|
|
||||||
@watcher.execute
|
@watcher.execute
|
||||||
else
|
else
|
||||||
@watcher.execute_if_updated
|
@watcher.execute_if_updated
|
||||||
|
@ -36,10 +53,6 @@ module ActionView
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def clear_cache
|
|
||||||
ActionView::LookupContext::DetailsKey.clear
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
def dirs_to_watch
|
def dirs_to_watch
|
||||||
all_view_paths.grep(FileSystemResolver).map!(&:path).tap(&:uniq!).sort!
|
all_view_paths.grep(FileSystemResolver).map!(&:path).tap(&:uniq!).sort!
|
||||||
|
@ -49,4 +62,5 @@ module ActionView
|
||||||
ActionView::ViewPaths.all_view_paths.flat_map(&:paths)
|
ActionView::ViewPaths.all_view_paths.flat_map(&:paths)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -98,7 +98,7 @@ module ActionView
|
||||||
end
|
end
|
||||||
|
|
||||||
unless enable_caching
|
unless enable_caching
|
||||||
app.executor.to_run ActionView::CacheExpiry::Executor.new(watcher: app.config.file_watcher)
|
app.executor.register_hook ActionView::CacheExpiry::Executor.new(watcher: app.config.file_watcher)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue