Class: Nanoc::Core::TextualCompiledContentCache Private

Inherits:
Store
  • Object
show all
Includes:
ContractsSupport
Defined in:
lib/nanoc/core/textual_compiled_content_cache.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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

Instance Attribute Summary

Attributes inherited from Store

#filename, #version

Instance Method Summary collapse

Methods included from ContractsSupport

enabled?, included, setup_once, warn_about_performance

Methods inherited from Store

#load, #store, tmp_path_for, tmp_path_prefix

Constructor Details

#initialize(config:) ⇒ TextualCompiledContentCache

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.

Returns a new instance of TextualCompiledContentCache.



13
14
15
16
17
# 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

Instance Method Details

#[](rep) ⇒ Object

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.

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.



24
25
26
27
# 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) ⇒ Object

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.

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.



40
41
42
43
44
45
46
47
# 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) ⇒ Boolean

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.

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

Returns:

  • (Boolean)


62
63
64
65
# 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) ⇒ Boolean

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.

Returns:

  • (Boolean)


30
31
32
33
# 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:) ⇒ Object

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.



49
50
51
52
53
54
55
56
57
58
# 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