Shopify / response_bank

Simple response caching for Ruby applications
http://www.shopify.com
MIT License
135 stars 18 forks source link

Use this in an API Controller? #85

Open walterdavis opened 8 months ago

walterdavis commented 8 months ago

I'm not 100% sure this is intended use, but I just bodged my way into adding this to a API controller, and it appears to work. The error that I hit when I first tried was:

A NoMethodError occurred in titles#show:

 undefined method `cache_configured?' for #<Api::V1::TitlesController:0x0000000003e7d8>

     unless cache_configured? && cacheable_req
            ^^^^^^^^^^^^^^^^^
Did you mean?  cache_version_data
 app/controllers/api/v1/titles_controller.rb:9:in `show'

(Rails 7.0.8.1)

And that's confusing, unless this line doesn't do what I imagine it does.

Which led me down the way to a bit of example code in one of the tests, which seems to also work in my API controller. I wonder if this leaves anything out that the other controllers get? All the other (non-API) routes seem to work just fine.

module Api
  module V1
    class TitlesController < ActionController::API
      helper ApplicationHelper, PeopleHelper
      include ResponseBank::Controller # <--- this

      def show
        response_cache do
          @title = Title.published.includes(:headings, :roles, :collections).friendly.find(params[:id])
        end
      end

      private

      # and this method
      def cache_configured?
        true
      end
    end
  end
end

I'm going to try this in production for a couple of minutes, but are there any alarm bells going off for you?

walterdavis commented 8 months ago

Follow-up: this seems to work just great. Might be worth a documentation note for anyone else who tries this on an API controller. Please let me know if you'd like a PR.