Skip to content

Commit

Permalink
Adding Fragment Cache to AMS
Browse files Browse the repository at this point in the history
It's an upgrade based on the new Cache implementation rails-api#693.
It allows to use the Rails conventions to cache
specific attributes or associations.
It's based on the Cache Composition implementation.
  • Loading branch information
joaomdmoura committed Feb 12, 2015
1 parent a824376 commit 3de7324
Show file tree
Hide file tree
Showing 10 changed files with 250 additions and 31 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ a ```key``` option that will be the prefix of the object cache
on a pattern ```"#{key}/#{object.id}-#{object.updated_at}"```.

**[NOTE] Every object is individually cached.**

**[NOTE] The cache is automatically expired after update an object but it's not deleted.**

```ruby
Expand All @@ -294,6 +295,25 @@ On this example every ```Post``` object will be cached with
the key ```"post/#{post.id}-#{post.updated_at}"```. You can use this key to expire it as you want,
but in this case it will be automatically expired after 3 hours.

### Fragmenting Caching

If there is some API endpoint that shouldn't be fully cached, you can still optmise it, using Fragment Cache on the attributes and relationships that you want to cache.

You can define the attribute or relationships by using ```only``` or ```except``` option on cache method.

Example:

```ruby
class PostSerializer < ActiveModel::Serializer
cache key: 'post', expires_in: 3.hours, only: [:title]
attributes :title, :body

has_many :comments

