From 3797bb321b6b285cc69d84f22e16a21fe617f5c0 Mon Sep 17 00:00:00 2001 From: HongGeun Date: Fri, 23 Jun 2023 20:34:32 +0900 Subject: [PATCH] =?UTF-8?q?[modify]=20redis=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../springboard/domain/comment/service/CommentService.java | 3 ++- .../springboard/domain/post/repository/PostRedisDao.java | 6 +++++- .../springboard/domain/post/service/PostService.java | 3 ++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/spring-board/src/main/java/com/example/springboard/domain/comment/service/CommentService.java b/spring-board/src/main/java/com/example/springboard/domain/comment/service/CommentService.java index 0f19636..2b5ba99 100644 --- a/spring-board/src/main/java/com/example/springboard/domain/comment/service/CommentService.java +++ b/spring-board/src/main/java/com/example/springboard/domain/comment/service/CommentService.java @@ -33,7 +33,8 @@ public List getComments(Long postId, Integer page, Integer count) public void addComment(Long postId, Long memberId, String content, Float score) { Comment comment = Comment.createComment(memberId, postId, content, score); - if(commentRepository.insertComment(comment) == false) { + boolean result = commentRepository.insertComment(comment); + if(!result) { throw new RuntimeException("삽입에 실패 했습니다."); } } diff --git a/spring-board/src/main/java/com/example/springboard/domain/post/repository/PostRedisDao.java b/spring-board/src/main/java/com/example/springboard/domain/post/repository/PostRedisDao.java index a4ec194..7c62fac 100644 --- a/spring-board/src/main/java/com/example/springboard/domain/post/repository/PostRedisDao.java +++ b/spring-board/src/main/java/com/example/springboard/domain/post/repository/PostRedisDao.java @@ -35,11 +35,15 @@ public int increaseView(Long postId) { if(valueOperations.get(key) == null) { valueOperations.set(generateKey(postId, KeyType.REAL), postRepository.getPostById(postId).getViews()); } - valueOperations.set(generateKey(postId, KeyType.SHADOW) , "", Duration.ofSeconds(1)); + resetShadowKey(postId, valueOperations); return valueOperations.increment(key).intValue(); } + private void resetShadowKey(Long postId, ValueOperations valueOperations) { + valueOperations.set(generateKey(postId, KeyType.SHADOW) , "", Duration.ofMinutes(5)); + } + public int getViews(Long postId) { String key = generateKey(postId, KeyType.REAL); return (int) redisTemplate.opsForValue().get(key); diff --git a/spring-board/src/main/java/com/example/springboard/domain/post/service/PostService.java b/spring-board/src/main/java/com/example/springboard/domain/post/service/PostService.java index f4e54a6..2d862d1 100644 --- a/spring-board/src/main/java/com/example/springboard/domain/post/service/PostService.java +++ b/spring-board/src/main/java/com/example/springboard/domain/post/service/PostService.java @@ -53,7 +53,8 @@ public void editPost(Long postId, Long memberId, String title, String content) { public void deletePost(Long postId, Long memberId) { checkAuth(postId, memberId); - postRedisDao.deleteAndGet(postId); + int views = postRedisDao.deleteAndGet(postId); + postRepository.updateViews(postId, views); postRepository.deletePost(postId); }