From 07a6f5a201aa91aaf0f8ffadebd4ec3adb95da8e Mon Sep 17 00:00:00 2001 From: takahashim Date: Wed, 14 Aug 2024 12:25:23 +0900 Subject: [PATCH 1/4] Add ReVIEW::Book::Cache --- lib/review/book/base.rb | 4 +++ lib/review/book/cache.rb | 53 +++++++++++++++++++++++++++++++++ lib/review/book/image_finder.rb | 4 ++- 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 lib/review/book/cache.rb 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..997624970 --- /dev/null +++ b/lib/review/book/cache.rb @@ -0,0 +1,53 @@ +# +# 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.kind_of?(Symbol) + if has_key?(key) + read(key) + else + exec_block_and_save(key, &block) + end + end + + private + + def has_key?(key) + @store.has_key?(key) + end + + def read(key) + @store[key] + end + + def write(key, value) + @store[key] = value + end + + def exec_block_and_save(key, &block) + 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) From 15ec782092e4eb1a0206c061b556852b22b934c3 Mon Sep 17 00:00:00 2001 From: takahashim Date: Wed, 14 Aug 2024 14:51:05 +0900 Subject: [PATCH 2/4] reset cache on each `compile_block` --- test/test_helper.rb | 1 + 1 file changed, 1 insertion(+) 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 From 2c050b8d12af13faba8a8fe92d2665090dd30569 Mon Sep 17 00:00:00 2001 From: takahashim Date: Wed, 14 Aug 2024 14:55:12 +0900 Subject: [PATCH 3/4] rubocop --- lib/review/book/cache.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/review/book/cache.rb b/lib/review/book/cache.rb index 997624970..6dec993e2 100644 --- a/lib/review/book/cache.rb +++ b/lib/review/book/cache.rb @@ -19,8 +19,9 @@ def reset # key should be Symbol, not String def fetch(key, &block) - raise ArgumentError, 'Key should be Symbol' unless key.kind_of?(Symbol) - if has_key?(key) + raise ArgumentError, 'Key should be Symbol' unless key.is_a?(Symbol) + + if key?(key) read(key) else exec_block_and_save(key, &block) @@ -29,8 +30,8 @@ def fetch(key, &block) private - def has_key?(key) - @store.has_key?(key) + def key?(key) + @store.key?(key) end def read(key) @@ -41,7 +42,7 @@ def write(key, value) @store[key] = value end - def exec_block_and_save(key, &block) + def exec_block_and_save(key) result = yield(key) write(key, result) From 00dfb94d7f7c2310a2984da6f0f242ff63fab221 Mon Sep 17 00:00:00 2001 From: takahashim Date: Wed, 14 Aug 2024 14:57:33 +0900 Subject: [PATCH 4/4] rename (key? -> cached?) and make public --- lib/review/book/cache.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/review/book/cache.rb b/lib/review/book/cache.rb index 6dec993e2..27f5f8d78 100644 --- a/lib/review/book/cache.rb +++ b/lib/review/book/cache.rb @@ -21,19 +21,19 @@ def reset def fetch(key, &block) raise ArgumentError, 'Key should be Symbol' unless key.is_a?(Symbol) - if key?(key) + if cached?(key) read(key) else exec_block_and_save(key, &block) end end - private - - def key?(key) + def cached?(key) @store.key?(key) end + private + def read(key) @store[key] end