Skip to content

Commit 1d24c97

Browse files
committed
Lazify calculating caller file digest until used
1 parent 4c0e2dc commit 1d24c97

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Breaking changes:
88
- [#1574](https://github.com/rails-api/active_model_serializers/pull/1574) Default key case for the JsonApi adapter changed to dashed. (@remear)
99

1010
Features:
11+
- [#1687](https://github.com/rails-api/active_model_serializers/pull/1687) Only calculate `_cache_digest` (in `cache_key`) when `skip_digest` is false. (@bf4)
1112
- [#1647](https://github.com/rails-api/active_model_serializers/pull/1647) Restrict usage of `serializable_hash` options
1213
to the ActiveModel::Serialization and ActiveModel::Serializers::JSON interface. (@bf4)
1314
- [#1645](https://github.com/rails-api/active_model_serializers/pull/1645) Transform keys referenced in values. (@remear)

lib/active_model/serializer/caching.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ module Caching
1818
# force
1919
# race_condition_ttl
2020
# Passed to ::_cache as
21-
# serializer._cache.fetch(cache_key, @klass._cache_options)
22-
serializer.class_attribute :_cache_digest # @api private : Generated
21+
# serializer.cache_store.fetch(cache_key, @klass._cache_options)
22+
# Passed as second argument to serializer.cache_store.fetch(cache_key, self.class._cache_options)
23+
serializer.class_attribute :_cache_digest_file_path # @api private : Derived at inheritance
2324
end
2425
end
2526

@@ -42,7 +43,12 @@ module ClassMethods
4243
def inherited(base)
4344
super
4445
caller_line = caller[1]
45-
base._cache_digest = digest_caller_file(caller_line)
46+
base._cache_digest_file_path = caller_line
47+
end
48+
49+
def _cache_digest
50+
return @_cache_digest if defined?(@_cache_digest)
51+
@_cache_digest = digest_caller_file(_cache_digest_file_path)
4652
end
4753

4854
# Hashes contents of file for +_cache_digest+

0 commit comments

Comments
 (0)