Struct DefaultCacheStorage

Source
pub struct DefaultCacheStorage(/* private fields */);
Expand description

The default cache storage implementation.

§Layout

This storage implementation uses the following directory structure:

<root>/
├─ <storage-version>/
│  ├─ responses/
│  │  ├─ <key>
│  │  ├─ <key>
│  │  ├─ ...
│  ├─ content/
│  │  ├─ <digest>
│  │  ├─ <digest>
│  │  ├─ ...
│  ├─ tmp/

Where <root> is the root storage directory, <storage-version> is a constant that changes when the directory layout changes (currently v1), <key> is supplied by the cache, and <digest> is the calculated digest of a response body.

§The responses directory

The responses directory contains a file for each cached response.

The file is a bincode-serialized CachedResponse that contains information about the response, including the response body content digest.

§Response file locking

Advisory file locks are obtained on a response file as the cache entries are read and updated.

This is used to coordinate access to the storage via this library; it does not protect against external modifications to the storage.

§The content directory

The content directory contains a file for each cached response body.

The file name is the digest of the response body contents.

Currently the blake3 hash algorithm is used for calculating response body digests.

§The tmp directory

The tmp directory is used for temporarily storing response bodies as they are saved to the cache.

The content digest of the response is calculated as the response is written into temporary storage.

Once the response body has been fully read, the temporary file is atomically renamed to its content directory location; if the content already exists, the temporary file is deleted.

§Integrity

This storage implementation does not provide strong guarantees on the integrity of the stored response bodies.

If the storage is externally modified, the modification will go undetected and the modified response bodies will be served.

§Fault tolerance

If an error occurs while updating a cache entry with DefaultCacheStorage::put, a future DefaultCacheStorage::get call will treat the entry as not present.

Implementations§

Source§

impl DefaultCacheStorage

Source

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

Constructs a new default cache storage with the given

Trait Implementations§

Source§

impl CacheStorage for DefaultCacheStorage

Source§

async fn get<B: HttpBody>(&self, key: &str) -> Result<Option<StoredResponse<B>>>

Gets a previously stored response for the given response key. Read more
Source§

async fn put( &self, key: &str, parts: &Parts, policy: &CachePolicy, digest: &str, ) -> Result<()>

Puts a response with an existing content digest into the storage for the given response key. Read more
Source§

async fn put_with_body<B: HttpBody>( &self, key: &str, parts: &Parts, policy: &CachePolicy, body: B, ) -> Result<(Body<B>, String)>

Puts a response with supplied body into the storage for the given response key. Read more
Source§

async fn delete(&self, key: &str) -> Result<()>

Deletes a previously cached response for the given response key. Read more
Source§

fn body_path(&self, digest: &str) -> PathBuf

Gets the path to a response body for the given content digest. Read more

Auto Trait Implementations§

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
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more