Class: VivlioStarter::CLI::Metrics::Cache
- Inherits:
-
Object
- Object
- VivlioStarter::CLI::Metrics::Cache
- Defined in:
- lib/vivlio_starter/cli/metrics/cache.rb
Overview
メトリクスキャッシュを管理する
Constant Summary collapse
- CACHE_DIR =
'.cache/metrics'- DISABLE_ENV =
'VIVLIO_METRICS_CACHE'
Instance Method Summary collapse
-
#cache_file_path(basename) ⇒ Object
キャッシュファイルのパスを取得する.
-
#clear! ⇒ Object
全キャッシュをクリアする.
-
#enabled? ⇒ Boolean
キャッシュが有効か判定する.
-
#ensure_cache_dir! ⇒ Object
キャッシュディレクトリを初期化する.
-
#fresh?(basename, source_path) ⇒ Boolean
キャッシュが有効(新鮮)か判定する.
-
#initialize(cache_dir: CACHE_DIR) ⇒ Cache
constructor
A new instance of Cache.
-
#read(basename, source_path) ⇒ Object
キャッシュからデータを読み込む.
-
#write(basename, data, source_path: nil) ⇒ Object
キャッシュにデータを書き込む.
Constructor Details
Instance Method Details
#cache_file_path(basename) ⇒ Object
キャッシュファイルのパスを取得する
95 |
# File 'lib/vivlio_starter/cli/metrics/cache.rb', line 95 def cache_file_path(basename) = File.join(cache_dir, "#{basename}.yml") |
#clear! ⇒ Object
全キャッシュをクリアする
90 91 92 |
# File 'lib/vivlio_starter/cli/metrics/cache.rb', line 90 def clear! FileUtils.rm_rf(cache_dir) end |
#enabled? ⇒ Boolean
キャッシュが有効か判定する
40 41 42 |
# File 'lib/vivlio_starter/cli/metrics/cache.rb', line 40 def enabled? ENV[DISABLE_ENV] != '0' end |
#ensure_cache_dir! ⇒ Object
キャッシュディレクトリを初期化する
45 46 47 |
# File 'lib/vivlio_starter/cli/metrics/cache.rb', line 45 def ensure_cache_dir! FileUtils.mkdir_p(cache_dir) end |
#fresh?(basename, source_path) ⇒ Boolean
キャッシュが有効(新鮮)か判定する
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/vivlio_starter/cli/metrics/cache.rb', line 50 def fresh?(basename, source_path) return false unless enabled? return false unless source_path cache_path = cache_file_path(basename) return false unless File.exist?(cache_path) return false unless File.exist?(source_path) cache_mtime = File.mtime(cache_path) source_mtime = File.mtime(source_path) cache_mtime >= source_mtime rescue Errno::ENOENT false end |
#read(basename, source_path) ⇒ Object
キャッシュからデータを読み込む
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/vivlio_starter/cli/metrics/cache.rb', line 67 def read(basename, source_path) return nil unless fresh?(basename, source_path) cache_path = cache_file_path(basename) data = YAML.safe_load_file(cache_path, permitted_classes: [Symbol]) CacheEntry.new(basename:, mtime: File.mtime(cache_path), data:) rescue Psych::SyntaxError, Errno::ENOENT nil end |
#write(basename, data, source_path: nil) ⇒ Object
キャッシュにデータを書き込む
79 80 81 82 83 84 85 86 87 |
# File 'lib/vivlio_starter/cli/metrics/cache.rb', line 79 def write(basename, data, source_path: nil) # rubocop:disable Lint/UnusedMethodArgument return unless enabled? ensure_cache_dir! cache_path = cache_file_path(basename) File.write(cache_path, data.to_yaml) rescue Errno::EACCES => e Common.log_warn("キャッシュ書き込みに失敗: #{e.}") end |