Sha256: 347d6938d0a7fa9136623a7952ba49eabffdcef484f870d2b63052157504011d

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 KB

Contents

require 'reviewed/cache'

module Faraday
  class Cache

    def initialize(app)
      @app = app
    end

    def store
      Reviewed::Cache.store
    end

    def call(env)
      @url = env[:url]

      @website_id = env[:request_headers]['x-reviewed-website']

      if serve_from_cache && store.exist?(cache_key)
        Hashie::Mash.new(Marshal.load( store.read(cache_key) ))
      else
        @app.call(env).on_complete do |response|
          if store_response
            store.delete(cache_key)
            store.write(cache_key, Marshal.dump(response), write_options)
          end
        end
      end
    end

    private

    def serve_from_cache
      @url.query.blank? || !@url.query.match(/\b(skip|reset)-cache\b/)
    end

    def store_response
      @url.query.blank? || !@url.query.match(/\bskip-cache\b/)
    end

    def cache_key
      [@website_id, @url.request_uri].join(':')
    end

    def write_options
      { expires_in: Integer(ENV['REVIEWED_CACHE_TIMEOUT'] || 90).minutes }
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
reviewed-0.6.2 lib/faraday/cache.rb