Class: Ace::Support::Models::Molecules::CacheManager
- Inherits:
-
Object
- Object
- Ace::Support::Models::Molecules::CacheManager
- Defined in:
- lib/ace/support/models/molecules/cache_manager.rb
Overview
Manages local cache of API data
Constant Summary collapse
- API_CACHE_FILE =
"api.json"- PREVIOUS_CACHE_FILE =
"api.previous.json"- METADATA_FILE =
"metadata.json"
Instance Attribute Summary collapse
-
#cache_dir ⇒ Object
readonly
Returns the value of attribute cache_dir.
Instance Method Summary collapse
-
#cached? ⇒ Boolean
Alias for exists? for clearer CLI usage.
-
#clear ⇒ Hash
Clear cache.
-
#exists? ⇒ Boolean
Check if cache exists.
-
#fresh?(max_age: 86_400) ⇒ Boolean
Check if cache is fresh (less than max_age old).
-
#get_provider(provider_id) ⇒ Hash?
Get provider details with models.
-
#initialize(cache_dir: nil) ⇒ CacheManager
constructor
Initialize cache manager.
-
#last_sync_at ⇒ Time?
Get last sync time.
-
#list_providers ⇒ Array<Hash>
List all providers with model counts.
-
#metadata ⇒ Hash
Read cache metadata.
-
#read ⇒ Hash?
Read current API cache.
-
#read_previous ⇒ Hash?
Read previous API cache (for diff).
-
#write(data) ⇒ Boolean
Write API data to cache.
Constructor Details
#initialize(cache_dir: nil) ⇒ CacheManager
Initialize cache manager
19 20 21 |
# File 'lib/ace/support/models/molecules/cache_manager.rb', line 19 def initialize(cache_dir: nil) @cache_dir = cache_dir || Atoms::CachePathResolver.resolve end |
Instance Attribute Details
#cache_dir ⇒ Object (readonly)
Returns the value of attribute cache_dir.
15 16 17 |
# File 'lib/ace/support/models/molecules/cache_manager.rb', line 15 def cache_dir @cache_dir end |
Instance Method Details
#cached? ⇒ Boolean
Alias for exists? for clearer CLI usage
81 82 83 |
# File 'lib/ace/support/models/molecules/cache_manager.rb', line 81 def cached? exists? end |
#clear ⇒ Hash
Clear cache
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/ace/support/models/molecules/cache_manager.rb', line 147 def clear deleted_files = [] [api_cache_path, previous_cache_path, ].each do |path| if Atoms::FileReader.exist?(path) Atoms::FileWriter.delete(path) deleted_files << File.basename(path) end end { status: :success, deleted_files: deleted_files, message: deleted_files.empty? ? "Cache was already empty" : "Deleted #{deleted_files.size} files" } end |
#exists? ⇒ Boolean
Check if cache exists
75 76 77 |
# File 'lib/ace/support/models/molecules/cache_manager.rb', line 75 def exists? Atoms::FileReader.exist?(api_cache_path) end |
#fresh?(max_age: 86_400) ⇒ Boolean
Check if cache is fresh (less than max_age old)
127 128 129 130 131 132 |
# File 'lib/ace/support/models/molecules/cache_manager.rb', line 127 def fresh?(max_age: 86_400) mtime = Atoms::FileReader.mtime(api_cache_path) return false unless mtime (Time.now - mtime) < max_age end |
#get_provider(provider_id) ⇒ Hash?
Get provider details with models
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/ace/support/models/molecules/cache_manager.rb', line 103 def get_provider(provider_id) data = read return nil unless data provider_data = normalize_providers(data)[provider_id] return nil unless provider_data models = normalize_models(provider_data).map do |model_id, model_data| { id: model_id, name: model_data["name"] || model_id, deprecated: model_data["deprecated"] == true } end { id: provider_id, models: models.sort_by { |m| m[:id] } } end |
#last_sync_at ⇒ Time?
Get last sync time
136 137 138 139 140 141 142 143 |
# File 'lib/ace/support/models/molecules/cache_manager.rb', line 136 def last_sync_at = return nil unless ["last_sync_at"] Time.parse(["last_sync_at"]) rescue ArgumentError nil end |
#list_providers ⇒ Array<Hash>
List all providers with model counts
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/ace/support/models/molecules/cache_manager.rb', line 87 def list_providers data = read return [] unless data normalize_providers(data).map do |provider_id, provider_data| models = normalize_models(provider_data) { id: provider_id, model_count: models.size } end end |
#metadata ⇒ Hash
Read cache metadata
64 65 66 67 68 69 70 71 |
# File 'lib/ace/support/models/molecules/cache_manager.rb', line 64 def content = Atoms::FileReader.read() return unless content Atoms::JsonParser.parse(content) rescue ApiError end |
#read ⇒ Hash?
Read current API cache
25 26 27 28 29 30 31 |
# File 'lib/ace/support/models/molecules/cache_manager.rb', line 25 def read content = Atoms::FileReader.read(api_cache_path) return nil unless content data = Atoms::JsonParser.parse(content) normalize_cache_data(data) end |
#read_previous ⇒ Hash?
Read previous API cache (for diff)
35 36 37 38 39 40 41 |
# File 'lib/ace/support/models/molecules/cache_manager.rb', line 35 def read_previous content = Atoms::FileReader.read(previous_cache_path) return nil unless content data = Atoms::JsonParser.parse(content) normalize_cache_data(data) end |
#write(data) ⇒ Boolean
Write API data to cache
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/ace/support/models/molecules/cache_manager.rb', line 46 def write(data) # Move current to previous first if Atoms::FileReader.exist?(api_cache_path) Atoms::FileWriter.rename(api_cache_path, previous_cache_path) end # Write new data json = Atoms::JsonParser.to_json(data, pretty: false) Atoms::FileWriter.write(api_cache_path, json) # Update metadata true end |