Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ReVIEW::Book::Cache and use it in ReVIEW::Book::ImageFinder #1919

Merged
merged 4 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/review/book/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require 'review/configure'
require 'review/catalog'
require 'review/book/bib'
require 'review/book/cache'

module ReVIEW
module Book
Expand All @@ -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)
Expand All @@ -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

Expand Down
54 changes: 54 additions & 0 deletions lib/review/book/cache.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion lib/review/book/image_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading