Last active
January 13, 2026 15:13
-
-
Save madnotdead/9b1741a58a88a0e4e1a2f2f3f5236f6b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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