#cache #http

bin+lib cached-path

Download and cache HTTP resources

17 releases

0.6.1 Feb 25, 2023
0.6.0 Dec 19, 2022
0.5.3 Mar 7, 2022
0.5.1 Mar 30, 2021
0.1.0 Dec 30, 2019

#96 in HTTP client

Download history 4008/week @ 2023-12-11 3490/week @ 2023-12-18 2930/week @ 2023-12-25 2748/week @ 2024-01-01 4030/week @ 2024-01-08 3656/week @ 2024-01-15 4076/week @ 2024-01-22 4291/week @ 2024-01-29 4336/week @ 2024-02-05 5660/week @ 2024-02-12 5350/week @ 2024-02-19 5043/week @ 2024-02-26 5115/week @ 2024-03-04 4799/week @ 2024-03-11 5505/week @ 2024-03-18 5262/week @ 2024-03-25

21,136 downloads per month
Used in 8 crates (5 directly)


1.5K SLoC


crates.io Documentation MIT/Apache-2 licensed CI

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 the Python library allenai/cached_path.


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 https://github.com/epwalsh/rust-cached-path/blob/main/README.md

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

use cached_path::cached_path;

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

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 https://raw.githubusercontent.com/epwalsh/rust-cached-path/main/test_fixtures/utf-8_sample/archives/utf-8.tar.gz

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("README.md").unwrap();
# From the command line:
$ cached-path --dir /tmp/my-cache/ --connect-timeout 3 README.md


~504K SLoC