Skip to content

Commit

Permalink
Added serializer file digest to the cache_key
Browse files Browse the repository at this point in the history
Fixes #901
  • Loading branch information
cristianbica committed May 6, 2015
1 parent ece43f3 commit f74af54
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
2 changes: 2 additions & 0 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ class << self
attr_accessor :_cache_only
attr_accessor :_cache_except
attr_accessor :_cache_options
attr_accessor :_cache_digest
end

def self.inherited(base)
base._attributes = []
base._attributes_keys = {}
base._associations = {}
base._urls = []
base._cache_digest = Digest::MD5.hexdigest(caller(1,1).first[/^[^:]+/])
end

def self.attributes(*attrs)
Expand Down
7 changes: 7 additions & 0 deletions lib/active_model/serializer/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ def is_fragment_cached?
end

def cache_key
parts = []
parts << object_cache_key
parts << @klass._cache_digest unless @klass._cache_options && @klass._cache_options[:skip_digest]
parts.join("/")
end

def object_cache_key
(@klass._cache_key) ? "#{@klass._cache_key}/#{@cached_serializer.object.id}-#{@cached_serializer.object.updated_at}" : @cached_serializer.object.cache_key
end

Expand Down
21 changes: 14 additions & 7 deletions test/fixtures/poro.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Model
FILE_DIGEST = Digest::MD5.hexdigest(__FILE__)

def initialize(hash={})
@attributes = hash
end
Expand All @@ -7,6 +9,10 @@ def cache_key
"#{self.class.name.downcase}/#{self.id}-#{self.updated_at}"
end

def cache_key_with_digest
"#{cache_key}/#{FILE_DIGEST}"
end

def updated_at
@attributes[:updated_at] ||= DateTime.now.to_time.to_i
end
Expand Down Expand Up @@ -64,15 +70,15 @@ class ProfilePreviewSerializer < ActiveModel::Serializer
Bio = Class.new(Model)
Blog = Class.new(Model)
Role = Class.new(Model)
User = Class.new(Model)
User = Class.new(Model)
Location = Class.new(Model)
Place = Class.new(Model)

module Spam; end
Spam::UnrelatedLink = Class.new(Model)

PostSerializer = Class.new(ActiveModel::Serializer) do
cache key:'post', expires_in: 0.1
cache key:'post', expires_in: 0.1, skip_digest: true
attributes :id, :title, :body

has_many :comments
Expand All @@ -99,7 +105,7 @@ def self.root_name
end

CommentSerializer = Class.new(ActiveModel::Serializer) do
cache expires_in: 1.day
cache expires_in: 1.day, skip_digest: true
attributes :id, :body

belongs_to :post
Expand All @@ -111,7 +117,7 @@ def custom_options
end

AuthorSerializer = Class.new(ActiveModel::Serializer) do
cache key:'writer'
cache key:'writer', skip_digest: true
attributes :id, :name

has_many :posts, embed: :ids
Expand All @@ -120,7 +126,7 @@ def custom_options
end

RoleSerializer = Class.new(ActiveModel::Serializer) do
cache only: [:name]
cache only: [:name], skip_digest: true
attributes :id, :name, :description, :slug

def slug
Expand All @@ -137,7 +143,7 @@ def slug
end

LocationSerializer = Class.new(ActiveModel::Serializer) do
cache only: [:place]
cache only: [:place], skip_digest: true
attributes :id, :lat, :lng

belongs_to :place
Expand All @@ -154,13 +160,14 @@ def place
end

BioSerializer = Class.new(ActiveModel::Serializer) do
cache except: [:content]
cache except: [:content], skip_digest: true
attributes :id, :content, :rating

belongs_to :author
end

BlogSerializer = Class.new(ActiveModel::Serializer) do
cache key: 'blog'
attributes :id, :name

belongs_to :writer
Expand Down
14 changes: 10 additions & 4 deletions test/serializers/cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ class CacheTest < Minitest::Test
def setup
ActionController::Base.cache_store.clear
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
@blog = Blog.new(id: 999, name: "Custom blog")
@post = Post.new(title: 'New Post', body: 'Body')
@bio = Bio.new(id: 1, content: 'AMS Contributor')
@author = Author.new(name: 'Joao M. D. Moura')
@blog = Blog.new(id: 999, name: "Custom blog", writer: @author, articles: [])
@role = Role.new(name: 'Great Author')
@location = Location.new(lat: '-23.550520', lng: '-46.633309')
@place = Place.new(name: 'Amazing Place')
Expand All @@ -30,6 +30,7 @@ def setup
@post_serializer = PostSerializer.new(@post)
@author_serializer = AuthorSerializer.new(@author)
@comment_serializer = CommentSerializer.new(@comment)
@blog_serializer = BlogSerializer.new(@blog)
end

def test_cache_definition
Expand All @@ -56,9 +57,9 @@ def test_default_cache_key_fallback
end

def test_cache_options_definition
assert_equal({expires_in: 0.1}, @post_serializer.class._cache_options)
assert_equal(nil, @author_serializer.class._cache_options)
assert_equal({expires_in: 1.day}, @comment_serializer.class._cache_options)
assert_equal({expires_in: 0.1, skip_digest: true}, @post_serializer.class._cache_options)
assert_equal(nil, @blog_serializer.class._cache_options)
assert_equal({expires_in: 1.day, skip_digest: true}, @comment_serializer.class._cache_options)
end

def test_fragment_cache_definition
Expand Down Expand Up @@ -115,6 +116,11 @@ def test_fragment_fetch_with_virtual_associations
assert_equal({place: 'Nowhere'}, ActionController::Base.cache_store.fetch(@location.cache_key))
end

def test_uses_file_digest_in_cahe_key
blog = render_object_with_cache(@blog)
assert_equal(@blog_serializer.attributes, ActionController::Base.cache_store.fetch(@blog.cache_key_with_digest))
end

private
def render_object_with_cache(obj)
serializer_class = ActiveModel::Serializer.serializer_for(obj)
Expand Down

0 comments on commit f74af54

Please sign in to comment.