mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 15:23:06 +00:00
Adding cache support to version 0.10.0
It's a new implementation of cache based on ActiveSupport::Cache.
The implementation abstracts the cache in Adapter class on a
private method called cached_object, this method is intended
to be used on Adapters inside serializable_hash method in order
to cache each instance of the object that will be returned by
the serializer.
Some of its features are:
- A different syntax. (no longer need the cache_key method).
- An options argument that have the same arguments of ActiveSupport::Cache::Store, plus a key option that will be the prefix of the object cache on a pattern "#{key}-#{object.id}".
- It cache the objects individually and not the whole Serializer return, re-using it in different requests (as a show and a index method for example.)
This commit is contained in:
62
test/serializers/cache_test.rb
Normal file
62
test/serializers/cache_test.rb
Normal file
@@ -0,0 +1,62 @@
|
||||
require 'test_helper'
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class CacheTest < Minitest::Test
|
||||
def setup
|
||||
@post = Post.new({ title: 'New Post', body: 'Body' })
|
||||
@comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
|
||||
@author = Author.new(name: 'Joao M. D. Moura')
|
||||
@role = Role.new(name: 'Great Author')
|
||||
@author.posts = [@post]
|
||||
@author.roles = [@role]
|
||||
@author.bio = nil
|
||||
@post.comments = [@comment]
|
||||
@post.author = @author
|
||||
@comment.post = @post
|
||||
@comment.author = @author
|
||||
|
||||
@post_serializer = PostSerializer.new(@post)
|
||||
@author_serializer = AuthorSerializer.new(@author)
|
||||
@comment_serializer = CommentSerializer.new(@comment)
|
||||
end
|
||||
|
||||
def test_cache_definition
|
||||
assert_equal(ActionController::Base.cache_store, @post_serializer.class._cache)
|
||||
assert_equal(ActionController::Base.cache_store, @author_serializer.class._cache)
|
||||
assert_equal(ActionController::Base.cache_store, @comment_serializer.class._cache)
|
||||
end
|
||||
|
||||
def test_cache_key_definition
|
||||
assert_equal('post', @post_serializer.class._cache_key)
|
||||
assert_equal('writer', @author_serializer.class._cache_key)
|
||||
assert_equal(nil, @comment_serializer.class._cache_key)
|
||||
end
|
||||
|
||||
def test_cache_key_interpolation_with_updated_at
|
||||
author = render_object_with_cache_without_cache_key(@author)
|
||||
assert_equal(nil, ActionController::Base.cache_store.fetch(@author.cache_key))
|
||||
assert_equal(author, ActionController::Base.cache_store.fetch("#{@author_serializer.class._cache_key}/#{@author_serializer.object.id}-#{@author_serializer.object.updated_at}").to_json)
|
||||
end
|
||||
|
||||
def test_default_cache_key_fallback
|
||||
comment = render_object_with_cache_without_cache_key(@comment)
|
||||
assert_equal(comment, ActionController::Base.cache_store.fetch(@comment.cache_key).to_json)
|
||||
end
|
||||
|
||||
def test_cache_options_definition
|
||||
assert_equal({expires_in: 0.05}, @post_serializer.class._cache_options)
|
||||
assert_equal(nil, @author_serializer.class._cache_options)
|
||||
assert_equal({expires_in: 1.day}, @comment_serializer.class._cache_options)
|
||||
end
|
||||
|
||||
private
|
||||
def render_object_with_cache_without_cache_key(obj)
|
||||
serializer_class = ActiveModel::Serializer.serializer_for(obj)
|
||||
serializer = serializer_class.new(obj)
|
||||
adapter = ActiveModel::Serializer.adapter.new(serializer)
|
||||
adapter.to_json
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user