Skip to content

Commit 5f3bdcc

Browse files
markizbf4
authored andcommitted
Use ActiveSupport::Cache.expand_cache_key for cache key expansions (#1878)
* Use ActiveSupport::Cache.expand_cache_key for cache key expansions
1 parent 6de3f31 commit 5f3bdcc

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Fixes:
1515

1616
Misc:
1717

18+
- [#1878](https://github.com/rails-api/active_model_serializers/pull/1878) Cache key generation for serializers now uses `ActiveSupport::Cache.expand_cache_key` instead of `Array#join` by default and is also overridable. This change should be backward-compatible. (@markiz)
19+
1820
### [v0.10.2 (2016-07-05)](https://github.com/rails-api/active_model_serializers/compare/v0.10.1...v0.10.2)
1921

2022
Fixes:

lib/active_model/serializer/caching.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,11 @@ def cache_key(adapter_instance)
263263
parts << object_cache_key
264264
parts << adapter_instance.cache_key
265265
parts << serializer_class._cache_digest unless serializer_class._skip_digest?
266-
@cache_key = parts.join('/')
266+
@cache_key = expand_cache_key(parts)
267+
end
268+
269+
def expand_cache_key(parts)
270+
ActiveSupport::Cache.expand_cache_key(parts)
267271
end
268272

269273
# Use object's cache_key if available, else derive a key from the object

test/cache_test.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,30 @@
44

55
module ActiveModelSerializers
66
class CacheTest < ActiveSupport::TestCase
7+
# Instead of a primitive cache key (i.e. a string), this class
8+
# returns a list of objects that require to be expanded themselves.
9+
class AuthorWithExpandableCacheElements < Author
10+
# For the test purposes it's important that #to_s for HasCacheKey differs
11+
# between instances, hence not a Struct.
12+
class HasCacheKey
13+
attr_reader :cache_key
14+
def initialize(cache_key)
15+
@cache_key = cache_key
16+
end
17+
18+
def to_s
19+
"HasCacheKey##{object_id}"
20+
end
21+
end
22+
23+
def cache_key
24+
[
25+
HasCacheKey.new(name),
26+
HasCacheKey.new(id)
27+
]
28+
end
29+
end
30+
731
class UncachedAuthor < Author
832
# To confirm cache_key is set using updated_at and cache_key option passed to cache
933
undef_method :cache_key
@@ -106,6 +130,20 @@ def test_cache_key_interpolation_with_updated_at_when_cache_key_is_not_defined_o
106130
assert_equal(uncached_author_serializer.attributes.to_json, cache_store.fetch(key).to_json)
107131
end
108132

133+
def test_cache_key_expansion
134+
author = AuthorWithExpandableCacheElements.new(id: 10, name: 'hello')
135+
same_author = AuthorWithExpandableCacheElements.new(id: 10, name: 'hello')
136+
diff_author = AuthorWithExpandableCacheElements.new(id: 11, name: 'hello')
137+
138+
author_serializer = AuthorSerializer.new(author)
139+
same_author_serializer = AuthorSerializer.new(same_author)
140+
diff_author_serializer = AuthorSerializer.new(diff_author)
141+
adapter = AuthorSerializer.serialization_adapter_instance
142+
143+
assert_equal(author_serializer.cache_key(adapter), same_author_serializer.cache_key(adapter))
144+
refute_equal(author_serializer.cache_key(adapter), diff_author_serializer.cache_key(adapter))
145+
end
146+
109147
def test_default_cache_key_fallback
110148
render_object_with_cache(@comment)
111149
key = "#{@comment.cache_key}/#{adapter.cache_key}"

0 commit comments

Comments
 (0)