Sha256: b3e1669b194f55065acbd06b1472d3168456348dd013533424254c6dca705a54

Contents?: true

Size: 1.6 KB

Versions: 36

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal: true

module ActionView
  class CacheExpiry
    class Executor
      def initialize(watcher:)
        @execution_lock = Concurrent::ReentrantReadWriteLock.new
        @cache_expiry = ViewModificationWatcher.new(watcher: watcher) do
          clear_cache
        end
      end

      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
        @watcher_class = watcher
        @watcher = nil
        @mutex = Mutex.new
        @block = block
      end

      def execute_if_updated
        @mutex.synchronize do
          watched_dirs = dirs_to_watch
          return if watched_dirs.empty?

          if watched_dirs != @watched_dirs
            @watched_dirs = watched_dirs
            @watcher = @watcher_class.new([], watched_dirs, &@block)
            @watcher.execute
          else
            @watcher.execute_if_updated
          end
        end
      end

      private
        def dirs_to_watch
          all_view_paths.grep(FileSystemResolver).map!(&:path).tap(&:uniq!).sort!
        end

        def all_view_paths
          ActionView::ViewPaths.all_view_paths.flat_map(&:paths)
        end
    end
  end
end

Version data entries

36 entries across 34 versions & 4 rubygems

Version Path
actionview-7.0.8.7 lib/action_view/cache_expiry.rb
actionview-7.0.8.6 lib/action_view/cache_expiry.rb
actionview-7.0.8.5 lib/action_view/cache_expiry.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/actionview-7.0.8.4/lib/action_view/cache_expiry.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/actionview-7.0.5.1/lib/action_view/cache_expiry.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/actionview-7.0.5.1/lib/action_view/cache_expiry.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/actionview-7.0.5.1/lib/action_view/cache_expiry.rb
actionview-7.0.8.4 lib/action_view/cache_expiry.rb
actionview-7.0.8.1 lib/action_view/cache_expiry.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/actionview-7.0.2.3/lib/action_view/cache_expiry.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/actionview-7.0.3.1/lib/action_view/cache_expiry.rb
actionview-7.0.8 lib/action_view/cache_expiry.rb
actionview-7.0.7.2 lib/action_view/cache_expiry.rb
actionview-7.0.7.1 lib/action_view/cache_expiry.rb
actionview-7.0.7 lib/action_view/cache_expiry.rb
actionview-7.0.6 lib/action_view/cache_expiry.rb
actionview-7.0.5.1 lib/action_view/cache_expiry.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/actionview-7.0.2.3/lib/action_view/cache_expiry.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/actionview-7.0.3.1/lib/action_view/cache_expiry.rb
actionview-7.0.5 lib/action_view/cache_expiry.rb