Skip to content

Commit

Permalink
redis 분리 및 api 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
dojinyou committed Jan 14, 2024
1 parent e421f54 commit 9b6cbe2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.mjucow.eatda.domain.store.service.query

import com.mjucow.eatda.domain.poplarstore.service.PopularStoreCommandService
import com.mjucow.eatda.domain.store.service.query.dto.StoreDetailDto
import com.mjucow.eatda.domain.store.service.query.dto.StoreDto
import com.mjucow.eatda.persistence.store.StoreRepository
Expand All @@ -11,7 +10,7 @@ import org.springframework.transaction.annotation.Transactional
@Transactional(readOnly = true)
class StoreQueryService(
val repository: StoreRepository,
val popularStoreCommandService: PopularStoreCommandService,
// val popularStoreCommandService: PopularStoreCommandService,
) {
fun findAllByCategoryAndCursor(cursor: Long? = null, categoryId: Long? = null, size: Int): List<StoreDto> {
return if (categoryId == null) {
Expand All @@ -33,7 +32,8 @@ class StoreQueryService(
fun findById(storeId: Long): StoreDetailDto {
val entity = repository.getReferenceById(storeId)
// FIXME(async): findById가 popular store command 과정에서 실패하지 않도록 비동기 호출 처리
popularStoreCommandService.setStore(storeId)
// TODO: Redis 이슈 해결 후 주석 제거
// popularStoreCommandService.setStore(storeId)
return StoreDetailDto.from(entity)
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,48 @@
package com.mjucow.eatda.presentation.store.popularstore

import com.mjucow.eatda.domain.poplarstore.service.PopularStoreCacheService
import com.mjucow.eatda.domain.poplarstore.service.PopularStoreQueryService
import com.mjucow.eatda.domain.poplarstore.service.dto.PopularStoreDtos
import com.mjucow.eatda.presentation.common.ApiResponse
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.bind.annotation.RestController
import java.time.Instant

@RequestMapping("/api/v1/stores/popular")
@RestController
class PopularStoreController(
private val cacheService: PopularStoreCacheService,
private val queryService: PopularStoreQueryService,
) : PopularStoreApiPresentation {
@GetMapping
@ResponseStatus(HttpStatus.OK)
override fun findAllPopularStore(): ApiResponse<PopularStoreDtos> {
return ApiResponse.success(queryService.getPopularStores())
}

@PostMapping("/cache/{storeId}")
@ResponseStatus(HttpStatus.NO_CONTENT)
fun setRedis(@PathVariable storeId: String) {
cacheService.setStore(Instant.now(), storeId.toLong())
}

@PostMapping("/cache/{storeId}")
@ResponseStatus(HttpStatus.OK)
fun getRedis(@PathVariable storeId: String): ApiResponse<RedisCache?> {
val key = cacheService.createSearchKey(Instant.now())
val popularStores = cacheService.getStoresSortByPopular(key)
if (popularStores.isEmpty()) {
return ApiResponse.success(null)
} else {
val e = popularStores[0]
return ApiResponse.success(RedisCache(e.storeId, e.count))
}
}

data class RedisCache(val storeId: Long, val count: Long)
}

0 comments on commit 9b6cbe2

Please sign in to comment.