Skip to main content

DownloadCache

Struct DownloadCache 

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

Download file cache manager with tiered L1 (Moka) + L2 (persistent) lookup.

On get_*: L1 โ†’ miss โ†’ L2 โ†’ backfill L1. On put_*: write to both layers.

Implementationsยง

Sourceยง

impl DownloadCache

Source

pub async fn new(config: &CacheConfig, ttl: Option<u64>) -> Result<Self>

Create a new DownloadCache with default TTL.

ยงArguments
  • config - The cache configuration specifying directories, TTLs, and backend settings.
  • ttl - Time-to-live for cache entries in seconds (optional).
ยงReturns

A new DownloadCache instance with default TTL (7 days).

ยงErrors

Returns an error if the backend initialization fails or the backend is ambiguous.

Source

pub async fn get_by_hash( &self, hash: &str, ) -> Result<Option<(CachedFile, PathBuf)>>

Retrieve a file by its content hash.

ยงArguments
  • hash - The SHA-256 hash of the file content.
ยงReturns

The cached file metadata and its path, or None if not found.

ยงErrors

Returns an error if the underlying cache backend fails.

Source

pub async fn get_by_video_and_format( &self, video_id: &str, format_id: &str, ) -> Result<Option<(CachedFile, PathBuf)>>

Retrieve a file by video ID and format ID.

ยงArguments
  • video_id - The video identifier.
  • format_id - The format identifier.
ยงReturns

The cached file metadata and its path, or None if not found.

ยงErrors

Returns an error if the underlying cache backend fails.

Source

pub async fn get_by_video_and_preferences( &self, video_id: &str, preferences: &FormatPreferences, ) -> Result<Option<(CachedFile, PathBuf)>>

Retrieve a file by video ID and quality/codec preferences.

ยงArguments
  • video_id - The video identifier.
  • preferences - The format preferences to match against.
ยงReturns

The cached file metadata and its path, or None if no match.

ยงErrors

Returns an error if the underlying cache backend fails.

Source

pub async fn get_thumbnail_by_video_id( &self, video_id: &str, ) -> Result<Option<(CachedThumbnail, PathBuf)>>

Retrieve a thumbnail by video ID.

ยงArguments
  • video_id - The video identifier.
ยงReturns

The cached thumbnail metadata and its path, or None if not found.

ยงErrors

Returns an error if the underlying cache backend fails.

Source

pub async fn get_subtitle_by_language( &self, video_id: &str, language: &str, ) -> Result<Option<(CachedFile, PathBuf)>>

Retrieve a subtitle by video ID and language code.

ยงArguments
  • video_id - The video identifier.
  • language - The language code (e.g., โ€œenโ€, โ€œesโ€).
ยงReturns

The cached subtitle file metadata and its path, or None if not found.

ยงErrors

Returns an error if the underlying cache backend fails.

Source

pub async fn put_file( &self, source_path: &Path, filename: impl Into<String>, video_id: Option<String>, format: Option<&Format>, ) -> Result<PathBuf>

Store a file in the cache (both layers).

ยงArguments
  • source_path - Path to the source file.
  • filename - Display name for the cached file.
  • video_id - Optional video ID association.
  • format - Optional format metadata to store alongside the file.
ยงErrors

Returns an error if the file cannot be hashed or stored.

Source

pub async fn put_file_with_preferences( &self, source_path: &Path, filename: impl Into<String>, video_id: Option<String>, format: Option<&Format>, preferences: &FormatPreferences, ) -> Result<PathBuf>

Store a file in the cache with quality/codec preferences (both layers).

ยงArguments
  • source_path - Path to the source file.
  • filename - Display name for the cached file.
  • video_id - Optional video ID association.
  • format - Optional format metadata.
  • preferences - The format preferences used for selection.
ยงErrors

Returns an error if the file cannot be hashed or stored.

Source

pub async fn put_thumbnail( &self, source_path: &Path, filename: impl Into<String>, video_id: String, ) -> Result<PathBuf>

Store a thumbnail in the cache (both layers).

ยงArguments
  • source_path - Path to the source thumbnail file.
  • filename - Display name for the cached thumbnail.
  • video_id - The video ID associated with this thumbnail.
ยงErrors

Returns an error if the thumbnail cannot be stored.

Source

pub async fn put_subtitle_file( &self, source_path: &Path, filename: impl Into<String>, video_id: String, language_code: String, ) -> Result<PathBuf>

Store a subtitle file in the cache (both layers).

ยงArguments
  • source_path - Path to the source subtitle file.
  • filename - Display name for the cached subtitle.
  • video_id - The video ID associated with this subtitle.
  • language_code - The language code (e.g., โ€œenโ€, โ€œesโ€).
ยงErrors

Returns an error if the subtitle cannot be stored.

Source

pub async fn remove(&self, id: &str) -> Result<()>

Remove a file from the cache (both layers).

ยงArguments
  • id - The unique identifier of the cached file.
ยงErrors

Returns an error if the removal operation fails.

Source

pub async fn clean(&self) -> Result<()>

Clean expired entries (both layers).

ยงErrors

Returns an error if the cleanup operation fails.

Source

pub async fn calculate_file_hash(path: &Path) -> Result<String>

Calculate the SHA-256 hash of a file.

ยงArguments
  • path - The path to the file to hash.
ยงReturns

A hex-encoded SHA-256 hash string.

ยงErrors

Returns an error if the file cannot be read.

Trait Implementationsยง

Sourceยง

impl Debug for DownloadCache

Sourceยง

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

Formats the value using the given formatter. 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> Downcast for T
where T: Any,

Sourceยง

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
Sourceยง

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Sourceยง

fn as_any(&self) -> &(dyn Any + 'static)

Converts &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Anyโ€™s vtable from &Traitโ€™s.
Sourceยง

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Converts &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Anyโ€™s vtable from &mut Traitโ€™s.
Sourceยง

impl<T> DowncastSend for T
where T: Any + Send,

Sourceยง

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Sourceยง

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Sourceยง

fn into_any_sync(self: Box<T>) -> Box<dyn Any + Send + Sync>

Converts Box<Trait> (where Trait: DowncastSync) to Box<dyn Any + Send + Sync>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Sourceยง

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync> โ“˜

Converts Arc<Trait> (where Trait: DowncastSync) to Arc<Any>, which can then be downcast into Arc<ConcreteType> where ConcreteType implements Trait.
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> Pointable for T

Sourceยง

const ALIGN: usize

The alignment of pointer.
Sourceยง

type Init = T

The type for initializers.
Sourceยง

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Sourceยง

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Sourceยง

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Sourceยง

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Sourceยง

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

Sourceยง

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Sourceยง

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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