Crate cached_path[][src]

The idea behind cached-path is to provide a unified, simple interface for accessing both local and remote files. This can be used behind other APIs that need to access files agnostic to where they are located.

This is based on allennlp/common/ and transformers/


cached-path can be used as both a library and a command-line tool. To install cached-path as a command-line tool, run

cargo install --features build-binary cached-path


For remote resources, cached-path downloads and caches the resource, using the ETAG to know when to update the cache. The path returned is the local path to the latest cached version:

use cached_path::cached_path;

let path = cached_path(
# From the command line:
$ cached-path

For local files, the path returned is just the original path supplied:

use cached_path::cached_path;

let path = cached_path("").unwrap();
assert_eq!(path.to_str().unwrap(), "");
# From the command line:
$ cached-path

For resources that are archives, like *.tar.gz files, cached-path can also automatically extract the files:

use cached_path::{cached_path_with_options, Options};

let path = cached_path_with_options(
# From the command line:
$ cached-path --extract

It’s also easy to customize the cache location, the HTTP client, and other options using a CacheBuilder to construct a custom Cache object. This is the recommended thing to do if your application makes multiple calls to cached_path, since it avoids the overhead of creating a new HTTP client on each call:

use cached_path::Cache;

let cache = Cache::builder()
let path = cache.cached_path("").unwrap();
# From the command line:
$ cached-path --dir /tmp/my-cache/ --connect-timeout 3



Fetches and manages resources in a local cache directory.


Builder to facilitate creating Cache objects.


Options to use with Cache::cached_path_with_options.



Errors that can occur during caching.


Progress bar types.



Get the cached path to a resource.


Get the cached path to a resource using the given options.