Skip to content

Instantly share code, notes, and snippets.

@madnotdead
Last active January 13, 2026 15:13
Show Gist options
  • Select an option

  • Save madnotdead/9b1741a58a88a0e4e1a2f2f3f5236f6b to your computer and use it in GitHub Desktop.

Select an option

Save madnotdead/9b1741a58a88a0e4e1a2f2f3f5236f6b to your computer and use it in GitHub Desktop.
data class User(val id:String, val name:String, val lastName: String, val ttl: Long)
interface UserRepository {
suspend fun get(id: String) : User
}
class LocalDataSource {
val ttlExpiration = 5_000L
//concurent map?
private val users = mutableMapOf<String, User>()
suspend fun getUser(id: String): User? {
return users[id]?.ttl - datenow().tomillis() < ttlExpiration ?: null : users[id]
}
}
class RemoteDataSource(val apiClient: UserAPI) {
suspend fun getUser(id: String): User? {
withContext(Dispacher.IO) {
apiClient.getClientById(id)
}
}
}
class UserRepositoryImpl(val localDatasourte: LocalDataSource, val remoteDataSource: RemoteDataSource) : UserRepository {
override suspend fun get(id: String): User {
withContext(Dispacher.IO) {
//inMemory
val currentUser = localDatasourte.getUser(id)
//inNetwork
val expired = syste currentUser.ttl
return@withContext localDatasourte.getUser(id)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment