-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…ount Feat/#496 post view count
- Loading branch information
Showing
25 changed files
with
372 additions
and
33 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
15 changes: 15 additions & 0 deletions
15
src/main/java/com/jeju/nanaland/domain/common/repository/PostRepository.java
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,15 @@ | ||
package com.jeju.nanaland.domain.common.repository; | ||
|
||
import com.jeju.nanaland.domain.common.entity.Post; | ||
import io.lettuce.core.dynamic.annotation.Param; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Modifying; | ||
import org.springframework.data.jpa.repository.Query; | ||
|
||
|
||
public interface PostRepository extends JpaRepository<Post, Long> { | ||
|
||
@Modifying | ||
@Query("UPDATE Post p SET p.viewCount = p.viewCount + 1 WHERE p.id = :postId") | ||
void increaseViewCount(@Param("postId") Long postId); | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/jeju/nanaland/domain/common/service/PostViewCountService.java
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,33 @@ | ||
package com.jeju.nanaland.domain.common.service; | ||
|
||
import com.jeju.nanaland.domain.common.repository.PostRepository; | ||
import com.jeju.nanaland.global.util.RedisUtil; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class PostViewCountService { | ||
|
||
private final PostRepository postRepository; | ||
private final RedisUtil redisUtil; | ||
|
||
@Transactional | ||
public void increaseViewCount(Long postId, Long memberId) { | ||
|
||
String redisKey = "post_viewed_" + memberId + "_" + postId; | ||
|
||
// 30분 -> 1800초 | ||
long cacheDurationSeconds = 1800L; | ||
|
||
// 30분 이내에 조회한 기록이 없으면 | ||
if (redisUtil.getValue(redisKey) == null) { | ||
// 조회수 증가 | ||
postRepository.increaseViewCount(postId); | ||
// 레디스에 등록 | ||
redisUtil.setExpiringValue(redisKey, "viewed", cacheDurationSeconds); | ||
} | ||
|
||
} | ||
} |
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
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
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.