-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use ActiveSupport::Cache.expand_cache_key for cache key expansions #1878
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,30 @@ | |
|
||
module ActiveModelSerializers | ||
class CacheTest < ActiveSupport::TestCase | ||
# Instead of a primitive cache key (i.e. a string), this class | ||
# returns a list of objects that require to be expanded themselves. | ||
class AuthorWithExpandableCacheElements < Author | ||
# For the test purposes it's important that #to_s for HasCacheKey differs | ||
# between instances, hence not a Struct. | ||
class HasCacheKey | ||
attr_reader :cache_key | ||
def initialize(cache_key) | ||
@cache_key = cache_key | ||
end | ||
|
||
def to_s | ||
"HasCacheKey##{object_id}" | ||
end | ||
end | ||
|
||
def cache_key | ||
[ | ||
HasCacheKey.new(name), | ||
HasCacheKey.new(id) | ||
] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @markiz my only real concern is that these classes are really weird-looking. Would you mind adding some comments why you designed it this way? Be nice to future devs :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
end | ||
end | ||
|
||
class UncachedAuthor < Author | ||
# To confirm cache_key is set using updated_at and cache_key option passed to cache | ||
undef_method :cache_key | ||
|
@@ -106,6 +130,20 @@ def test_cache_key_interpolation_with_updated_at_when_cache_key_is_not_defined_o | |
assert_equal(uncached_author_serializer.attributes.to_json, cache_store.fetch(key).to_json) | ||
end | ||
|
||
def test_cache_key_expansion | ||
author = AuthorWithExpandableCacheElements.new(id: 10, name: 'hello') | ||
same_author = AuthorWithExpandableCacheElements.new(id: 10, name: 'hello') | ||
diff_author = AuthorWithExpandableCacheElements.new(id: 11, name: 'hello') | ||
|
||
author_serializer = AuthorSerializer.new(author) | ||
same_author_serializer = AuthorSerializer.new(same_author) | ||
diff_author_serializer = AuthorSerializer.new(diff_author) | ||
adapter = AuthorSerializer.serialization_adapter_instance | ||
|
||
assert_equal(author_serializer.cache_key(adapter), same_author_serializer.cache_key(adapter)) | ||
refute_equal(author_serializer.cache_key(adapter), diff_author_serializer.cache_key(adapter)) | ||
end | ||
|
||
def test_default_cache_key_fallback | ||
render_object_with_cache(@comment) | ||
key = "#{@comment.cache_key}/#{adapter.cache_key}" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
d0e54da