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.

Defined Under Namespace

Classes: LazyCompressedValue

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.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/nanoc/core/textual_compiled_content_cache.rb', line 41

def initialize(config:)
  super(
    self.class.tmp_path_for(
      config:,
      store_name: 'compiled_content',
    ),
    5,
  )

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



59
60
61
62
# File 'lib/nanoc/core/textual_compiled_content_cache.rb', line 59

def [](rep)
  item_cache = @cache[rep.item.identifier] || {}
  item_cache[rep.name]&.uncompressed
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.



76
77
78
79
# File 'lib/nanoc/core/textual_compiled_content_cache.rb', line 76

def []=(rep, content)
  @cache[rep.item.identifier] ||= {}
  @cache[rep.item.identifier][rep.name] = LazyCompressedValue.new(uncompressed: content)
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)


65
66
67
68
# File 'lib/nanoc/core/textual_compiled_content_cache.rb', line 65

def include?(rep)
  item_cache = @cache[rep.item.identifier] || {}
  item_cache.key?(rep.name)
end

#load_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.

Similar to Store#load_data, but does not use zlib compression on the data itself (instead, values are compressed individually).



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

def load_data
  raw_data = File.binread(data_filename)
  self.data = Marshal.load(raw_data)
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.



81
82
83
84
85
86
87
88
89
90
# File 'lib/nanoc/core/textual_compiled_content_cache.rb', line 81

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

#reset_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.

Identical to Store#store_data; replicated for clarity.



107
108
109
# File 'lib/nanoc/core/textual_compiled_content_cache.rb', line 107

def reset_data
  FileUtils.rm_f(data_filename)
end

#store_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.

Similar to Store#store_data, but does not use zlib compression on the data itself (instead, values are compressed individually).



101
102
103
104
# File 'lib/nanoc/core/textual_compiled_content_cache.rb', line 101

def store_data
  raw_data = Marshal.dump(data)
  write_data_to_file(data_filename, raw_data)
end