Skip to content

Commit

Permalink
Default the generated cache key to use custom #strftime instead of ra…
Browse files Browse the repository at this point in the history
…w #to_s to achieve more accurate precision
  • Loading branch information
aaronlerch committed Jun 26, 2015
1 parent 508eb68 commit 2e803eb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
4 changes: 3 additions & 1 deletion lib/active_model/serializer/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ def cache_key
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
object_time_safe = @cached_serializer.object.updated_at
object_time_safe = object_time_safe.strftime("%Y%m%d%H%M%S%9N") if object_time_safe.respond_to?(:strftime)
(@klass._cache_key) ? "#{@klass._cache_key}/#{@cached_serializer.object.id}-#{object_time_safe}" : @cached_serializer.object.cache_key
end

def meta
Expand Down
14 changes: 6 additions & 8 deletions test/action_controller/serialization_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,12 @@ def render_fragment_changed_object_with_except_cache_enabled

def render_fragment_changed_object_with_relationship
comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
comment2 = Comment.new({ id: 1, body: 'ZOMG AN UPDATED-BUT-NOT-CACHE-EXPIRED COMMENT' })
author = Author.new(id: 1, name: 'Joao Moura.')
post = Post.new({ id: 1, title: 'New Post', body: 'Body', comments: [comment], author: author })
post2 = Post.new({ id: 1, title: 'New Post2', body: 'Body2', comments: [comment], author: author })
like = Like.new({ id: 1, post: post, time: 3.days.ago })
like = Like.new({ id: 1, likeable: comment, time: 3.days.ago })

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

render json: like
Expand Down Expand Up @@ -229,7 +228,7 @@ def test_render_with_cache_enable
assert_equal expected.to_json, @response.body

get :render_changed_object_with_cache_enabled
assert_equal expected.to_json, @response.body
assert_not_equal expected.to_json, @response.body

ActionController::Base.cache_store.clear
get :render_changed_object_with_cache_enabled
Expand Down Expand Up @@ -291,10 +290,9 @@ def test_render_fragment_changed_object_with_relationship
expected_return = {
"id"=>1,
"time"=>DateTime.now.to_s,
"post" => {
"likeable" => {
"id"=>1,
"title"=>"New Post",
"body"=>"Body"
"body"=>"ZOMG A COMMENT"
}
}

Expand Down
13 changes: 9 additions & 4 deletions test/fixtures/poro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ def initialize(hash={})
end

def cache_key
"#{self.class.name.downcase}/#{self.id}-#{self.updated_at}"
"#{self.class.name.downcase}/#{self.id}-#{self.updated_at.strftime("%Y%m%d%H%M%S%9N")}"
end

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

def updated_at
@attributes[:updated_at] ||= DateTime.now.to_time.to_i
@attributes[:updated_at] ||= DateTime.now.to_time
end

def read_attribute_for_serialization(name)
Expand Down Expand Up @@ -69,14 +69,19 @@ class ProfilePreviewSerializer < ActiveModel::Serializer

Post = Class.new(Model)
Like = Class.new(Model)
Comment = Class.new(Model)
Author = Class.new(Model)
Bio = Class.new(Model)
Blog = Class.new(Model)
Role = Class.new(Model)
User = Class.new(Model)
Location = Class.new(Model)
Place = Class.new(Model)
Comment = Class.new(Model) do
# Uses a custom non-time-based cache key
def cache_key
"#{self.class.name.downcase}/#{self.id}"
end
end

module Spam; end
Spam::UnrelatedLink = Class.new(Model)
Expand Down Expand Up @@ -143,7 +148,7 @@ def slug
LikeSerializer = Class.new(ActiveModel::Serializer) do
attributes :id, :time

belongs_to :post
belongs_to :likeable
end

LocationSerializer = Class.new(ActiveModel::Serializer) do
Expand Down
4 changes: 2 additions & 2 deletions test/serializers/cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_cache_key_definition
def test_cache_key_interpolation_with_updated_at
author = render_object_with_cache(@author)
assert_equal(nil, ActionController::Base.cache_store.fetch(@author.cache_key))
assert_equal(@author_serializer.attributes.to_json, ActionController::Base.cache_store.fetch("#{@author_serializer.class._cache_key}/#{@author_serializer.object.id}-#{@author_serializer.object.updated_at}").to_json)
assert_equal(@author_serializer.attributes.to_json, ActionController::Base.cache_store.fetch("#{@author_serializer.class._cache_key}/#{@author_serializer.object.id}-#{@author_serializer.object.updated_at.strftime("%Y%m%d%H%M%S%9N")}").to_json)
end

def test_default_cache_key_fallback
Expand Down Expand Up @@ -85,7 +85,7 @@ def test_associations_cache_when_updated
# Generate a new Cache of Post object and each objects related to it.
render_object_with_cache(@post)

# Check if if cache the objects separately
# Check if it cached the objects separately
assert_equal(@post_serializer.attributes, ActionController::Base.cache_store.fetch(@post.cache_key))
assert_equal(@comment_serializer.attributes, ActionController::Base.cache_store.fetch(@comment.cache_key))

Expand Down

0 comments on commit 2e803eb

Please sign in to comment.