Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jun-labs/async-paging

πŸ“— 비동기 νŽ˜μ΄μ§• 예제 μ½”λ“œ.
https://github.com/jun-labs/async-paging

async cursor limit offset page pagination paging sync

Last synced: about 15 hours ago
JSON representation

πŸ“— 비동기 νŽ˜μ΄μ§• 예제 μ½”λ“œ.

Awesome Lists containing this project

README

        

# Async Paging

νŽ˜μ΄μ§• **`카운트`**, **`데이터 페치`** 쿼리 뢄리 예제.





## πŸ’» ν”„λ‘œκ·Έλž¨ μ‹€ν–‰

λΉŒλ“œ ν›„, ν”„λ‘œμ νŠΈ μ‹€ν–‰ ν•©λ‹ˆλ‹€. μ• ν”Œλ¦¬μΌ€μ΄μ…˜ μ‹€ν–‰ ν›„, 데이터 μ΄ˆκΈ°ν™” μž‘μ—…μ΄ 있기 λ•Œλ¬Έμ— 잠깐 λŒ€κΈ°ν•©λ‹ˆλ‹€.

```shell
./gradlew build
```

```shell
java -jar build/libs/paging-async-0.0.1.jar
```





## πŸ“š Content

νŽ˜μ΄μ§•μ—μ„œ **`카운트`** 와 **`데이터 페칭`** 두 쿼리가 순차적으둜 μ‹€ν–‰λ©λ‹ˆλ‹€. 이λ₯Ό λΆ„λ¦¬ν•œ ν›„, λΉ„λ™κΈ°λ‘œ μ‘°νšŒν•˜λ©΄ 쑰회 μ„±λŠ₯을 쑰금 더 ν–₯μƒμ‹œν‚¬ 수 μžˆμŠ΅λ‹ˆλ‹€.

```kotlin
@Repository
class UserEntityReadRepository(
private val queryFactory: JPAQueryFactory,
) : UserReadRepository {

companion object {
private val totalCountExpression = numberTemplate(Long::class.java, "count(1)")
}

override suspend fun findUsers(
page: Int,
size: Int,
): Pair> = coroutineScope {
val totalCount = async {
queryFactory.select(totalCountExpression)
.from(user)
.fetchOne() ?: 0L
}

val findUsers = async {
queryFactory.selectFrom(user)
.offset((page) * size.toLong())
.limit(10)
.fetch()
}
Pair(totalCount.await(), findUsers.await())
}
}
```

> 단, μ΄λŠ” offset λ°©μ‹μ˜ ν•œκ³„λŠ” λ²—μ–΄λ‚˜μ§€ λͺ»ν•˜λ―€λ‘œ, 데이터가 λ§Žμ„ 수둝 속도가 λŠλ €μ§‘λ‹ˆλ‹€.