Sha256: 9c778da1bbf603157eb95d9c65fdb2ed46bc0ae4a4ef581c0e7e7ec5ef9a7a01

Contents?: true

Size: 1.35 KB

Versions: 29

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

module PagesCore
  module StaticCache
    class VarnishHandler
      attr_reader :varnish_url

      def initialize(varnish_url)
        @varnish_url = varnish_url
      end

      def cache_page(_controller, request, response)
        response.set_header("X-Cache-Tags", "static")
        request.session_options[:skip] = true
        # controller.expires_in 1.hour, public: true
      end

      def cache_page_permanently(controller, request, response)
        response.set_header("X-Cache-Tags", "permanent")
        request.session_options[:skip] = true
        controller.expires_in 1.year, public: true
      end

      def purge!
        return unless PagesCore::CacheSweeper.enabled

        hydra = Typhoeus::Hydra.hydra
        hydra.queue(ban_request("static"))
        hydra.queue(ban_request("permanent"))
        hydra.run
      end

      def sweep!
        return unless PagesCore::CacheSweeper.enabled

        # PagesCore::SweepCacheJob.perform_later
        sweep_now!
      end

      def sweep_now!
        return unless PagesCore::CacheSweeper.enabled

        ban_request("static").run
      end

      private

      def ban_request(cache_tag)
        Typhoeus::Request.new(varnish_url,
                              method: :ban,
                              headers: { "X-Cache-Tags": cache_tag })
      end
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
pages_core-3.15.5 lib/pages_core/static_cache/varnish_handler.rb
pages_core-3.15.4 lib/pages_core/static_cache/varnish_handler.rb
pages_core-3.15.3 lib/pages_core/static_cache/varnish_handler.rb
pages_core-3.15.2 lib/pages_core/static_cache/varnish_handler.rb
pages_core-3.15.1 lib/pages_core/static_cache/varnish_handler.rb
pages_core-3.14.0 lib/pages_core/static_cache/varnish_handler.rb
pages_core-3.13.0 lib/pages_core/static_cache/varnish_handler.rb
pages_core-3.12.7 lib/pages_core/static_cache/varnish_handler.rb
pages_core-3.12.6 lib/pages_core/static_cache/varnish_handler.rb
pages_core-3.12.5 lib/pages_core/static_cache/varnish_handler.rb
pages_core-3.12.4 lib/pages_core/static_cache/varnish_handler.rb
pages_core-3.12.3 lib/pages_core/static_cache/varnish_handler.rb
pages_core-3.12.2 lib/pages_core/static_cache/varnish_handler.rb
pages_core-3.12.1 lib/pages_core/static_cache/varnish_handler.rb
pages_core-3.12.0 lib/pages_core/static_cache/varnish_handler.rb
pages_core-3.11.3 lib/pages_core/static_cache/varnish_handler.rb
pages_core-3.11.2 lib/pages_core/static_cache/varnish_handler.rb
pages_core-3.11.1 lib/pages_core/static_cache/varnish_handler.rb
pages_core-3.11.0 lib/pages_core/static_cache/varnish_handler.rb
pages_core-3.10.2 lib/pages_core/static_cache/varnish_handler.rb