rust-cached-path
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
.
Installation
cached-path
can be used as both a library and a command-line tool. To install cached-path
as a command-line tool, run
Usage
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;
let path = cached_path.unwrap;
assert!;
# From the command line:
For local files, the path returned is just the original path supplied:
use cached_path;
let path = cached_path.unwrap;
assert_eq!;
# From the command line:
For resources that are archives, like *.tar.gz
files, cached-path
can also
automatically extract the files:
use ;
let path = cached_path_with_options.unwrap;
assert!;
# From the command line:
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 Cache;
let cache = builder
.dir
.connect_timeout
.build.unwrap;
let path = cache.cached_path.unwrap;
# From the command line: