api_cache
¶
API Cache Service - Specialized cache for external API responses.
This module provides a dedicated cache service for storing and retrieving external API responses (Spotify, Last.fm, etc.) with JSON persistence.
Key Features: - JSON-based persistence for API response data - Content-aware TTL management (eternal for successful responses, retry TTL for failures) - Integration with SmartCacheConfig for intelligent caching policies - Automatic cache invalidation when tracks are removed from library
ApiCacheService
¶
Specialized cache service for external API responses with JSON persistence.
Initialize API cache service.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
AppConfig
|
Typed application configuration |
required |
logger
|
Logger | None
|
Optional logger instance |
None
|
Source code in src/services/cache/api_cache.py
initialize
async
¶
Initialize API cache by loading data from disk.
Source code in src/services/cache/api_cache.py
get_cached_result
async
¶
Get cached API result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
artist
|
str
|
The artist name for cache key generation. |
required |
album
|
str
|
The album name for cache key generation. |
required |
source
|
str
|
API source (musicbrainz, discogs, etc.) |
required |
Returns:
| Type | Description |
|---|---|
CachedApiResult | None
|
Cached API result if found and valid, None otherwise |
Source code in src/services/cache/api_cache.py
set_cached_result
async
¶
Store API result in cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
artist
|
str
|
The artist name for cache key generation. |
required |
album
|
str
|
The album name for cache key generation. |
required |
source
|
str
|
API source identifier. |
required |
success
|
bool
|
Whether the API call was successful. |
required |
data
|
dict[str, Any] | None
|
API response data (if successful). |
None
|
metadata
|
dict[str, Any] | None
|
Additional metadata to store. |
None
|
Source code in src/services/cache/api_cache.py
invalidate_for_album
async
¶
Invalidate all API cache entries for specific album.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
artist
|
str
|
The artist name for cache key generation. |
required |
album
|
str
|
The album name for cache key generation. |
required |
Source code in src/services/cache/api_cache.py
invalidate_all
async
¶
Clear all API cache entries.
cleanup_expired
async
¶
Remove expired entries from API cache.
Returns:
| Type | Description |
|---|---|
int
|
Number of entries removed |
Source code in src/services/cache/api_cache.py
save_to_disk
async
¶
Save API cache to JSON file.
Source code in src/services/cache/api_cache.py
emit_track_removed
¶
Emit track removed event for cache invalidation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
track_id
|
str
|
Unique track identifier |
required |
artist
|
str
|
Artist name |
required |
album
|
str
|
Album name |
required |
Source code in src/services/cache/api_cache.py
emit_track_modified
¶
Emit track modified event for cache invalidation.
Called when user changes artist/album in Music.app. Invalidates cache for the OLD artist/album combination since it's now stale.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
track_id
|
str
|
Unique track identifier |
required |
old_artist
|
str
|
Previous artist name (before user edit) |
required |
old_album
|
str
|
Previous album name (before user edit) |
required |
Source code in src/services/cache/api_cache.py
shutdown
async
¶
Gracefully shutdown background tasks.
Waits for all pending background tasks to complete, handling any exceptions that may occur during task execution.
Source code in src/services/cache/api_cache.py
get_stats
¶
Get API cache statistics.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing cache statistics |