Skip to main content

Cache

Struct Cache 

Source
pub struct Cache { /* private fields */ }
Expand description

A directory of cached responses, keyed by URL.

Each URL is stored as <key>.bin (the bytes) and <key>.json (a CacheEntry without the bytes), where <key> is the 64-bit FNV-1a hash of the URL in hex. The metadata records the URL itself, so two URLs whose hashes collide read as a miss rather than as each other’s data. Each file is written whole to a temporary name and renamed, the metadata last. Nothing locks the cache: two writers of one URL may leave one’s body beside the other’s fetch time.

Implementations§

Source§

impl Cache

Source

pub fn new(dir: impl Into<PathBuf>) -> Self

A cache in dir, created on the first write.

Source

pub fn dir(&self) -> &Path

The directory the cache lives in.

Source

pub fn platform_dir() -> Option<PathBuf>

The standard place for hpr’s cache on this platform, or None if the environment names no home folder.

HPR_CACHE_DIR, when set and not empty, wins on every platform; it is used as given, so a relative path is taken from the current directory. Otherwise:

platformfolder
macOS$HOME/Library/Caches/hpr-sim
Windows%LOCALAPPDATA%\hpr-sim\cache
Linux and other Unix$XDG_CACHE_HOME/hpr-sim, or $HOME/.cache/hpr-sim

XDG_CACHE_HOME counts only when it is an absolute path, as the XDG Base Directory Specification says. The folder is not created until the first write.

Source

pub fn get(&self, url: &str) -> Result<Option<CacheEntry>, NetError>

The cached entry for url, or None if there is none.

§Errors

NetError::Cache if a file exists but cannot be read; NetError::CorruptEntry if the metadata does not parse.

Source

pub fn put( &self, url: &str, body: &[u8], fetched_at_s: u64, ) -> Result<(), NetError>

Stores body as url’s response, fetched at fetched_at_s.

§Errors

NetError::Cache if the directory or a file cannot be written.

Trait Implementations§

Source§

impl Clone for Cache

Source§

fn clone(&self) -> Cache

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Cache

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Cache

§

impl RefUnwindSafe for Cache

§

impl Send for Cache

§

impl Sync for Cache

§

impl Unpin for Cache

§

impl UnsafeUnpin for Cache

§

impl UnwindSafe for Cache

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.