Sha256: 0e5ec53b443ca88a766dfb5e8fcf8e8e99bc5ac257054559b06c7f248e1b1d22

Contents?: true

Size: 1.26 KB

Versions: 8

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

module PagesCore
  module StaticCache
    class PageCacheHandler
      def cache_page(controller, _request, _response)
        controller.class.page_cache_directory = page_cache_directory
        controller.cache_page
      end

      def cache_page_permanently(controller, _request, _response)
        controller.class.page_cache_directory = permanent_page_cache_directory
        controller.cache_page
      end

      def purge!
        return unless PagesCore::CacheSweeper.enabled

        clear_directory(page_cache_directory)
        clear_directory(permanent_page_cache_directory)
      end

      def sweep!
        return unless PagesCore::CacheSweeper.enabled

        PagesCore::SweepCacheJob.perform_later
      end

      def sweep_now!
        return unless PagesCore::CacheSweeper.enabled

        clear_directory(page_cache_directory)
      end

      private

      def cache_root
        Rails.root.join("public")
      end

      def clear_directory(path)
        return unless File.exist?(path)

        FileUtils.rm_rf(Dir.glob("#{path}/*"))
      end

      def page_cache_directory
        cache_root.join("static_cache")
      end

      def permanent_page_cache_directory
        cache_root.join("cache")
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
pages_core-3.9.2 lib/pages_core/static_cache/page_cache_handler.rb
pages_core-3.9.1 lib/pages_core/static_cache/page_cache_handler.rb
pages_core-3.9.0 lib/pages_core/static_cache/page_cache_handler.rb
pages_core-3.8.3 lib/pages_core/static_cache/page_cache_handler.rb
pages_core-3.8.2 lib/pages_core/static_cache/page_cache_handler.rb
pages_core-3.8.1 lib/pages_core/static_cache/page_cache_handler.rb
pages_core-3.8.0 lib/pages_core/static_cache/page_cache_handler.rb
pages_core-3.7.0 lib/pages_core/static_cache/page_cache_handler.rb