Class: Nanoc::Core::BinaryCompiledContentCache Private

Inherits:
Store
  • Object
show all
Includes:
ContractsSupport
Defined in:
lib/nanoc/core/binary_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:) ⇒ BinaryCompiledContentCache

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 BinaryCompiledContentCache.



13
14
15
16
17
# File 'lib/nanoc/core/binary_compiled_content_cache.rb', line 13

def initialize(config:)
  super(Nanoc::Core::Store.tmp_path_for(config:, store_name: 'binary_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
28
29
30
31
32
33
# File 'lib/nanoc/core/binary_compiled_content_cache.rb', line 24

def [](rep)
  item_cache = @cache[rep.item.identifier] || {}

  rep_cache = item_cache[rep.name]
  return nil if rep_cache.nil?

  rep_cache.transform_values do |filename|
    Nanoc::Core::Content.create(filename, binary: true)
  end
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.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/nanoc/core/binary_compiled_content_cache.rb', line 46

def []=(rep, content)
  @cache[rep.item.identifier] ||= {}
  @cache[rep.item.identifier][rep.name] ||= {}
  rep_cache = @cache[rep.item.identifier][rep.name]

  content.each do |snapshot, binary_content|
    # Check
    if Nanoc::Core::ContractsSupport.enabled? && !File.file?(binary_content.filename)
      raise Nanoc::Core::Errors::InternalInconsistency, "Binary content at #{binary_content.filename.inspect} does not exist, but is expected to."
    end

    filename = build_filename(rep, snapshot)
    rep_cache[snapshot] = filename

    # Avoid reassigning the same content if this binary cached content was
    # already used, because it was available and the item wasn’t oudated.
    next if binary_content.filename == filename

    # Copy
    FileUtils.mkdir_p(File.dirname(filename))
    smart_cp(binary_content.filename, filename)
  end
end

#dataObject

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.



83
84
85
# File 'lib/nanoc/core/binary_compiled_content_cache.rb', line 83

def data
  @cache
end

#data=(new_data) ⇒ 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.



87
88
89
90
91
92
93
# File 'lib/nanoc/core/binary_compiled_content_cache.rb', line 87

def data=(new_data)
  @cache = {}

  new_data.each_pair do |item_identifier, content_per_rep|
    @cache[item_identifier] ||= content_per_rep
  end
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)


36
37
38
39
# File 'lib/nanoc/core/binary_compiled_content_cache.rb', line 36

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.



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/nanoc/core/binary_compiled_content_cache.rb', line 70

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)
    path = dirname_for_item_identifier(key)
    FileUtils.rm_rf(path)
  end
end

#use_clonefile?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)


95
96
97
# File 'lib/nanoc/core/binary_compiled_content_cache.rb', line 95

def use_clonefile?
  defined?(Clonefile)
end