lib/nanoc/base/repos/compiled_content_cache.rb in nanoc-4.11.1 vs lib/nanoc/base/repos/compiled_content_cache.rb in nanoc-4.11.2

- old
+ new

@@ -9,54 +9,60 @@ class CompiledContentCache < ::Nanoc::Int::Store include Nanoc::Core::ContractsSupport contract C::KeywordArgs[config: Nanoc::Core::Configuration] => C::Any def initialize(config:) - super(Nanoc::Int::Store.tmp_path_for(config: config, store_name: 'compiled_content'), 2) + @textual_cache = Nanoc::Int::TextualCompiledContentCache.new(config: config) + @binary_cache = Nanoc::Int::BinaryCompiledContentCache.new(config: config) - @cache = {} + @wrapped_caches = [@textual_cache, @binary_cache] end contract Nanoc::Core::ItemRep => C::Maybe[C::HashOf[Symbol => Nanoc::Core::Content]] # 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. def [](rep) - item_cache = @cache[rep.item.identifier] || {} - item_cache[rep.name] + textual_content_map = @textual_cache[rep] + binary_content_map = @binary_cache[rep] + + # If either the textual or the binary content cache is nil, assume the + # cache is entirely absent. + # + # This is necessary to support the case where only textual content is + # cached (which was the case in older versions of Nanoc). + return nil if [textual_content_map, binary_content_map].any?(&:nil?) + + textual_content_map.merge(binary_content_map) end - contract Nanoc::Core::ItemRep, C::HashOf[Symbol => Nanoc::Core::Content] => C::HashOf[Symbol => Nanoc::Core::Content] + contract Nanoc::Core::ItemRep, C::HashOf[Symbol => Nanoc::Core::Content] => C::Any # 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. + # names and the values the compiled content at the given snapshot. def []=(rep, content) - @cache[rep.item.identifier] ||= {} - @cache[rep.item.identifier][rep.name] = content + @textual_cache[rep] = content.select { |_key, c| c.textual? } + @binary_cache[rep] = content.select { |_key, c| c.binary? } end - def prune(items:) - item_identifiers = Set.new(items.map(&:identifier)) + def prune(*args) + @wrapped_caches.each { |w| w.prune(*args) } + end - @cache.keys.each do |key| - @cache.delete(key) unless item_identifiers.include?(key) - end + # True if there is cached compiled content available for this item, and + # all entries are present (either textual or binary). + def full_cache_available?(rep) + @textual_cache.include?(rep) && @binary_cache.include?(rep) end - protected - - def data - @cache + def load(*args) + @wrapped_caches.each { |w| w.load(*args) } end - def data=(new_data) - @cache = {} - - new_data.each_pair do |item_identifier, content_per_rep| - @cache[item_identifier] ||= content_per_rep - end + def store(*args) + @wrapped_caches.each { |w| w.store(*args) } end end end end