Class: DurableHuggingfaceHub::Cache::DeleteCacheStrategy
- Inherits:
-
Object
- Object
- DurableHuggingfaceHub::Cache::DeleteCacheStrategy
- Defined in:
- lib/durable_huggingface_hub/cache.rb
Overview
Strategy for deleting cache entries.
This class provides a safe way to plan and execute cache cleanup operations. It allows previewing what will be deleted before actually performing the deletion.
Instance Attribute Summary collapse
-
#files ⇒ Array<DurableHuggingfaceHub::Types::CachedFileInfo>
readonly
Individual files to delete.
-
#repos ⇒ Array<DurableHuggingfaceHub::Types::CachedRepoInfo>
readonly
Repositories to delete.
-
#revisions ⇒ Array<DurableHuggingfaceHub::Types::CachedRevisionInfo>
readonly
Revisions to delete.
Instance Method Summary collapse
-
#execute ⇒ Boolean
Execute the deletion strategy.
-
#file_count ⇒ Integer
Number of files that will be deleted.
-
#initialize(cache_dir:, repos: [], revisions: [], files: []) ⇒ DeleteCacheStrategy
constructor
Initialize a new delete strategy.
-
#preview ⇒ String
Preview what will be deleted.
-
#repo_count ⇒ Integer
Number of repositories that will be deleted.
-
#revision_count ⇒ Integer
Number of revisions that will be deleted.
-
#size_to_delete ⇒ Integer
Total size that will be deleted in bytes.
-
#size_to_delete_str ⇒ String
Human-readable size string for what will be deleted.
Constructor Details
#initialize(cache_dir:, repos: [], revisions: [], files: []) ⇒ DeleteCacheStrategy
Initialize a new delete strategy.
310 311 312 313 314 315 |
# File 'lib/durable_huggingface_hub/cache.rb', line 310 def initialize(cache_dir:, repos: [], revisions: [], files: []) @cache_dir = cache_dir @repos = repos @revisions = revisions @files = files end |
Instance Attribute Details
#files ⇒ Array<DurableHuggingfaceHub::Types::CachedFileInfo> (readonly)
Returns Individual files to delete.
302 303 304 |
# File 'lib/durable_huggingface_hub/cache.rb', line 302 def files @files end |
#repos ⇒ Array<DurableHuggingfaceHub::Types::CachedRepoInfo> (readonly)
Returns Repositories to delete.
296 297 298 |
# File 'lib/durable_huggingface_hub/cache.rb', line 296 def repos @repos end |
#revisions ⇒ Array<DurableHuggingfaceHub::Types::CachedRevisionInfo> (readonly)
Returns Revisions to delete.
299 300 301 |
# File 'lib/durable_huggingface_hub/cache.rb', line 299 def revisions @revisions end |
Instance Method Details
#execute ⇒ Boolean
Execute the deletion strategy.
This method will delete all specified repositories, revisions, and files. Use with caution - deletions are permanent.
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
# File 'lib/durable_huggingface_hub/cache.rb', line 391 def execute # Delete individual files first @files.each do |file_info| delete_file_safely(file_info.file_path) end # Delete revisions @revisions.each do |revision_info| delete_revision_safely(revision_info) end # Delete entire repositories @repos.each do |repo_info| delete_repository_safely(repo_info) end true end |
#file_count ⇒ Integer
Number of files that will be deleted.
357 358 359 |
# File 'lib/durable_huggingface_hub/cache.rb', line 357 def file_count @files.length end |
#preview ⇒ String
Preview what will be deleted.
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
# File 'lib/durable_huggingface_hub/cache.rb', line 364 def preview summary = [] has_items = repo_count.positive? || revision_count.positive? || file_count.positive? if has_items summary << "Will delete:" summary << " #{repo_count} repositories" if repo_count.positive? summary << " #{revision_count} revisions" if revision_count.positive? summary << " #{file_count} files" if file_count.positive? summary << "Total size: #{size_to_delete_str}" if repo_count.positive? summary << "" summary << "Repositories:" @repos.each { |repo| summary << " #{repo.repo_id} (#{repo.size_str})" } end end summary.join("\n") end |
#repo_count ⇒ Integer
Number of repositories that will be deleted.
343 344 345 |
# File 'lib/durable_huggingface_hub/cache.rb', line 343 def repo_count @repos.length end |
#revision_count ⇒ Integer
Number of revisions that will be deleted.
350 351 352 |
# File 'lib/durable_huggingface_hub/cache.rb', line 350 def revision_count @revisions.length end |
#size_to_delete ⇒ Integer
Total size that will be deleted in bytes.
320 321 322 |
# File 'lib/durable_huggingface_hub/cache.rb', line 320 def size_to_delete @repos.sum(&:size) + @revisions.sum(&:size) + @files.sum(&:size) end |
#size_to_delete_str ⇒ String
Human-readable size string for what will be deleted.
327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/durable_huggingface_hub/cache.rb', line 327 def size_to_delete_str units = ["B", "KB", "MB", "GB", "TB"] size = size_to_delete.to_f unit_index = 0 while size >= 1024 && unit_index < units.length - 1 size /= 1024.0 unit_index += 1 end format("%.1f %s", size, units[unit_index]) end |