Class: Ace::Support::Models::Molecules::CacheManager

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(cache_dir: nil) ⇒ CacheManager

Initialize cache manager

Parameters:

  • cache_dir (String, nil) (defaults to: nil)

    Cache directory (default: from CachePathResolver)



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_dirObject (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

Returns:

  • (Boolean)


81
82
83
# File 'lib/ace/support/models/molecules/cache_manager.rb', line 81

def cached?
  exists?
end

#clearHash

Clear cache

Returns:

  • (Hash)

    Result with status and deleted files



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

Returns:

  • (Boolean)


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)

Parameters:

  • max_age (Integer) (defaults to: 86_400)

    Max age in seconds (default: 24 hours)

Returns:

  • (Boolean)


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

Parameters:

  • provider_id (String)

    Provider ID

Returns:

  • (Hash, nil)

    Provider data or nil if not found



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_atTime?

Get last sync time

Returns:

  • (Time, nil)


136
137
138
139
140
141
142
143
# File 'lib/ace/support/models/molecules/cache_manager.rb', line 136

def last_sync_at
  meta = 
  return nil unless meta["last_sync_at"]

  Time.parse(meta["last_sync_at"])
rescue ArgumentError
  nil
end

#list_providersArray<Hash>

List all providers with model counts

Returns:

  • (Array<Hash>)

    Provider info hashes



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

#metadataHash

Read cache metadata

Returns:

  • (Hash)

    Metadata hash



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

#readHash?

Read current API cache

Returns:

  • (Hash, nil)

    Parsed API data or nil



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_previousHash?

Read previous API cache (for diff)

Returns:

  • (Hash, nil)

    Parsed previous API data or nil



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

Parameters:

  • data (Hash)

    API data

Returns:

  • (Boolean)

    true on success



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