Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for caching both body and headers in Garner::Mixins::Rack #54

Open
dblock opened this issue Sep 5, 2013 · 6 comments
Open

Comments

@dblock
Copy link
Contributor

dblock commented Sep 5, 2013

We have monkey patched the Rack mixin to allow caching both headers and body, like this:

module Garner
  module Cache
    class Identity
      TRACKED_HEADERS = %w{ X-Total-Count }

      alias_method :_fetch, :fetch
      def fetch(&block)
        if @key_hash[:tracked_grape_headers]
          result, headers = _fetch do
            result = yield
            headers = @ruby_context.header.slice(*TRACKED_HEADERS) if @ruby_context.respond_to?(:header)
            [result, headers]
          end

          (headers || {}).each do |key, value|
            @ruby_context.header(key, value) if @ruby_context.respond_to?(:header)
          end

          result
        else
          _fetch(&block)
        end
      end

    end
  end
end
require "garner/mixins/rack"

module Garner
  module Mixins
    module Rack

      alias_method :_garner, :garner
      def garner(&block)
        response, headers = _garner.key({ tracked_grape_headers: true }, &block)
      end

    end
  end
end

It should be possible to roll this functionality into Garner. The Identity part should probably become more generic to store data and metadata, while the Rack mixin would just reuse that functionality.

@fancyremarker
Copy link
Contributor

What would the API for a feature like this look like, in your opinion? I want to be careful not to complicate the API too much to support this feature.

@monfresh
Copy link

+1 for this feature. Without it, I can't use Garner because we send Pagination info via the Link header and other custom headers which clients that consume our API rely on. I'll try @dblock's patch in the meantime.

monfresh added a commit to civictechdc/open211 that referenced this issue Mar 17, 2014
Garner doesn't support this out of the box. I applied this monkey patch:
artsy/garner#54
monfresh added a commit to civictechdc/open211 that referenced this issue Mar 20, 2014
Garner doesn't support this out of the box. I applied this monkey patch:
artsy/garner#54
@onomated
Copy link

+1 Same here. Issues with pagination info in the header

@rdurgarao
Copy link

+1 Same issue with header cache

@scottraio
Copy link

+1

@ShallmentMo
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants