Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔀 :: (#132) 게시글 엔티티 수정 #133

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ public class PublicFeed extends BaseTimeEntity {
@ColumnDefault("0")
private Integer likeCount;

@NotNull
@ColumnDefault("0")
private Integer viewCount;
public void addViewCounts(Integer views) {
viewCount += views;
}

@Builder
public PublicFeed(String title, String content, Integer likeCount, Feed feed) {
this.title = title;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.example.moizaspringserver.domain.feed.facade;

import com.example.moizaspringserver.domain.feed.entity.PublicFeed;
import com.example.moizaspringserver.domain.feed.exception.FeedNotFoundException;
import com.example.moizaspringserver.domain.feed.properties.ViewCountProperties;
import com.example.moizaspringserver.domain.feed.respository.PublicFeedRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@RequiredArgsConstructor
@Component
public class ViewCountFacade {

private final ViewCountProperties viewCountProperties;
private final PublicFeedRepository publicFeedRepository;
private final Map<Integer, Integer> viewCounts = new ConcurrentHashMap<>();

@Transactional(readOnly = true)
public Integer getViewCountOf(Integer feedId) {
return publicFeedRepository.findById(feedId)
.orElseThrow(() -> FeedNotFoundException.EXCEPTION)
.getViewCount();
}

@Transactional
public void flushViewCount(Integer feedId) {
PublicFeed feed = publicFeedRepository.findById(feedId)
.orElseThrow(() -> FeedNotFoundException.EXCEPTION);

feed.addViewCounts(viewCounts.get(feedId));
viewCounts.remove(feedId);
}

@Transactional
public void addViewCountOf(Integer feedId) {
viewCounts.computeIfAbsent(feedId, key -> viewCounts.put(key, 0));

viewCounts.put(feedId, viewCounts.get(feedId));

if(viewCounts.get(feedId) >= viewCountProperties.getCacheMaximum())
flushViewCount(feedId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.moizaspringserver.domain.feed.properties;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConstructorBinding;

@Getter
@ConstructorBinding
@AllArgsConstructor
@ConfigurationProperties(prefix = "feed.viewcount")
public class ViewCountProperties {
private final Integer cacheMaximum;
}

This file was deleted.

This file was deleted.

6 changes: 5 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ aws:
access-key : ${SES_ACCESS}
secret-key : ${SES_SECRET}
ses-source : ${SES_SOURCE}
aws-regions : ${AWS_REGIONS:ap-northeast-2}
aws-regions : ${AWS_REGIONS:ap-northeast-2}

feed:
viewcount:
cache-maximum: ${VIEWCOUNT_CACHE_MAXIMUM}