Overview
TheCacheManager class provides a lightweight caching system for La Urban’s radio data with intelligent change detection. It tracks song changes and live state transitions to minimize unnecessary UI updates.
Features
- 15-second update interval for main data
- Song change detection by ID comparison
- Live state transition tracking
- Minimal memory footprint
Constructor
Properties
Cached radio data from La Urban API
Timestamp (in milliseconds) of the last cache update
ID of the currently cached song
Current live streaming state
Update interval in milliseconds (15 seconds)
Methods
needsUpdate()
Checks if the cache needs to be refreshed based on the update interval.Boolean - true if cache is stale or empty, false otherwise
Example:
- Returns
trueif no data is cached (laurbanData === null) - Returns
trueif time since last check >=updateInterval(15 seconds) - Returns
falseif cache is still fresh
hasChanged()
Compares new data with cached data to detect significant changes.New radio data object to compare against cached data
Boolean - true if song changed or live state changed, false otherwise
Example:
Song Change Detection
Song Change Detection
Compares Logs:
now_playing.id between cached and new data:🎵 Detectado cambio de canciónLive State Change Detection
Live State Change Detection
Compares Logs:
live.is_live between cached and new data:🎥 Detectado cambio en estado de live- No data is currently cached
- New data is null/undefined
- Song ID has changed
- Live streaming state has changed
update()
Updates the cache with new data and refreshes tracking properties.Radio data object to store in cache
- Stores the entire data object in
laurbanData - Updates
lastCheckto current timestamp - Caches
currentSongIdfromnewData.now_playing?.id - Caches
currentLiveStatefromnewData.live?.is_live(defaults tofalse)
get()
Retrieves the currently cached data.Object|null - The cached radio data or null if no data is cached
Example:
Usage Patterns
Expected Data Structure
The CacheManager expects radio data with the following structure:Logger Integration
The CacheManager includes a fallback logger implementation if the global logger is not available:Performance Considerations
Update Interval Strategy
Update Interval Strategy
The 15-second update interval balances freshness with API load:
- Too frequent: Wastes bandwidth, increases server load
- Too slow: Users see stale information
- 15 seconds: Optimal for song change detection while being API-friendly
needsUpdate() to prevent unnecessary fetches.Change Detection Benefits
Change Detection Benefits
The
hasChanged() method prevents unnecessary DOM updates:- Only update UI when song actually changes
- Avoid re-rendering heavy components
- Reduce visual flicker and animations
- Improve battery life on mobile devices
