Курсовая: Вроде как доделано нормальное кэширование

This commit is contained in:
Amon 2023-12-25 15:56:59 +04:00
parent 87332767e7
commit 7b3fcfa792
6 changed files with 39 additions and 11 deletions

View File

@ -27,4 +27,7 @@ interface CategoryDao {
@Query("select * from category limit 1")
fun getFirst() : Flow<Category>
@Query("select * from category where id >= :loadKey limit :limit")
fun getByLoadKey(loadKey: Int, limit: Int) : Flow<List<Category>>
}

View File

@ -22,6 +22,9 @@ interface CommentDao {
@Query("select * from comment where comment.post_id = :postId ORDER BY id DESC")
fun getByPost(postId: Int) : PagingSource<Int, Comment>
@Query("delete from comment where post_id = :query")
fun deleteByQuery(query: String)
@Query("select * from comment where id >= :loadKey and post_id = :postId order by id desc limit :limit")
fun getByLoadKeyInitial(postId: Int, loadKey: Int, limit: Int) : Flow<List<Comment>>
@Query("select * from comment where id <= :loadKey and post_id = :postId order by id desc limit :limit")
fun getByLoadKey(postId: Int, loadKey: Int, limit: Int) : Flow<List<Comment>>
}

View File

@ -22,6 +22,6 @@ interface PostDao {
@Query("select * from post where post.category_id = :categoryId ORDER BY date DESC")
fun getByCategory(categoryId: String) : PagingSource<Int, Post>
@Query("delete from post where category_id = :query")
fun deleteByQuery(query: String)
@Query("select * from post where category_id = :categoryId and id >= :loadKey ORDER BY date DESC limit :limit")
fun getByLoadKey(categoryId: Int, loadKey: Int, limit: Int) : Flow<List<Post>>
}

View File

@ -8,6 +8,7 @@ import androidx.room.withTransaction
import com.example.dtf.api.ServerService
import com.example.dtf.db.AppDatabase
import com.example.dtf.models.Category
import kotlinx.coroutines.flow.first
import java.io.IOException
@OptIn(ExperimentalPagingApi::class)
@ -42,11 +43,14 @@ class CategoryMediator (
limit = state.config.pageSize
)
database.withTransaction {
val present = categoryDao.getByLoadKey(loadKey, state.config.pageSize).first()
if (loadType == LoadType.REFRESH) {
for (category in response.categories) {
categoryDao.delete(category)
for (category in present) {
if (category !in response.categories){
categoryDao.delete(category)
}
}
}

View File

@ -8,6 +8,7 @@ import androidx.room.withTransaction
import com.example.dtf.api.ServerService
import com.example.dtf.db.AppDatabase
import com.example.dtf.models.Comment
import kotlinx.coroutines.flow.first
import java.io.IOException
@OptIn(ExperimentalPagingApi::class)
@ -46,8 +47,18 @@ class CommentMediator (
database.withTransaction {
val present = if (loadKey == 0) {
commentDao.getByLoadKeyInitial(query.toInt(), loadKey, state.config.pageSize).first()
} else {
commentDao.getByLoadKey(query.toInt(), loadKey, state.config.pageSize).first()
}
if (loadType == LoadType.REFRESH) {
commentDao.deleteByQuery(query)
for (comment in present) {
if (comment !in response.comments) {
commentDao.delete(comment)
}
}
}
for (comment in response.comments) {

View File

@ -8,6 +8,7 @@ import androidx.room.withTransaction
import com.example.dtf.api.ServerService
import com.example.dtf.db.AppDatabase
import com.example.dtf.models.Post
import kotlinx.coroutines.flow.first
import java.io.IOException
@OptIn(ExperimentalPagingApi::class)
@ -46,12 +47,18 @@ class PostMediator (
database.withTransaction {
val present = postDao.getByLoadKey(query.toInt(), loadKey, state.config.pageSize).first()
if (loadType == LoadType.REFRESH) {
postDao.deleteByQuery(query)
for (post in present) {
if (post !in response.posts) {
postDao.delete(post)
}
}
}
for (comment in response.posts) {
postDao.insert(comment)
for (post in response.posts) {
postDao.insert(post)
}
}