Android's local storage (SharedPreferences, databases) lacks built-in TTL support unlike Redis. This guide shows how to implement automatic cache expiration using WorkManager's OneTimeWorkRequest with a configurable delay. The approach uses a generic SaveWithTTL abstract class, a Hilt-injected CoroutineWorker that deletes data after the delay, and enqueueUniqueWork with REPLACE policy to reset TTL on updates. It also covers reading HTTP Cache-Control max-age headers to let the backend dynamically control cache lifetimes without app updates.
Sort: