Skip to content

Commit

Permalink
Merge pull request #68 from LetsCareer-A/feat/#67
Browse files Browse the repository at this point in the history
#67 [feat] 4.12 커리어 전체 조회 + redis 캐싱
  • Loading branch information
oosedus authored Sep 4, 2024
2 parents 219b315 + 4cf6297 commit b8e3bf1
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 4 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dependencies {

// redis
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'org.springframework.boot:spring-boot-starter-cache'

// s3
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,16 @@ public ApiResponse getCareers(@RequestHeader("userId") Long userId,
@RequestParam(value = "page", defaultValue = "1") int page,
@RequestParam(value = "size", defaultValue = "15") int size) {
try {
return SuccessResponse.success(SuccessCode.GET_CAREER_DETAIL_SUCCESS, careerService.getCareers(userId, page, size));
return SuccessResponse.success(SuccessCode.GET_CAREER_SUCCESS, careerService.getCareers(userId, page, size));
} catch (NotFoundException | BadRequestException e) {
return ErrorResponse.error(e.getErrorCode());
}
}

@GetMapping("/all")
public ApiResponse getAllCareers(@RequestHeader("userId") Long userId) {
try {
return SuccessResponse.success(SuccessCode.GET_ALL_CAREERS_SUCCESS, careerService.getAllCareers(userId));
} catch (NotFoundException | BadRequestException e) {
return ErrorResponse.error(e.getErrorCode());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.letscareer.career.domain.dto.response;

import com.example.letscareer.career.domain.dto.CareerDTO;

import java.util.List;

public record GetAllCareersResponse(
List<CareerDTO> careers
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface CareerRepository extends JpaRepository<Career, Long> {
public Optional<Career> findByCareerIdAndUser(Long careerId, User user);
Optional<Career> findByCareerIdAndUser(Long careerId, User user);

public Page<Career> findByUser(User user, Pageable pageable);
Page<Career> findByUser(User user, Pageable pageable);

List<Career> findByUser(User user);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.letscareer.career.service;

import com.example.letscareer.career.domain.dto.response.GetAllCareersResponse;
import com.example.letscareer.career.domain.model.Career;
import com.example.letscareer.career.domain.dto.CareerDTO;
import com.example.letscareer.career.domain.dto.request.SaveCareerRequest;
Expand All @@ -15,6 +16,7 @@
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -99,6 +101,25 @@ public GetCareersResponse getCareers(Long userId, int page, int size) {
);
}

@Cacheable(value = "allCareersCache", key = "#userId", unless = "#result == null || #result.careers.size() == 0")
public GetAllCareersResponse getAllCareers(Long userId) {
User user = userRepository.findByUserId(userId)
.orElseThrow(() -> new NotFoundException(ErrorCode.USER_NOT_FOUND_EXCEPTION));

List<Career> careers = careerRepository.findByUser(user);

List<CareerDTO> careerDTOS = careers.stream()
.map(career -> new CareerDTO(
career.getCareerId(),
career.getCategory().getValue(),
career.getTitle(),
toSummary(career.getSituation())
))
.collect(Collectors.toList());

return new GetAllCareersResponse(careerDTOS);
}

private String toSummary(String content) {
return content.length() > 25 ? content.substring(0, 25) + "..." : content;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.example.letscareer.common.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.cache.annotation.EnableCaching;

import java.time.Duration;

@EnableCaching
@Configuration
public class RedisCacheConfig {
@Bean
public RedisCacheConfiguration redisCacheConfiguration() {
return RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofMinutes(3)) // 3분
.disableCachingNullValues()
.serializeKeysWith(
RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())
)
.serializeValuesWith(
RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.example.letscareer.common.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;

@Configuration
public class RedisConfig {

@Value("${spring.data.redis.host}")
private String host;

@Value("${spring.data.redis.port}")
private int port;

@Bean
public LettuceConnectionFactory redisConnectionFactory() {
return new LettuceConnectionFactory(new RedisStandaloneConfiguration(host, port));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public enum SuccessCode {
STAGES_GET_SUCCESS(HttpStatus.OK, "지원 일정 및 단계 조회 성공"),
DOC_STAGES_GET_SUCCESS(HttpStatus.OK, "서류전형 단계 조회 성공"),
MID_STAGES_GET_SUCCESS(HttpStatus.OK, "중간전형 단계 조회 성공"),
INT_STAGES_GET_SUCCESS(HttpStatus.OK, "면접전형 단계 조회 성공"),;
INT_STAGES_GET_SUCCESS(HttpStatus.OK, "면접전형 단계 조회 성공"),
GET_ALL_CAREERS_SUCCESS(HttpStatus.OK, "커리어 전체 조회 성공"),
GET_CAREER_SUCCESS(HttpStatus.OK, "커리어 조회 성공"),;

private final HttpStatus httpStatus;
private final String message;
Expand Down

0 comments on commit b8e3bf1

Please sign in to comment.