url :post
end
```

## Getting Help

If you find a bug, please report an [Issue](https://github.com/rails-api/active_model_serializers/issues/new).
Expand Down
8 changes: 8 additions & 0 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,25 @@ class Serializer

class << self
attr_accessor :_attributes
attr_accessor :_attributes_keys
attr_accessor :_associations
attr_accessor :_urls
attr_accessor :_cache
attr_accessor :_cache_key
attr_accessor :_cache_only
attr_accessor :_cache_except
attr_accessor :_cache_options
end

def self.inherited(base)
base._attributes = []
base._attributes_keys = {}
base._associations = {}
base._urls = []
end

def self.attributes(*attrs)
attrs = attrs.first if attrs.first.class == Array
@_attributes.concat attrs

attrs.each do |attr|
Expand All @@ -33,6 +38,7 @@ def self.attributes(*attrs)

def self.attribute(attr, options = {})
key = options.fetch(:key, attr)
@_attributes_keys[attr] = {key: key} if key != attr
@_attributes.concat [key]
define_method key do
object.read_attribute_for_serialization(attr)
Expand All @@ -43,6 +49,8 @@ def self.attribute(attr, options = {})
def self.cache(options = {})
@_cache = ActionController::Base.cache_store if Rails.configuration.action_controller.perform_caching
@_cache_key = options.delete(:key)
@_cache_only = options.delete(:only)
@_cache_except = options.delete(:except)
@_cache_options = (options.empty?) ? nil : options
end

Expand Down
17 changes: 3 additions & 14 deletions lib/active_model/serializer/adapter.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
require 'active_model/serializer/cache'

module ActiveModel
class Serializer
class Adapter
extend ActiveSupport::Autoload
include ActiveModel::Serializer::Cache
autoload :Json
autoload :Null
autoload :JsonApi
Expand Down Expand Up @@ -50,20 +53,6 @@ def include_meta(json)
json[meta_key] = meta if meta && root
json
end

private

def cached_object
klass = serializer.class
if klass._cache
_cache_key = (klass._cache_key) ? "#{klass._cache_key}/#{serializer.object.id}-#{serializer.object.updated_at}" : serializer.object.cache_key
klass._cache.fetch(_cache_key, klass._cache_options) do
yield
end
else
yield
end
end
end
end
end
1 change: 0 additions & 1 deletion lib/active_model/serializer/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ def add_linked(resource_name, serializers, parent = nil)
end
end


def attributes_for_serializer(serializer, options)
if serializer.respond_to?(:each)
result = []
Expand Down
100 changes: 100 additions & 0 deletions lib/active_model/serializer/cache.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
module ActiveModel
class Serializer
module Cache
private
def cached_object
klass = serializer.class
if klass._cache && !klass._cache_only && !klass._cache_except
_cache_key = (klass._cache_key) ? "#{klass._cache_key}/#{serializer.object.id}-#{serializer.object.updated_at}" : serializer.object.cache_key
klass._cache.fetch(_cache_key, klass._cache_options) do
yield
end

elsif klass._cache_only && !klass._cache_except || !klass._cache_only && klass._cache_except
serializers = fragment_serializer(@serializer.object.class.name, klass)

cached_serializer = serializers[:cached].constantize.new(@serializer.object)
non_cached_serializer = serializers[:non_cached].constantize.new(@serializer.object)

cached_hash = self.class.new(cached_serializer, @options).serializable_hash
non_cached_hash = self.class.new(non_cached_serializer, @options).serializable_hash

if @serializer.root && self.class == ActiveModel::Serializer::Adapter::JsonApi
cached_hash_root(cached_hash, non_cached_hash)
else
non_cached_hash.merge cached_hash
end
else
yield
end
end

def cached_hash_root(cached_hash, non_cached_hash)
@root
hash = {}

core_cached = cached_hash.first
core_non_cached = non_cached_hash.first
no_root_cache = cached_hash.delete_if {|key, value| key == core_cached[0] }
no_root_non_cache = non_cached_hash.delete_if {|key, value| key == core_non_cached[0] }

if @root
hash[@root] = (core_cached[1]) ? core_cached[1].merge(core_non_cached[1]) : core_non_cached[1]
else
hash = (core_cached[1]) ? core_cached[1].merge(core_non_cached[1]) : core_non_cached[1]
end

hash.merge no_root_non_cache.merge no_root_cache
end

def fragment_associations(serializers, associations)
associations.each do |association|
options = ",#{association[1][:association_options]}" if association[1].include?(:association_options)
eval("#{serializers[:non_cached]}.#{association[1][:type].to_s}(:#{association[0]}#{options})")
end
end

def cached_attributes_and_association(klass, serializers)
cached_attr = (klass._cache_only) ? klass._cache_only : @serializer.attributes.keys.delete_if {|attr| klass._cache_except.include?(attr) }
non_cached_attr = @serializer.attributes.keys.delete_if {|attr| cached_attr.include?(attr) }
associations = @serializer.each_association

cached_attr.each do |attr|
if @serializer.each_association.keys.include?(attr)
associations.delete(attr)
serializers[:cached].constantize.send(@serializer.each_association[attr][:type], attr)
end
end

cached_attr.each do |attribute|
options = @serializer.class._attributes_keys[attribute]
options ||= {}
serializers[:cached].constantize.attribute(attribute, options)
end

non_cached_attr.each do |attribute|
options = @serializer.class._attributes_keys[attribute]
options ||= {}
serializers[:non_cached].constantize.attribute(attribute, options)
end
fragment_associations(serializers, associations)
end

def fragment_serializer(name, klass)
cached = "#{name.capitalize}CachedSerializer"
non_cached = "#{name.capitalize}NonCachedSerializer"

Object.const_set cached, Class.new(ActiveModel::Serializer) unless Object.const_defined?(cached)
Object.const_set non_cached, Class.new(ActiveModel::Serializer) unless Object.const_defined?(non_cached)

klass._cache_options ||= {}
klass._cache_options[:key] = klass._cache_key if klass._cache_key
cached.constantize.cache(klass._cache_options)

serializers = {cached: cached, non_cached: non_cached}
cached_attributes_and_association(klass, serializers)
return serializers
end
end
end
end
2 changes: 2 additions & 0 deletions test/action_controller/json_api_linked_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,14 @@ def test_render_resource_with_nested_has_many_include
"roles"=>[{
"id" => "1",
"name" => "admin",
"description" => nil,
"links" => {
"author" => "1"
}
}, {
"id" => "2",
"name" => "colab",
"description" => nil,
"links" => {
"author" => "1"
}
Expand Down
77 changes: 76 additions & 1 deletion test/action_controller/serialization_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,42 @@ def render_changed_object_with_cache_enabled
render json: post
end

def render_fragment_changed_object_with_only_cache_enabled
author = Author.new(id: 1, name: 'Joao Moura.')
role = Role.new({ id: 42, name: 'ZOMG A ROLE', description: 'DESCRIPTION HERE', author: author })

generate_cached_serializer(role)
role.name = 'lol'
role.description = 'HUEHUEBRBR'

render json: role
end

def render_fragment_changed_object_with_except_cache_enabled
author = Author.new(id: 1, name: 'Joao Moura.')
bio = Bio.new({ id: 42, content: 'ZOMG A ROLE', rating: 5, author: author })

generate_cached_serializer(bio)
bio.content = 'lol'
bio.rating = 0

render json: bio
end

def render_fragment_changed_object_with_relationship
comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
author = Author.new(id: 1, name: 'Joao Moura.')
post = Post.new({ id: 1, title: 'New Post', blog:nil, body: 'Body', comments: [comment], author: author })
post2 = Post.new({ id: 1, title: 'New Post2', blog:nil, body: 'Body2', comments: [comment], author: author })
like = Like.new({ id: 1, post: post, time: 3.days.ago })

generate_cached_serializer(like)
like.post = post2
like.time = DateTime.now.to_s

render json: like
end

private
def generate_cached_serializer(obj)
serializer_class = ActiveModel::Serializer.serializer_for(obj)
Expand Down Expand Up @@ -200,6 +236,45 @@ def test_render_with_cache_enable_and_expired
assert_equal 'application/json', @response.content_type
assert_equal expected.to_json, @response.body
end

def test_render_with_fragment_only_cache_enable
ActionController::Base.cache_store.clear
get :render_fragment_changed_object_with_only_cache_enabled
response = JSON.parse(@response.body)

assert_equal 'application/json', @response.content_type
assert_equal 'ZOMG A ROLE', response["name"]
assert_equal 'HUEHUEBRBR', response["description"]
end

def test_render_with_fragment_except_cache_enable
ActionController::Base.cache_store.clear
get :render_fragment_changed_object_with_except_cache_enabled
response = JSON.parse(@response.body)

assert_equal 'application/json', @response.content_type
assert_equal 5, response["rating"]
assert_equal 'lol', response["content"]
end

def test_render_fragment_changed_object_with_relationship
ActionController::Base.cache_store.clear
get :render_fragment_changed_object_with_relationship
response = JSON.parse(@response.body)

expected_return = {
"post" => {
"id"=>1,
"title"=>"New Post",
"body"=>"Body"
},
"id"=>1,
"time"=>DateTime.now.to_s
}

assert_equal 'application/json', @response.content_type
assert_equal expected_return, response
end
end
end
end
end
Loading

0 comments on commit 3de7324

Please sign in to comment.