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

Support of ActionController::API #46

Open
abrisse opened this issue Apr 25, 2017 · 9 comments · May be fixed by #80
Open

Support of ActionController::API #46

abrisse opened this issue Apr 25, 2017 · 9 comments · May be fixed by #80

Comments

@abrisse
Copy link

abrisse commented Apr 25, 2017

Currently this gem does not bring caching features to ActionController::API because:

  • ActionController::Base does not include ActionController::Caching [Link]
  • This gem only includes ActionController::Caching::Actions in ActionController::Base [Link]

Is there any specific reason for that?

@jeroenhouben
Copy link

@abrisse i included these modules, but action_caching still doens't seem to work.

class V1::BaseController < ActionController::API
  include ActionController::HttpAuthentication::Basic::ControllerMethods
  include ActionController::Caching
  include ActionController::Caching::Actions

p.s. I have added the gem.

@jeroenhouben
Copy link

I just inherited from ActionController::Base and now things do work. Still think this could be address though.

@uxxman
Copy link

uxxman commented Sep 7, 2017

Facing same issue.
Not working with ActionController::API.
Works when using ActionController::Base

@flvrone
Copy link

flvrone commented May 3, 2018

Are there any better solutions than inheriting from ActionController::Base by now?
@pixeltrix, maybe You could help?

@richmolj
Copy link

This mixin seems to be working for me:

# Have Rails 5 --api app play nicely with actionpack caching
module Cacheable
  def self.prepended(klass)
    klass.class_eval do
      include ActionController::Caching
      include ActionController::Caching::Actions

      attr_accessor :action_has_layout

      def self.cache_store
        @cache_store ||= begin
          store = Rails.application.config.cache_store
          ActiveSupport::Cache.lookup_store(store)
        end
      end
    end
  end

  def cache_store
    @cache_store ||= begin
      store = Rails.application.config.cache_store
      ActiveSupport::Cache.lookup_store(store)
    end
  end
end
class PostsController < ApplicationController
  prepend Cacheable
end

@flvrone
Copy link

flvrone commented Aug 24, 2018

@richmolj looks cool, thanks!
I don't quite understand why are you using prepend there though. Could you please explain it a bit? Wouldn't include give us same result here?

@flvrone
Copy link

flvrone commented Aug 24, 2018

@richmolj hey, I've just tested this stuff, and turns out the code "works" with this mixin, e.g. no errors, but also no actual caching performed (and it is enabled), when I switch it back to inheritance from ActionController::Base - only then the caching is working.

Under "no caching performed" I mean that with action that works with external API, it takes 2000ms to respond, and it responds with same 2000ms every time. And when I use inheritance from ActionController::Base - it's about 8ms after first time.

@flvrone
Copy link

flvrone commented Aug 24, 2018

@rafaelfranca perhaps You could take a look on it?

@richmolj
Copy link

@FunkyloverOne it's actually been a while since I implemented this, so I don't have all the details. I know it has been working for me in production for a couple months, but maybe I missed some other config. You might want to try mixing it in to the controller subclass; I think I had problems with ApplicationController. I'll follow up more if I return to that codebase.

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

Successfully merging a pull request may close this issue.

5 participants