diff --git a/lib/review/book/base.rb b/lib/review/book/base.rb index 6cad53f64..d094d7517 100644 --- a/lib/review/book/base.rb +++ b/lib/review/book/base.rb @@ -10,6 +10,7 @@ require 'review/configure' require 'review/catalog' require 'review/book/bib' +require 'review/book/cache' module ReVIEW module Book @@ -19,6 +20,7 @@ class Base attr_accessor :catalog attr_reader :basedir attr_accessor :bibpaper_index + attr_reader :cache def self.load(basedir = '.', config: nil) new(basedir, config: config) @@ -39,6 +41,8 @@ def initialize(basedir = '.', config: nil) @warn_old_files = {} # XXX for checking CHAPS, PREDEF, POSTDEF @basedir_seen = {} + + @cache = ReVIEW::Book::Cache.new update_rubyenv end diff --git a/lib/review/book/cache.rb b/lib/review/book/cache.rb new file mode 100644 index 000000000..27f5f8d78 --- /dev/null +++ b/lib/review/book/cache.rb @@ -0,0 +1,54 @@ +# +# Copyright (c) 2014-2024 Minero Aoki, Kenshi Muto, Masayoshi Takahashi +# +# This program is free software. +# You can distribute or modify this program under the terms of +# the GNU LGPL, Lesser General Public License version 2.1. +# For details of LGPL, see the file "COPYING". +# +module ReVIEW + module Book + class Cache + def initialize + @store = {} + end + + def reset + @store.clear + end + + # key should be Symbol, not String + def fetch(key, &block) + raise ArgumentError, 'Key should be Symbol' unless key.is_a?(Symbol) + + if cached?(key) + read(key) + else + exec_block_and_save(key, &block) + end + end + + def cached?(key) + @store.key?(key) + end + + private + + def read(key) + @store[key] + end + + def write(key, value) + @store[key] = value + end + + def exec_block_and_save(key) + result = yield(key) + + write(key, result) + + result + end + end + end +end diff --git a/lib/review/book/image_finder.rb b/lib/review/book/image_finder.rb index ed72fc841..cd6d89820 100644 --- a/lib/review/book/image_finder.rb +++ b/lib/review/book/image_finder.rb @@ -26,7 +26,9 @@ def entry_object(path) end def dir_entries - Dir.glob(File.join(@basedir, '**{,/*/**}/*.*')).uniq.sort.map { |entry| entry.sub(%r{^\./}, '') } + @book.cache.fetch(:image_finder_dir_entries) do + Dir.glob(File.join(@basedir, '**{,/*/**}/*.*')).uniq.sort.map { |entry| entry.sub(%r{^\./}, '') } + end end def add_entry(path) diff --git a/test/test_helper.rb b/test/test_helper.rb index 3d3fcd1c0..da68b8be8 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -35,6 +35,7 @@ def compile_inline(text) def compile_block(text) method_name = "compile_block_#{@builder.target_name}" method_name = 'compile_block_default' unless self.respond_to?(method_name, true) + @chapter.book.cache.reset self.__send__(method_name, text) end