<commit_before># frozen_string_literal: true

module Nanoc
  module Core
    module CompilationPhases
      # 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.
      class Cache < Abstract
        include Nanoc::Core::ContractsSupport

        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

        contract Nanoc::Core::ItemRep, C::KeywordArgs[is_outdated: C::Bool], C::Func[C::None => C::Any] => C::Any
        def run(rep, is_outdated:)
          if can_reuse_content_for_rep?(rep, is_outdated: is_outdated)
            Nanoc::Core::NotificationCenter.post(:cached_content_used, rep)

            @compiled_content_store.set_all(rep, @compiled_content_cache[rep])
          else
            yield
          end

          rep.compiled = true
          @compiled_content_cache[rep] = @compiled_content_store.get_all(rep)
        end

        contract Nanoc::Core::ItemRep, C::KeywordArgs[is_outdated: C::Bool] => C::Bool
        def can_reuse_content_for_rep?(rep, is_outdated:)
          if is_outdated
            false
          else
            @compiled_content_cache.full_cache_available?(rep)
          end
        end
      end
    end
  end
end<commit_msg>Improve documentation for CompilationPhases::Cache<commit_after># frozen_string_literal: true

module Nanoc
  module Core
    module CompilationPhases
      # 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.
      class Cache < Abstract
        include Nanoc::Core::ContractsSupport

        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

        contract Nanoc::Core::ItemRep, C::KeywordArgs[is_outdated: C::Bool], C::Func[C::None => C::Any] => C::Any
        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

        contract Nanoc::Core::ItemRep, C::KeywordArgs[is_outdated: C::Bool] => C::Bool
        def can_reuse_content_for_rep?(rep, is_outdated:)
          if is_outdated
            false
          else
            @compiled_content_cache.full_cache_available?(rep)
          end
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.07642372381233402, 'fraction_numerical': 0.0, 'mean_word_length': 2.7008733624454146, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4148912', 'n_tokens_mistral': 986, 'n_tokens_neox': 937, 'n_words': 241}