class Nanoc::Core::TextualCompiledContentCache

Represents a cache than can be used to store already compiled content, to prevent it from being needlessly recompiled.

@api private

Public Class Methods

new(config:) click to toggle source
Calls superclass method Nanoc::Core::Store::new
# File lib/nanoc/core/textual_compiled_content_cache.rb, line 13
def initialize(config:)
  super(Nanoc::Core::Store.tmp_path_for(config: config, store_name: 'compiled_content'), 3)

  @cache = {}
end

Public Instance Methods

[](rep) click to toggle source

Returns the cached compiled content for the given item representation.

This cached compiled content is a hash where the keys are the snapshot names, and the values the compiled content at the given snapshot.

# File lib/nanoc/core/textual_compiled_content_cache.rb, line 24
def [](rep)
  item_cache = @cache[rep.item.identifier] || {}
  item_cache[rep.name]
end
[]=(rep, content) click to toggle source

Sets the compiled content for the given representation.

This cached compiled content is a hash where the keys are the snapshot names, and the values the compiled content at the given snapshot.

# File lib/nanoc/core/textual_compiled_content_cache.rb, line 40
def []=(rep, content)
  # FIXME: once the binary content cache is properly enabled (no longer
  # behind a feature flag), change contract to be TextualContent, rather
  # than Content.

  @cache[rep.item.identifier] ||= {}
  @cache[rep.item.identifier][rep.name] = content
end
full_cache_available?(rep) click to toggle source

True if there is cached compiled content available for this item, and all entries are textual.

# File lib/nanoc/core/textual_compiled_content_cache.rb, line 62
def full_cache_available?(rep)
  cache = self[rep]
  cache ? cache.none? { |_snapshot_name, content| content.binary? } : false
end
include?(rep) click to toggle source
# File lib/nanoc/core/textual_compiled_content_cache.rb, line 30
def include?(rep)
  item_cache = @cache[rep.item.identifier] || {}
  item_cache.key?(rep.name)
end
prune(items:) click to toggle source
# File lib/nanoc/core/textual_compiled_content_cache.rb, line 49
def prune(items:)
  item_identifiers = Set.new(items.map(&:identifier))

  @cache.each_key do |key|
    # TODO: remove unused item reps
    next if item_identifiers.include?(key)

    @cache.delete(key)
  end
end

Protected Instance Methods

data() click to toggle source
# File lib/nanoc/core/textual_compiled_content_cache.rb, line 69
def data
  @cache
end
data=(new_data) click to toggle source
# File lib/nanoc/core/textual_compiled_content_cache.rb, line 73
def data=(new_data)
  @cache = {}

  new_data.each_pair do |item_identifier, content_per_rep|
    @cache[item_identifier] ||= content_per_rep
  end
end