Module: ActiveModel::Serializer::Caching::ClassMethods
- Defined in:
- lib/active_model/serializer/caching.rb
Instance Method Summary (collapse)
- - (Boolean) _skip_digest?
-
- (Object) cache(options = {})
Enables a serializer to be automatically cached.
- - (Boolean) cache_enabled?
-
- (nil, ...) cache_store
The canonical method for getting the cache store for the serializer.
-
- (Object) digest_caller_file(caller_line)
Hashes contents of file for
_cache_digest. - - (Boolean) fragment_cache_enabled?
-
- (Object) fragmented(serializer)
private
Used by FragmentCache on the CachedSerializer to call attribute methods on the fragmented cached serializer.
- - (Object) inherited(base)
-
- (true, false) perform_caching
(also: #perform_caching?)
Value is from ActiveModelSerializers.config.perform_caching.
Instance Method Details
- (Boolean) _skip_digest?
61 62 63 |
# File 'lib/active_model/serializer/caching.rb', line 61 def _skip_digest? && [:skip_digest] end |
- (Object) cache(options = {})
require less code comments. See
Enables a serializer to be automatically cached
Sets ::_cache object to
ActionController::Base.cache_store
when Rails.configuration.action_controller.perform_caching
github.com/rails-api/active_model_serializers/pull/1249#issuecomment-146567837
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/active_model/serializer/caching.rb', line 95 def cache( = {}) self._cache = .delete(:cache_store) || ActiveModelSerializers.config.cache_store || ActiveSupport::Cache.lookup_store(:null_store) self._cache_key = .delete(:key) self._cache_only = .delete(:only) self._cache_except = .delete(:except) self. = .empty? ? nil : end |
- (Boolean) cache_enabled?
140 141 142 |
# File 'lib/active_model/serializer/caching.rb', line 140 def cache_enabled? perform_caching? && cache_store && !_cache_only && !_cache_except end |
- (nil, ...) cache_store
The canonical method for getting the cache store for the serializer.
130 131 132 133 134 135 136 137 138 |
# File 'lib/active_model/serializer/caching.rb', line 130 def cache_store return nil if _cache.nil? return _cache if _cache.class != ActiveSupport::Cache::NullStore if ActiveModelSerializers.config.cache_store self._cache = ActiveModelSerializers.config.cache_store else nil end end |
- (Object) digest_caller_file(caller_line)
Hashes contents of file for _cache_digest
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/active_model/serializer/caching.rb', line 48 def digest_caller_file(caller_line) serializer_file_path = caller_line[CALLER_FILE] serializer_file_contents = IO.read(serializer_file_path) Digest::MD5.hexdigest(serializer_file_contents) rescue TypeError, Errno::ENOENT warn <<-EOF.strip_heredoc Cannot digest non-existent file: '#{caller_line}'. Please set `::_cache_digest` of the serializer if you'd like to cache it. EOF ''.freeze end |
- (Boolean) fragment_cache_enabled?
144 145 146 147 |
# File 'lib/active_model/serializer/caching.rb', line 144 def fragment_cache_enabled? perform_caching? && cache_store && (_cache_only && !_cache_except || !_cache_only && _cache_except) end |
- (Object) fragmented(serializer)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Used by FragmentCache on the CachedSerializer
to call attribute methods on the fragmented cached serializer.
68 69 70 |
# File 'lib/active_model/serializer/caching.rb', line 68 def fragmented(serializer) self._fragmented = serializer end |
- (Object) inherited(base)
41 42 43 44 45 |
# File 'lib/active_model/serializer/caching.rb', line 41 def inherited(base) super caller_line = caller[1] base._cache_digest = digest_caller_file(caller_line) end |
- (true, false) perform_caching Also known as: perform_caching?
Value is from ActiveModelSerializers.config.perform_caching. Is used to globally enable or disable all serializer caching, just like Rails.configuration.action_controller.perform_caching, which is its default value in a Rails application. Memoizes value of config first time it is called with a non-nil value. rubocop:disable Style/ClassVars
113 114 115 116 |
# File 'lib/active_model/serializer/caching.rb', line 113 def perform_caching return @@perform_caching if defined?(@@perform_caching) && !@@perform_caching.nil? @@perform_caching = ActiveModelSerializers.config.perform_caching end |