forked from YAPP-Github/24th-Web-Team-1-BE
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8d40bfa
commit a18924c
Showing
6 changed files
with
156 additions
and
89 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
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
50 changes: 50 additions & 0 deletions
50
api/src/main/kotlin/com/few/api/domain/article/usecase/ArticleViewRecordsService.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,50 @@ | ||
package com.few.api.domain.article.usecase | ||
|
||
import com.few.api.config.ApiLocalCacheConfig.Companion.LOCAL_CM | ||
import com.few.api.config.ApiLocalCacheConfig.Companion.SELECT_ARTICLE_VIEWS_RECORD_SERVICE_CACHE | ||
import com.few.api.domain.article.repo.ArticleViewCountDao | ||
import com.few.api.domain.article.repo.query.SelectArticlesOrderByViewsQuery | ||
import com.few.api.domain.article.repo.query.SelectRankByViewsQuery | ||
import com.few.api.domain.article.repo.record.SelectArticleViewsRecord | ||
import com.few.api.domain.common.vo.CategoryType | ||
import org.springframework.cache.annotation.Cacheable | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class ArticleViewRecordsService( | ||
private val articleViewCountDao: ArticleViewCountDao, | ||
) { | ||
@Cacheable( | ||
keyGenerator = "articleViewRecordsServiceKeyGenerator", | ||
cacheManager = LOCAL_CM, | ||
cacheNames = [SELECT_ARTICLE_VIEWS_RECORD_SERVICE_CACHE], | ||
) | ||
fun execute( | ||
preArticleId: Long, | ||
categoryCd: Byte, | ||
): MutableList<SelectArticleViewsRecord> { | ||
/** | ||
* 아티클 조회수 테이블에서 마지막 읽은 아티클 아이디, 카테고리를 기반으로 Offset(테이블 row 순위)을 구함 | ||
*/ | ||
val offset = | ||
if (preArticleId <= 0) { | ||
0L | ||
} else { | ||
articleViewCountDao.selectRankByViews( | ||
SelectRankByViewsQuery(preArticleId), | ||
) ?: 0 | ||
} | ||
|
||
/** | ||
* 구한 Offset을 기준으로 이번 스크롤에서 보여줄 아티클 11개를 뽑아옴 | ||
* 카테고리 별, 조회수 순 11개. 조회수가 같을 경우 최신 아티클이 우선순위를 가짐 | ||
*/ | ||
return articleViewCountDao | ||
.selectArticlesOrderByViews( | ||
SelectArticlesOrderByViewsQuery( | ||
offset, | ||
CategoryType.fromCode(categoryCd) ?: CategoryType.All, | ||
), | ||
).toMutableList() | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...c/main/kotlin/com/few/api/domain/article/usecase/ArticleViewRecordsServiceKeyGenerator.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,18 @@ | ||
package com.few.api.domain.article.usecase | ||
|
||
import org.springframework.cache.interceptor.KeyGenerator | ||
import org.springframework.stereotype.Service | ||
import java.lang.reflect.Method | ||
|
||
@Service | ||
class ArticleViewRecordsServiceKeyGenerator : KeyGenerator { | ||
override fun generate( | ||
target: Any, | ||
method: Method, | ||
vararg params: Any?, | ||
): Any { | ||
val preArticleId = params[0] as Long | ||
val categoryCd = params[1] as Byte | ||
return "$preArticleId ::$categoryCd" | ||
} | ||
} |
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