Class: I18n::Processes::Scanners::Files::CachingFileReader
- Inherits:
-
FileReader
- Object
- FileReader
- I18n::Processes::Scanners::Files::CachingFileReader
- Defined in:
- lib/i18n/processes/scanners/files/caching_file_reader.rb
Overview
Note:
This class is thread-safe. All methods are cached.
Reads the files in ‘rb’ mode and UTF-8 encoding. Wraps a FileReader and caches the results.
Instance Method Summary collapse
-
#initialize ⇒ CachingFileReader
constructor
A new instance of CachingFileReader.
-
#read_file(path) ⇒ String
Return the contents of the file at the given path.
Constructor Details
#initialize ⇒ CachingFileReader
Returns a new instance of CachingFileReader.
11 12 13 14 15 |
# File 'lib/i18n/processes/scanners/files/caching_file_reader.rb', line 11 def initialize super @mutex = Mutex.new @cache = {} end |
Instance Method Details
#read_file(path) ⇒ String
Note:
This method is cached, it will only access the filesystem on the first invocation.
Return the contents of the file at the given path. The file is read in the ‘rb’ mode and UTF-8 encoding.
23 24 25 26 |
# File 'lib/i18n/processes/scanners/files/caching_file_reader.rb', line 23 def read_file(path) absolute_path = File.(path) @cache[absolute_path] || @mutex.synchronize { @cache[absolute_path] ||= super } end |