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

fix: 방문 데이터 최적화 #338

Merged
merged 7 commits into from
Oct 8, 2024
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
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ include 'smeem-output-notification:firebase'
include 'smeem-output-oauth'
include 'smeem-output-oauth:apple'
include 'smeem-output-oauth:kakao'
include 'smeem-output-cache'
include 'smeem-output-cache:redis'
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.smeem.application.domain.badge.Badge;
import com.smeem.application.domain.trainingtime.DayType;
import com.smeem.application.domain.trainingtime.TrainingTime;
import com.smeem.application.domain.visit.Visit;
import com.smeem.application.port.input.MemberUseCase;
import com.smeem.application.port.input.dto.request.member.UpdateMemberHasPushAlarmRequest;
import com.smeem.application.port.input.dto.request.member.UpdateMemberRequest;
Expand All @@ -13,6 +12,7 @@
import com.smeem.application.port.input.dto.response.member.UpdateMemberResponse;
import com.smeem.application.port.input.dto.response.member.UsernameDuplicatedResponse;
import com.smeem.application.port.input.dto.response.plan.RetrieveMemberPlanResponse;
import com.smeem.application.port.output.cache.CachePort;
import com.smeem.application.port.output.persistence.*;
import com.smeem.common.logger.HookLogger;
import com.smeem.common.logger.LoggingMessage;
Expand All @@ -21,6 +21,8 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;

