josevalim/active_model_serializers — pull request #89 by twinturbo (2012-07-11 13:07:07+00:00)
Title: Serializers Cache JSON & Hashes

This PR adds caching support to serializers. the `to_json`
and `serializable_hash` calls are cached. Two accessors have
been added to `ActiveModel::Serializer`: `perform_caching` and `cache`.
I've done it this way so AMS could have it's own implementation without
having to depend on Rails. The `cache` interface is exactly the same as
`Rails.cache`. In fact, only `fetch` is used. I'd like to refactor some
configuration. I think a lot of the configuration should be moved to a
Railtie. At least, we need a railtie to configure AMS caching. The railtie
would look like this

```ruby
module ActiveModel
class Railtie < ::Rails::Railtie

initializer "activemodel.serializers.cache" do |app|
ActiveModel::Serializer.cache = app.cache
end

intializer "activemodel.serializers.caching" do |app|
ActiveModel::Serializer.perform_caching = app.config.perform_caching
end

# existing load hooks etc
end
end
```

Here's an example serializer:

```ruby
class PostSerializer < ActiveModel::Serializer
cache # sets perform_caching = true
cache false

# Manipulate the value under the covers
# self.perform_caching = true
end
```