Skip to content

Commit 6251b90

Browse files
committed
Merge pull request #902 from cristianbica/serializer_file_digest
Added serializer file digest to the cache_key
2 parents c2305f0 + 7a62d31 commit 6251b90

File tree

4 files changed

+37
-10
lines changed

4 files changed

+37
-10
lines changed

lib/active_model/serializer.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@ class << self
1919
attr_accessor :_cache_only
2020
attr_accessor :_cache_except
2121
attr_accessor :_cache_options
22+
attr_accessor :_cache_digest
2223
end
2324

2425
def self.inherited(base)
2526
base._attributes = self._attributes.try(:dup) || []
2627
base._attributes_keys = self._attributes_keys.try(:dup) || {}
2728
base._associations = self._associations.try(:dup) || {}
2829
base._urls = []
30+
serializer_file = File.open(caller.first[/^[^:]+/])
31+
base._cache_digest = Digest::MD5.hexdigest(serializer_file.read)
2932
end
3033

3134
def self.attributes(*attrs)

lib/active_model/serializer/adapter.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ def is_fragment_cached?
6363
end
6464

6565
def cache_key
66+
parts = []
67+
parts << object_cache_key
68+
parts << @klass._cache_digest unless @klass._cache_options && @klass._cache_options[:skip_digest]
69+
parts.join("/")
70+
end
71+
72+
def object_cache_key
6673
(@klass._cache_key) ? "#{@klass._cache_key}/#{@cached_serializer.object.id}-#{@cached_serializer.object.updated_at}" : @cached_serializer.object.cache_key
6774
end
6875

test/fixtures/poro.rb

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class Model
2+
FILE_DIGEST = Digest::MD5.hexdigest(File.open(__FILE__).read)
3+
24
def initialize(hash={})
35
@attributes = hash
46
end
@@ -7,6 +9,10 @@ def cache_key
79
"#{self.class.name.downcase}/#{self.id}-#{self.updated_at}"
810
end
911

12+
def cache_key_with_digest
13+
"#{cache_key}/#{FILE_DIGEST}"
14+
end
15+
1016
def updated_at
1117
@attributes[:updated_at] ||= DateTime.now.to_time.to_i
1218
end
@@ -72,7 +78,7 @@ module Spam; end
7278
Spam::UnrelatedLink = Class.new(Model)
7379

7480
PostSerializer = Class.new(ActiveModel::Serializer) do
75-
cache key:'post', expires_in: 0.1
81+
cache key:'post', expires_in: 0.1, skip_digest: true
7682
attributes :id, :title, :body
7783

7884
has_many :comments
@@ -99,7 +105,7 @@ def self.root_name
99105
end
100106

101107
CommentSerializer = Class.new(ActiveModel::Serializer) do
102-
cache expires_in: 1.day
108+
cache expires_in: 1.day, skip_digest: true
103109
attributes :id, :body
104110

105111
belongs_to :post
@@ -111,7 +117,7 @@ def custom_options
111117
end
112118

113119
AuthorSerializer = Class.new(ActiveModel::Serializer) do
114-
cache key:'writer'
120+
cache key:'writer', skip_digest: true
115121
attributes :id, :name
116122

117123
has_many :posts, embed: :ids
@@ -120,7 +126,7 @@ def custom_options
120126
end
121127

122128
RoleSerializer = Class.new(ActiveModel::Serializer) do
123-
cache only: [:name]
129+
cache only: [:name], skip_digest: true
124130
attributes :id, :name, :description, :slug
125131

126132
def slug
@@ -137,7 +143,7 @@ def slug
137143
end
138144

139145
LocationSerializer = Class.new(ActiveModel::Serializer) do
140-
cache only: [:place]
146+
cache only: [:place], skip_digest: true
141147
attributes :id, :lat, :lng
142148

143149
belongs_to :place
@@ -154,13 +160,14 @@ def place
154160
end
155161

156162
BioSerializer = Class.new(ActiveModel::Serializer) do
157-
cache except: [:content]
163+
cache except: [:content], skip_digest: true
158164
attributes :id, :content, :rating
159165

160166
belongs_to :author
161167
end
162168

163169
BlogSerializer = Class.new(ActiveModel::Serializer) do
170+
cache key: 'blog'
164171
attributes :id, :name
165172

166173
belongs_to :writer

test/serializers/cache_test.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ class CacheTest < Minitest::Test
55
def setup
66
ActionController::Base.cache_store.clear
77
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
8-
@blog = Blog.new(id: 999, name: "Custom blog")
98
@post = Post.new(title: 'New Post', body: 'Body')
109
@bio = Bio.new(id: 1, content: 'AMS Contributor')
1110
@author = Author.new(name: 'Joao M. D. Moura')
11+
@blog = Blog.new(id: 999, name: "Custom blog", writer: @author, articles: [])
1212
@role = Role.new(name: 'Great Author')
1313
@location = Location.new(lat: '-23.550520', lng: '-46.633309')
1414
@place = Place.new(name: 'Amazing Place')
@@ -30,6 +30,7 @@ def setup
3030
@post_serializer = PostSerializer.new(@post)
3131
@author_serializer = AuthorSerializer.new(@author)
3232
@comment_serializer = CommentSerializer.new(@comment)
33+
@blog_serializer = BlogSerializer.new(@blog)
3334
end
3435

3536
def test_cache_definition
@@ -56,9 +57,9 @@ def test_default_cache_key_fallback
5657
end
5758

5859
def test_cache_options_definition
59-
assert_equal({expires_in: 0.1}, @post_serializer.class._cache_options)
60-
assert_equal(nil, @author_serializer.class._cache_options)
61-
assert_equal({expires_in: 1.day}, @comment_serializer.class._cache_options)
60+
assert_equal({expires_in: 0.1, skip_digest: true}, @post_serializer.class._cache_options)
61+
assert_equal(nil, @blog_serializer.class._cache_options)
62+
assert_equal({expires_in: 1.day, skip_digest: true}, @comment_serializer.class._cache_options)
6263
end
6364

6465
def test_fragment_cache_definition
@@ -115,6 +116,15 @@ def test_fragment_fetch_with_virtual_associations
115116
assert_equal({place: 'Nowhere'}, ActionController::Base.cache_store.fetch(@location.cache_key))
116117
end
117118

119+
def test_uses_file_digest_in_cahe_key
120+
blog = render_object_with_cache(@blog)
121+
assert_equal(@blog_serializer.attributes, ActionController::Base.cache_store.fetch(@blog.cache_key_with_digest))
122+
end
123+
124+
def _cache_digest_definition
125+
assert_equal(::Model::FILE_DIGEST, @post_serializer.class._cache_digest)
126+
end
127+
118128
private
119129
def render_object_with_cache(obj)
120130
serializer_class = ActiveModel::Serializer.serializer_for(obj)

0 commit comments

Comments
 (0)