Class: Kotoshu::Cache::FrequencyCache
- Defined in:
- lib/kotoshu/cache/frequency_cache.rb
Overview
Frequency cache for Kelly Project frequency lists.
Manages Kelly frequency list downloads from the kotoshu/frequency-list-kelly
repository. Resources are cached locally in $XDG_CACHE_HOME/kotoshu/frequency-lists/{code}/
with metadata for versioning and expiration.
Extends BaseCache for common download, metadata, and validation logic.
Constant Summary collapse
- KELLY_LANGUAGES =
Kelly Project languages available
%w[ar zh en el it no ru sv].freeze
- GITHUB_REPO =
GitHub repository for Kelly frequency lists
"kotoshu/frequency-list-kelly"- GITHUB_BRANCH =
"main"
Instance Attribute Summary
Attributes inherited from BaseCache
#cache_path, #cache_ttl, #github_url, #max_cache_size, #source_registry, #url_base
Instance Method Summary collapse
-
#available_languages ⇒ Array<String>
Get list of available languages.
-
#cached_resources ⇒ Array<String>
List all cached resources.
-
#get_frequency(language_code, force_download: false) ⇒ Hash?
Get frequency data for a language (alias for get).
-
#install_local(language_code, path:, force: false) ⇒ Hash
Install a local frequency file into the cache without going through the network.
-
#supports_resource?(resource_id) ⇒ Boolean
Check if a resource type is supported.
Methods inherited from BaseCache
#available?, #clean, #clear, #clear_all, #download, #evict, #get, #initialize, #read_metadata, #reset_stats, #stats
Constructor Details
This class inherits a constructor from Kotoshu::Cache::BaseCache
Instance Method Details
#available_languages ⇒ Array<String>
Get list of available languages.
37 38 39 |
# File 'lib/kotoshu/cache/frequency_cache.rb', line 37 def available_languages KELLY_LANGUAGES.dup end |
#cached_resources ⇒ Array<String>
List all cached resources.
98 99 100 101 102 103 104 105 106 |
# File 'lib/kotoshu/cache/frequency_cache.rb', line 98 def cached_resources directories = Dir.glob(File.join(@cache_path, "*")).select do |path| basename = File.basename(path) # Skip dotfiles AND the working-directory scratch slot that # BaseCache#initialize creates at <cache_path>/tmp/. File.directory?(path) && !basename.start_with?(".") && basename != "tmp" end directories.map { |path| File.basename(path) } end |
#get_frequency(language_code, force_download: false) ⇒ Hash?
Get frequency data for a language (alias for get).
46 47 48 |
# File 'lib/kotoshu/cache/frequency_cache.rb', line 46 def get_frequency(language_code, force_download: false) get(language_code, force_download: force_download) end |
#install_local(language_code, path:, force: false) ⇒ Hash
Install a local frequency file into the cache without going
through the network. Mirrors LanguageCache#install_local for
the frequency resource type — symlinks the user's file into the
cache layout and writes a local-source metadata record so
subsequent BaseCache#available? / BaseCache#get calls find it.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/kotoshu/cache/frequency_cache.rb', line 68 def install_local(language_code, path:, force: false) lang_path = language_dir(language_code) FileUtils.mkdir_p(lang_path) target_frequency = File.join(lang_path, "frequency.json") = (language_code) if File.exist?(target_frequency) || File.symlink?(target_frequency) raise ArgumentError, "#{target_frequency} already exists (use force: true to overwrite)" unless force File.unlink(target_frequency) end File.symlink(File.(path), target_frequency) (, "version" => Time.now.utc.iso8601, "url" => "local:#{File.(path)}", "language" => language_code, "type" => "kelly_frequency", "source" => "local", "checksum" => checksum(File.read(path)), "cached_at" => Time.now.utc.iso8601) { frequency_path: target_frequency, metadata_path: , source: :local } end |
#supports_resource?(resource_id) ⇒ Boolean
Check if a resource type is supported.
54 55 56 |
# File 'lib/kotoshu/cache/frequency_cache.rb', line 54 def supports_resource?(resource_id) KELLY_LANGUAGES.include?(resource_id) end |