@Service
Expand All @@ -31,10 +33,10 @@ public class MemberService implements MemberUseCase {
private final TrainingTimePort trainingTimePort;
private final BadgePort badgePort;
private final DiaryPort diaryPort;
private final VisitPort visitPort;
private final GoalPort goalPort;
private final PlanPort planPort;
private final HookLogger hookLogger;
private final CachePort cachePort;

@Transactional
public UpdateMemberResponse updateMember(long memberId, UpdateMemberRequest request) {
Expand Down Expand Up @@ -89,12 +91,16 @@ public RetrievePerformanceResponse retrieveMemberPerformance(long memberId) {
}

@Transactional
public void checkAttendance(long memberId) {
val foundMember = memberPort.findById(memberId);
if (!visitPort.isExistByMemberAndToday(foundMember.getId())) {
public void visit(long memberId) {
Member foundMember = memberPort.findById(memberId);
String today = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
String key = "visit:" + today;

if (!cachePort.getBit(key, foundMember.getId())) {
System.out.println("test");
foundMember.visit();
memberPort.update(foundMember);
visitPort.visit(new Visit(foundMember.getId()));
cachePort.setBit(key, foundMember.getId(), true);
}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public interface MemberUseCase {
UsernameDuplicatedResponse checkUsernameDuplicated(String username);
void updateMemberHasPush(long memberId, UpdateMemberHasPushAlarmRequest request);
RetrievePerformanceResponse retrieveMemberPerformance(long memberId);
void checkAttendance(long memberId);
void visit(long memberId);
void updatePlan(long memberId, UpdateMemberPlanRequest request);
RetrieveMemberPlanResponse retrieveMemberPlan(long memberId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.smeem.application.port.output.cache;

public interface CachePort {
void setBit(String key, long offset, boolean value);
boolean getBit(String key, long offset);
}

This file was deleted.

2 changes: 2 additions & 0 deletions smeem-bootstrap/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ dependencies {
implementation project(':smeem-output-oauth')
implementation project(':smeem-output-oauth:apple')
implementation project(':smeem-output-oauth:kakao')
implementation project(':smeem-output-cache')
implementation project(':smeem-output-cache:redis')

implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
Expand Down
1 change: 1 addition & 0 deletions smeem-bootstrap/src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ spring:
- classpath:smeem-config/application-dev.yml
- classpath:notification-config/application-dev.yml
- classpath:common-config/application-dev.yml
- classpath:redis-config/application-dev.yml

logging.level:
org.hibernate.SQL: debug
1 change: 1 addition & 0 deletions smeem-bootstrap/src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ spring:
- classpath:smeem-config/application-local.yml
- classpath:notification-config/application-local.yml
- classpath:common-config/application-local.yml
- classpath:redis-config/application-local.yml

logging.level:
org.hibernate.SQL: debug
1 change: 1 addition & 0 deletions smeem-bootstrap/src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ spring:
- classpath:smeem-config/application-prod.yml
- classpath:notification-config/application-prod.yml
- classpath:common-config/application-prod.yml
- classpath:redis-config/application-prod.yml

logging.level:
org.hibernate.SQL: debug
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ public SmeemResponse<RetrievePerformanceResponse> retrievePerformance(Principal

@ResponseStatus(HttpStatus.OK)
@PatchMapping("/visit")
public SmeemResponse<?> checkAttendance(Principal principal) {
public SmeemResponse<?> visit(Principal principal) {
val memberId = smeemConverter.toMemberId(principal);
memberUseCase.checkAttendance(memberId);
memberUseCase.visit(memberId);
return SmeemResponse.of(SmeemMessage.UPDATE_MEMBER);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,5 @@ SmeemResponse<?> updateMemberHasPushAlarm(
responseCode = "200",
description = "OK success")
})
SmeemResponse<?> checkAttendance(@Parameter(hidden = true) Principal principal);
SmeemResponse<?> visit(@Parameter(hidden = true) Principal principal);
}
27 changes: 27 additions & 0 deletions smeem-output-cache/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
project(':smeem-output-cache') {
dependencies {
}
}

project(':smeem-output-cache:redis') {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
}
}

allprojects {
dependencies {
implementation project(':smeem-common')
implementation project(':smeem-application')

implementation 'org.springframework.boot:spring-boot-starter-web'
}

tasks.bootJar {
enabled = false
}

tasks.jar {
enabled = true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.smeem.output.cache.redis.adapter;

import com.smeem.application.port.output.cache.CachePort;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

@Configuration
@RequiredArgsConstructor
public class CacheAdapter implements CachePort {
private final RedisTemplate<String, String> redisTemplate;

@Override
public void setBit(String key, long offset, boolean value) {
redisTemplate.opsForValue().setBit(key, offset, value);
}

@Override
public boolean getBit(String key, long offset) {
return Boolean.TRUE.equals(redisTemplate.opsForValue().getBit(key, offset));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.smeem.output.cache.redis.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;

@Configuration
@EnableRedisRepositories
@EnableCaching
@EnableConfigurationProperties(RedisProperties.class)
@RequiredArgsConstructor
public class RedisConfig {

@Bean
public LettuceConnectionFactory lettuceConnectionFactory(RedisProperties redisProperties) {
RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration();
redisConfig.setHostName(redisProperties.host());
redisConfig.setPort(redisProperties.port());
redisConfig.setPassword(RedisPassword.of(redisProperties.password()));

LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder()
.useSsl()
.build();

return new LettuceConnectionFactory(redisConfig, clientConfig);
}

@Bean
public <T> RedisTemplate<String, T> redisTemplate(
RedisConnectionFactory redisConnectionFactory,
ObjectMapper objectMapper
) {
RedisTemplate<String, T> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer(objectMapper));
return redisTemplate;
}

@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofDays(7))
.serializeKeysWith(
RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
.serializeValuesWith(
RedisSerializationContext.SerializationPair.fromSerializer(
new GenericJackson2JsonRedisSerializer()));

return RedisCacheManager.builder(redisConnectionFactory)
.cacheDefaults(redisCacheConfiguration)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.smeem.output.cache.redis.config;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "redis")
public record RedisProperties(
String host,
int port,
String password
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
redis:
host: ${REDIS_HOST}
port: 6379
password: ${REDIS_PASSWORD}
ssl: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
redis:
host: ${REDIS_HOST}
port: 6379
password: ${REDIS_PASSWORD}
ssl: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
redis:
host: ${REDIS_HOST}
port: 6379
password: ${REDIS_PASSWORD}
ssl: true
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public class MemberAdapter implements MemberPort {
private final DeletedDiaryRepository deletedDiaryRepository;
private final DiaryRepository diaryRepository;
private final TrainingTimeRepository trainingTimeRepository;
private final VisitRepository visitRepository;
private final WithdrawRepository withdrawRepository;

@Override
Expand Down Expand Up @@ -55,7 +54,6 @@ public void deleteById(long id) {
deletedDiaryRepository.deleteByMemberId(id);
diaryRepository.deleteByMemberId(id);
trainingTimeRepository.deleteByMemberId(id);
visitRepository.deleteByMemberId(id);
memberRepository.deleteById(id);
}

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Loading
Loading