Module: ActiveModel::Serializer::Caching::ClassMethods

Defined in:
lib/active_model/serializer/caching.rb

Instance Method Summary (collapse)

Instance Method Details

- (Boolean) _skip_digest?

Returns:

  • (Boolean)


61
62
63
# File 'lib/active_model/serializer/caching.rb', line 61

def _skip_digest?
  _cache_options && _cache_options[:skip_digest]
end

- (Object) cache(options = {})

TODO:

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

Examples:

class PostSerializer < ActiveModel::Serializer
  cache key: 'post', expires_in: 3.hours
  attributes :title, :body

  has_many :comments
end

Parameters:

  • options (Hash) (defaults to: {})

    with valid keys: cache_store : @see ::_cache key : @see ::_cache_key only : @see ::_cache_only except : @see ::_cache_except skip_digest : does not include digest in cache_key all else : @see ::_cache_options



95
96
97
98
99
100
101
102
103
104
# File 'lib/active_model/serializer/caching.rb', line 95

def cache(options = {})
  self._cache =
    options.delete(:cache_store) ||
    ActiveModelSerializers.config.cache_store ||
    ActiveSupport::Cache.lookup_store(:null_store)
  self._cache_key = options.delete(:key)
  self._cache_only = options.delete(:only)
  self._cache_except = options.delete(:except)
  self._cache_options = options.empty? ? nil : options
end

- (Boolean) cache_enabled?

Returns:

  • (Boolean)


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.

Returns:

  • (nil)

    when _cache is not set (i.e. when `cache` has not been called)

  • (._cache)

    when _cache is not the NullStore

  • (ActiveModelSerializers.config.cache_store)

    when _cache is the NullStore. This is so we can use `cache` being called to mean the serializer should be cached even if ActiveModelSerializers.config.cache_store has not yet been set. That means that when _cache is the NullStore and ActiveModelSerializers.config.cache_store is configured, `cache_store` becomes `ActiveModelSerializers.config.cache_store`.

  • (nil)

    when _cache is the NullStore and ActiveModelSerializers.config.cache_store is nil.



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?

Returns:

  • (Boolean)


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

Returns:

  • (true, false)


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