-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from RajashekarRaju/implement-caching-mechanism
Implement simple cache mechanism with LruCache
- Loading branch information
Showing
17 changed files
with
239 additions
and
43 deletions.
There are no files selected for viewing
This file contains 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
9 changes: 9 additions & 0 deletions
9
app/src/main/java/com/developersbreach/composeactors/core/cache/CacheKey.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.developersbreach.composeactors.core.cache | ||
|
||
object CacheKey { | ||
fun forPerson(id: Int): String = "${PERSON}_$id" | ||
fun forMovie(id: String): String = "${MOVIE}_$id" | ||
|
||
private const val PERSON = "person_" | ||
private const val MOVIE = "movie_" | ||
} |
31 changes: 31 additions & 0 deletions
31
app/src/main/java/com/developersbreach/composeactors/core/cache/CacheManager.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.developersbreach.composeactors.core.cache | ||
|
||
import android.util.LruCache | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class CacheManager @Inject constructor( | ||
cacheSize: Int = 100 | ||
) { | ||
private val cache = object : LruCache<String, Any>(cacheSize) { | ||
override fun sizeOf(key: String, value: Any): Int { | ||
return 1 | ||
} | ||
} | ||
|
||
@Synchronized | ||
fun <T> get(key: String): T? { | ||
return cache[key] as? T | ||
} | ||
|
||
@Synchronized | ||
fun <T> put(key: String, value: T) { | ||
cache.put(key, value) | ||
} | ||
|
||
@Synchronized | ||
fun remove(key: String) { | ||
cache.remove(key) | ||
} | ||
} |
This file contains 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
17 changes: 17 additions & 0 deletions
17
app/src/main/java/com/developersbreach/composeactors/core/database/dao/PersonDetailsDao.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.developersbreach.composeactors.core.database.dao | ||
|
||
import androidx.room.Dao | ||
import androidx.room.Insert | ||
import androidx.room.OnConflictStrategy | ||
import androidx.room.Query | ||
import com.developersbreach.composeactors.core.database.entity.PersonDetailEntity | ||
|
||
@Dao | ||
interface PersonDetailsDao { | ||
|
||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
suspend fun insertPersonDetail(personDetail: PersonDetailEntity) | ||
|
||
@Query("SELECT * FROM person_detail_table WHERE column_person_detail_id = :personId") | ||
suspend fun getPersonDetail(personId: Int): PersonDetailEntity? | ||
} |
This file contains 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
This file contains 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
46 changes: 46 additions & 0 deletions
46
...c/main/java/com/developersbreach/composeactors/core/database/entity/PersonDetailEntity.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.developersbreach.composeactors.core.database.entity | ||
|
||
import androidx.compose.runtime.Stable | ||
import androidx.room.ColumnInfo | ||
import androidx.room.Entity | ||
import androidx.room.PrimaryKey | ||
import com.developersbreach.composeactors.data.person.model.PersonDetail | ||
|
||
@Entity(tableName = "person_detail_table") | ||
data class PersonDetailEntity( | ||
|
||
@Stable | ||
@PrimaryKey | ||
@ColumnInfo(name = "column_person_detail_id") | ||
val personId: Int, | ||
|
||
@ColumnInfo(name = "column_person_detail_name") | ||
val personName: String, | ||
|
||
@ColumnInfo(name = "column_person_detail_profile_path") | ||
val profilePath: String?, | ||
|
||
@ColumnInfo(name = "column_person_detail_biography") | ||
val biography: String, | ||
|
||
@ColumnInfo(name = "column_person_detail_date_of_birth") | ||
val dateOfBirth: String?, | ||
|
||
@ColumnInfo(name = "column_person_detail_place_of_birth") | ||
val placeOfBirth: String?, | ||
|
||
@ColumnInfo(name = "column_person_detail_popularity") | ||
val popularity: Double | ||
) | ||
|
||
fun PersonDetailEntity.toPersonDetail(): PersonDetail { | ||
return PersonDetail( | ||
personId = personId, | ||
personName = personName, | ||
profilePath = profilePath, | ||
biography = biography, | ||
dateOfBirth = dateOfBirth, | ||
placeOfBirth = placeOfBirth, | ||
popularity = popularity | ||
) | ||
} |
This file contains 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
This file contains 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
12 changes: 11 additions & 1 deletion
12
app/src/main/java/com/developersbreach/composeactors/data/person/model/FavoritePerson.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,20 @@ | ||
package com.developersbreach.composeactors.data.person.model | ||
|
||
import androidx.compose.runtime.Stable | ||
import com.developersbreach.composeactors.core.database.entity.FavoritePersonsEntity | ||
|
||
data class FavoritePerson( | ||
@Stable val personId: Int, | ||
val personName: String, | ||
val profileUrl: String, | ||
val placeOfBirth: String? | ||
) | ||
) | ||
|
||
fun FavoritePerson.FavoritePersonsEntity(): FavoritePersonsEntity { | ||
return FavoritePersonsEntity( | ||
personId = this.personId, | ||
personName = this.personName, | ||
personProfileUrl = this.profileUrl, | ||
personPlaceOfBirth = this.placeOfBirth | ||
) | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.