Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/main/java/com/jiwon/mylog/domain/post/entity/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public class Post extends BaseEntity {
private List<PostImage> images = new ArrayList<>();

@Builder.Default
@OneToMany(mappedBy = "post")
@OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true)
private List<PostTag> postTags = new ArrayList<>();

@Builder.Default
Expand Down Expand Up @@ -112,12 +112,12 @@ public void update(PostRequest request, Category category, List<Tag> tags) {
}

private void updateTags(List<Tag> tags) {
this.postTags.clear();
this.postTags.addAll(
tags.stream()
.map(tag -> PostTag.createPostTag(this, tag))
.toList()
);
this.postTags.removeIf(pt -> !tags.contains(pt.getTag()));
for (Tag tag : tags) {
if (postTags.stream().noneMatch(pt -> pt.getTag().equals(tag))) {
postTags.add(PostTag.createPostTag(this, tag));
}
}
}

public void delete() {
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/jiwon/mylog/domain/tag/entity/PostTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,21 @@
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Entity
@Table(
name = "post_tag",
uniqueConstraints = @UniqueConstraint(
name = "post_tag_uk",
columnNames = {"post_id", "tag_id"}
)
)
public class PostTag {

@Id
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/jiwon/mylog/global/redis/RedisUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.jiwon.mylog.global.redis;

import com.jiwon.mylog.global.redis.key.RedisKey;
import java.time.Duration;
import java.time.LocalDate;
import java.time.format.DateTimeParseException;
Expand Down Expand Up @@ -111,11 +112,11 @@ public void addPostViewUser(String key, String value) {
redisTemplate.expire(key, Duration.ofHours(12));
}

public Map<Long, Integer> getAllPostView(String keyPrefix) {
Set<String> keys = redisTemplate.keys(keyPrefix);
public Map<Long, Integer> getAllPostView(RedisKey keyPattern) {
Set<String> keys = redisTemplate.keys(keyPattern.getPrefix() + "*");
return keys.stream()
.collect(Collectors.toMap(
key -> Long.parseLong(key.replace(keyPrefix, "")),
key -> keyPattern.getUserIdentifier(key),
key -> getInt(key, 0)
));
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/jiwon/mylog/global/redis/key/RedisKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ public String getPrefix() {
return prefix;
}

public Long getUserIdentifier(String redisKey) {
int index = redisKey.lastIndexOf(':');
String keyString = redisKey.substring(index + 1);
return Long.parseLong(keyString);
}

public Duration getTtl() {
return ttl;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ public class PostViewScheduler {
private final PostRepository postRepository;
private final RedisUtil redisUtil;

@Scheduled(fixedRate = 10 * 60 * 1000L)
@Scheduled(fixedRate = 30 * 60 * 1000L)
@Transactional
public void syncPostViewToDB() {
Map<Long, Integer> postCounts = redisUtil.getAllPostView(RedisKey.VIEW_COUNT_KEY.getPrefix());
Map<Long, Integer> postCounts = redisUtil.getAllPostView(RedisKey.VIEW_COUNT_KEY);
log.info("PostViewScheduler 시작: {}개", postCounts.size());

postCounts.forEach((postId, view) -> {
try {
log.debug("postId: {}, view: {}", postId, view);
postRepository.updatePostView(postId, view);
} catch (Exception e) {
log.error("조회수 동기화 실패, 게시글 {}: {}", postId, e.getMessage());
log.error("조회수 동기화 실패, postId={}: {}", postId, e.getMessage());
}
});
}
Expand Down