-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1919 from kmuto/fix-image-finder
Add `ReVIEW::Book::Cache` and use it in `ReVIEW::Book::ImageFinder`
- Loading branch information
Showing
4 changed files
with
62 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters