Skip to content

feat: add cache manager functionality#142

Draft
matthewhaynesonline wants to merge 3 commits into
huggingface:mainfrom
matthewhaynesonline:cache-manager
Draft

feat: add cache manager functionality#142
matthewhaynesonline wants to merge 3 commits into
huggingface:mainfrom
matthewhaynesonline:cache-manager

Conversation

@matthewhaynesonline

@matthewhaynesonline matthewhaynesonline commented Feb 27, 2026

Copy link
Copy Markdown

Depends on #143

Motivation

Begins the work of providing cache management features in the Rust client and providing parity with the Python client huggingface_hub/utils/_cache_manager.py. (which would be useful for a pet project I'm going to work on).

Summary

  • Port cache scanning and deletion strategy from python client _cache_manager.py
  • Add cache-manager feature flag for core functionality
  • Add cache-manager-display feature flag for table output
  • Add cache-manager example with usage docs

Note: there are some difference from the python implementation where it made sense from a rust perspective, but I tried to be faithful to the original.

$ cargo run --example cache-manager --features="cache-manager"

    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.07s
     Running `target/debug/examples/cache-manager`
[examples/cache-manager.rs:16:5] &cache_info = HFCacheInfo {
    size_on_disk: 76178327175,
    repos: {
        CachedRepoInfo {
            repo: Repo {
                repo_id: "HuggingFaceTB/SmolLM2-1.7B-Instruct",
                repo_type: Model,
                revision: "main",
            },
            repo_path: "/Users/matt/.cache/huggingface/hub/models--HuggingFaceTB--SmolLM2-1.7B-Instruct",
            size_on_disk: 3426154904,
            nb_files: 8,
            revisions: {
               ...
            },
            last_accessed: Some(
                SystemTime {
                    tv_sec: 1738021666,
                    tv_nsec: 353517867,
                },
            ),
            last_modified: Some(
                SystemTime {
                    tv_sec: 1737955174,
                    tv_nsec: 621743227,
                },
            ),
        },
        ...
    },
    warnings: [
        RepoNotDir {
            ...
        },
        
        InvalidRepoName {
            ...
        },
    ],
    cache: Cache {
        path: "/Users/matt/.cache/huggingface/hub",
    },
}
$ cargo run --example cache-manager --features="cache-manager" -- e8c3b32edf5434bc2275fc9bab85f82640a19130
   
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.23s
     Running `target/debug/examples/cache-manager e8c3b32edf5434bc2275fc9bab85f82640a19130`
...
[examples/cache-manager.rs:16:5] &cache_info = HFCacheInfo {
    size_on_disk: 76178327175,
    repos: {...}
}

[examples/cache-manager.rs:21:9] &delete_strat = DeleteCacheStrategy {
    expected_freed_size: 438682922,
    blobs: {},
    refs: {},
    repos: {
        "/Users/matt/.cache/huggingface/hub/models--sentence-transformers--all-mpnet-base-v2",
    },
    snapshots: {},
}
$ cargo run --example cache-manager --features="cache-manager,cache-manager-display"

   Compiling hf-hub v0.5.0 (/Users/matt/Code/matthewhaynes/hf-hub)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.38s
     Running `target/debug/examples/cache-manager`
     
+-------------------------------------------------+-----------+--------------+----------+---------------+---------------+------------------+---------------------------------------------------------------------------------------------+
| REPO ID                                         | REPO TYPE | SIZE ON DISK | NB FILES | LAST_ACCESSED | LAST_MODIFIED | REFS             | LOCAL PATH                                                                                  |
+========================================================================================================================================================================================================================================+
|-------------------------------------------------+-----------+--------------+----------+---------------+---------------+------------------+---------------------------------------------------------------------------------------------|
| HuggingFaceTB/SmolLM2-1.7B-Instruct             | model     |         3.4G | 8        | 1 year ago    | 1 year ago    | main             | /Users/matt/.cache/huggingface/hub/models--HuggingFaceTB--SmolLM2-1.7B-Instruct             |
|-------------------------------------------------+-----------+--------------+----------+---------------+---------------+------------------+---------------------------------------------------------------------------------------------|
...
|-------------------------------------------------+-----------+--------------+----------+---------------+---------------+------------------+---------------------------------------------------------------------------------------------|
| sentence-transformers/all-mpnet-base-v2         | model     |       438.7M | 11       | 2 days ago    | 6 days ago    | main             | /Users/matt/.cache/huggingface/hub/models--sentence-transformers--all-mpnet-base-v2         |
+-------------------------------------------------+-----------+--------------+----------+---------------+---------------+------------------+---------------------------------------------------------------------------------------------+%

- Move hardcoded strings into constants mod
- Add path mod for FS path construction utils
- Implement display / str traits for RepoType
- Use path utils mod for path construction in lib
@matthewhaynesonline matthewhaynesonline force-pushed the cache-manager branch 4 times, most recently from d0185b3 to 42d504d Compare March 2, 2026 01:46
- Port cache scanning and deletion strategy from python client _cache_manager.py
- Add cache-manager feature flag for core functionality
- Add cache-manager-display feature flag for table output
- Add cache-manager example with usage docs

Squashed commits:
[d58d607] Clean up cargo file (to not enable cache manager by default) and also clean up example
[968b82a] WIP cache manager
[338f83f] WIP cache manager
[e7a393e] WIP: cache manager
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant