Skip to content

Commit 01a225f

Browse files
committed
Merge pull request #939 from aaronlerch/default-cache-key
Use a more precise generated cache key
2 parents c0a8264 + 6892ca3 commit 01a225f

File tree

4 files changed

+20
-15
lines changed

4 files changed

+20
-15
lines changed

lib/active_model/serializer/adapter.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ def cache_key
7272
end
7373

7474
def object_cache_key
75-
(@klass._cache_key) ? "#{@klass._cache_key}/#{@cached_serializer.object.id}-#{@cached_serializer.object.updated_at}" : @cached_serializer.object.cache_key
75+
object_time_safe = @cached_serializer.object.updated_at
76+
object_time_safe = object_time_safe.strftime("%Y%m%d%H%M%S%9N") if object_time_safe.respond_to?(:strftime)
77+
(@klass._cache_key) ? "#{@klass._cache_key}/#{@cached_serializer.object.id}-#{object_time_safe}" : @cached_serializer.object.cache_key
7678
end
7779

7880
def meta

test/action_controller/serialization_test.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,12 @@ def render_fragment_changed_object_with_except_cache_enabled
9898

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

106105
generate_cached_serializer(like)
107-
like.post = post2
106+
like.likable = comment2
108107
like.time = DateTime.now.to_s
109108

110109
render json: like
@@ -229,7 +228,7 @@ def test_render_with_cache_enable
229228
assert_equal expected.to_json, @response.body
230229

231230
get :render_changed_object_with_cache_enabled
232-
assert_equal expected.to_json, @response.body
231+
assert_not_equal expected.to_json, @response.body
233232

234233
ActionController::Base.cache_store.clear
235234
get :render_changed_object_with_cache_enabled
@@ -291,10 +290,9 @@ def test_render_fragment_changed_object_with_relationship
291290
expected_return = {
292291
"id"=>1,
293292
"time"=>DateTime.now.to_s,
294-
"post" => {
293+
"likeable" => {
295294
"id"=>1,
296-
"title"=>"New Post",
297-
"body"=>"Body"
295+
"body"=>"ZOMG A COMMENT"
298296
}
299297
}
300298

test/fixtures/poro.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ def initialize(hash={})
1010
end
1111

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

1616
def cache_key_with_digest
1717
"#{cache_key}/#{FILE_DIGEST}"
1818
end
1919

2020
def updated_at
21-
@attributes[:updated_at] ||= DateTime.now.to_time.to_i
21+
@attributes[:updated_at] ||= DateTime.now.to_time
2222
end
2323

2424
def read_attribute_for_serialization(name)
@@ -69,14 +69,19 @@ class ProfilePreviewSerializer < ActiveModel::Serializer
6969

7070
Post = Class.new(Model)
7171
Like = Class.new(Model)
72-
Comment = Class.new(Model)
7372
Author = Class.new(Model)
7473
Bio = Class.new(Model)
7574
Blog = Class.new(Model)
7675
Role = Class.new(Model)
7776
User = Class.new(Model)
7877
Location = Class.new(Model)
7978
Place = Class.new(Model)
79+
Comment = Class.new(Model) do
80+
# Uses a custom non-time-based cache key
81+
def cache_key
82+
"#{self.class.name.downcase}/#{self.id}"
83+
end
84+
end
8085

8186
module Spam; end
8287
Spam::UnrelatedLink = Class.new(Model)
@@ -143,7 +148,7 @@ def slug
143148
LikeSerializer = Class.new(ActiveModel::Serializer) do
144149
attributes :id, :time
145150

146-
belongs_to :post
151+
belongs_to :likeable
147152
end
148153

149154
LocationSerializer = Class.new(ActiveModel::Serializer) do

test/serializers/cache_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def test_cache_key_definition
4848
def test_cache_key_interpolation_with_updated_at
4949
author = render_object_with_cache(@author)
5050
assert_equal(nil, ActionController::Base.cache_store.fetch(@author.cache_key))
51-
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)
51+
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)
5252
end
5353

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

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

0 commit comments

Comments
 (0)