Class: Nanoc::Core::CompilationPhases::Cache

Inherits:
Abstract
  • Object
show all
Includes:
Nanoc::Core::ContractsSupport
Defined in:
lib/nanoc/core/compilation_phases/cache.rb

Overview

Provides functionality for (re)calculating the content of an item rep, with caching or outdatedness checking. Delegates to s::Recalculate if outdated or no cache available.

Instance Method Summary collapse

Methods included from Nanoc::Core::ContractsSupport

enabled?, included, setup_once, warn_about_performance

Methods inherited from Abstract

#call, #start, #stop

Constructor Details

#initialize(wrapped:, compiled_content_cache:, compiled_content_store:) ⇒ Cache

Returns a new instance of Cache.



11
12
13
14
15
16
# File 'lib/nanoc/core/compilation_phases/cache.rb', line 11

def initialize(wrapped:, compiled_content_cache:, compiled_content_store:)
  super(wrapped: wrapped)

  @compiled_content_cache = compiled_content_cache
  @compiled_content_store = compiled_content_store
end

Instance Method Details

#can_reuse_content_for_rep?(rep, is_outdated:) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
# File 'lib/nanoc/core/compilation_phases/cache.rb', line 38

def can_reuse_content_for_rep?(rep, is_outdated:)
  if is_outdated
    false
  else
    @compiled_content_cache.full_cache_available?(rep)
  end
end

#run(rep, is_outdated:) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/nanoc/core/compilation_phases/cache.rb', line 19

def run(rep, is_outdated:)
  if can_reuse_content_for_rep?(rep, is_outdated: is_outdated)
    # If cached content can be used for this item rep, do so, and skip
    # recalculation of the item rep compiled content.
    Nanoc::Core::NotificationCenter.post(:cached_content_used, rep)
    @compiled_content_store.set_all(rep, @compiled_content_cache[rep])
  else
    # Cached content couldn’t be used for this rep. Continue as usual with
    # recalculation of the item rep compiled content.
    yield

    # Update compiled content cache, now that the item rep is compiled.
    @compiled_content_cache[rep] = @compiled_content_store.get_all(rep)
  end

  rep.compiled = true
end