Class: Archaeo::CdxCache
- Inherits:
-
Object
- Object
- Archaeo::CdxCache
- Defined in:
- lib/archaeo/cdx_cache.rb
Overview
Persists CDX API query results to disk for resume support.
Caches snapshot lists keyed by query parameters so that interrupted downloads can resume without re-querying CDX.
Constant Summary collapse
- CACHE_DIR =
".cache"
Instance Method Summary collapse
- #cache_key(url, options = {}) ⇒ Object
- #cached?(url, **options) ⇒ Boolean
- #clear(url = nil, **options) ⇒ Object
- #fetch(url, **options) ⇒ Object
-
#initialize(base_dir) ⇒ CdxCache
constructor
A new instance of CdxCache.
Constructor Details
Instance Method Details
#cache_key(url, options = {}) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/archaeo/cdx_cache.rb', line 36 def cache_key(url, = {}) parts = [url.to_s] parts << [:from].to_s if [:from] parts << [:to].to_s if [:to] parts << [:match_type].to_s if [:match_type] parts += Array([:filters]).map(&:to_s) if [:filters] parts += Array([:collapse]).map(&:to_s) if [:collapse] parts << [:sort].to_s if [:sort] Digest::SHA256.hexdigest(parts.join("|"))[0, 16] end |
#cached?(url, **options) ⇒ Boolean
32 33 34 |
# File 'lib/archaeo/cdx_cache.rb', line 32 def cached?(url, **) File.exist?(cache_path(cache_key(url, ))) end |
#clear(url = nil, **options) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/archaeo/cdx_cache.rb', line 47 def clear(url = nil, **) if url FileUtils.rm_f(cache_path(cache_key(url, ))) else FileUtils.rm_rf(@cache_dir) end end |
#fetch(url, **options) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/archaeo/cdx_cache.rb', line 19 def fetch(url, **) key = cache_key(url, ) path = cache_path(key) if File.exist?(path) load_cache(path) else snapshots = yield save_cache(path, url, , snapshots) snapshots end end |