Skip to main content

## SmartCache

# Struct SmartCache

Source

    pub struct SmartCache {
        config: Arc<Mutex<CacheConfig>>,
        album_play_history: Arc<Mutex<HashMap<String, Vec<String>>>>,
    }

Expand description

Smart caching engine

## Fields§

§ `config: Arc<Mutex<CacheConfig>>` § `album_play_history: Arc<Mutex<HashMap<String, Vec<String>>>>`

Track recently played items per album

## Implementations§

Source §

### impl SmartCache

Source

#### pub fn new(config: CacheConfig) -> Self

Source

#### pub fn update_config(&self, config: CacheConfig)

Update configuration

Source

#### pub fn should_precache_queue(&self) -> bool

Check if should pre-cache queue items.

Note this deliberately does NOT consult `wifi_only`. It used to return `queue_precache_enabled && !wifi_only`, which disabled precaching outright whenever the user enabled WiFi-only — regardless of the network actually in use. The network check now lives in the download queue pump (`downloads_allowed_on_current_network`), which is the single gate for all download traffic, so this only answers “is precaching enabled?”.

TRACES: UR-053 | DR-074

Source

#### pub fn queue_precache_count(&self) -> usize

Get number of queue items to pre-cache

Source

#### pub fn track_play(&self, item_id: &str, album_id: Option<&str>)

Track that an item was played

Source

#### pub fn should_cache_album(&self, album_id: &str) -> Option<bool>

Check if album affinity threshold reached for caching

Source

#### pub fn get_config(&self) -> Option<CacheConfig>

Get configuration

Source

#### pub fn get_album_play_history(&self) -> Vec<(String, usize)>

Get all tracked albums with their play counts Returns Vec<(album_id, unique_tracks_played)>

Source

#### pub async fn get_total_download_size_async<S: DatabaseService>( &self, db_service: &Arc<S>, user_id: &str, ) -> Result<u64, String>

Get total download size for a user (async version)

Source

#### pub async fn can_download_async<S: DatabaseService>( &self, db_service: &Arc<S>, user_id: &str, new_size: u64, ) -> bool

Check if storage limit allows download (async version)

Source

#### pub async fn reclaim_expired_async<S: DatabaseService>( &self, db_service: &Arc<S>, user_id: &str, now: &str, ) -> Result<usize, String>

Reclaim temporary downloads whose life limit has passed.

The time-based half of the temporary tier (DR-127); [`evict_lru_async`] is the space-pressure half. A row is reclaimed by whichever fires first.

Scoped to `download_source = 'auto'` for the same reason eviction is: a `'user'` row is someone’s own download and has no expiry. `COALESCE` guards rows predating migration 012, whose source is NULL and whose provenance must therefore be treated as the user’s.

Expiry is normally derived — `completed_at` plus the configured TTL — rather than stamped at completion. That means a TTL change applies to entries already on disk instead of only to future ones, and entries predating the column expire without a backfill. `expires_at` is honoured as a per-row override when something sets it.

`now` is passed in rather than read from the clock so the policy is testable without sleeping. Both sides go through SQLite’s `datetime()` because `completed_at` is written as `CURRENT_TIMESTAMP` (`YYYY-MM-DD HH:MM:SS`) while callers pass RFC-3339 (`…T…+00:00`) — a raw string comparison between the two formats is wrong, since `' ' < 'T'`.

Returns the number of entries reclaimed.

TRACES: UR-071 | DR-127 | UT-120

Source

#### pub async fn evict_lru_async<S: DatabaseService>( &self, db_service: &Arc<S>, user_id: &str, space_needed: u64, ) -> Result<u64, String>

Evict least recently used items to make space (async version).

The space-pressure half of the temporary tier; [`reclaim_expired_async`] is the time-based half.

## Trait Implementations§

Source §

### impl Clone for SmartCache

Source §

#### fn clone(&self) -> SmartCache

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

## Auto Trait Implementations§

§

### impl Freeze for SmartCache

§

### impl RefUnwindSafe for SmartCache

§

### impl Send for SmartCache

§

### impl Sync for SmartCache

§

### impl Unpin for SmartCache

§

### impl UnsafeUnpin for SmartCache

§

### impl UnwindSafe for SmartCache

## 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> 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> From<T> for T

Source §

#### fn from(t: T) -> T

Returns the argument unchanged.

§

### impl<T> Instrument for T

§

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

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

#### 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.

§

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

§

#### fn and<P, B, E>(self, other: P) -> And<T, P>
where T: 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
§

#### fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: 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> 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.
§

### impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

#### fn vzip(self) -> V

§

### impl<T> WithSubscriber for T

§

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

#### fn with_current_subscriber(self) -> WithDispatch<Self>

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