active_model_serializers: Cache not working

Not sure about how the cache method works but seems not to be working.

class CommentSerializer < ActiveModel::Serializer
  cache only: [:content]
  attributes :id, :content

  def content
     # nokogiri parsing stuff.
  end
end

Even with the cache enabled I can see some objects being allocated by the nokogiri gem.

About this issue

  • Original URL
  • State: open
  • Created 9 years ago
  • Comments: 29 (13 by maintainers)

Most upvoted comments

I recently upgraded my AMS gem from 0.8.3 to 0.10.0.rc1 in hopes of getting some better caching functionality. I am trying to cache some App Labels and the serializer there looks like this:

class AppLabelSerializer < ActiveModel::Serializer
  cache key: 'app_label', expires_in: 3.hours
  attributes :id, :key, :label, :label_plural

  def key
    object.app_label_dictionary.key
  end

end

But I’m not seeing any caching in my local server output. Previously, in the older version of AMS, I used this caching syntax:

class AppLabelSerializer < ActiveModel::Serializer
  cached
  delegate :cache_key, to: :object

  attributes :id, :key, :label, :label_plural
end

And there was a very clear indication in the server output showing when the db was being queried vs. a cache_read. Since switching to the release candidate and using the new caching syntax I’m not seeing any caching. Is there something else that I am missing? Some specific config?