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

[Feat] 이미지 캐싱 기능 구현 #46

Merged
merged 1 commit into from
Jan 26, 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
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
@EnableRedisRepositories
@EnableRedisRepositories(basePackages = "server.inuappcenter.kr.data.redis")
public class RedisConfig {
@Value("${spring.redis.host}")
private String host;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package server.inuappcenter.kr.data.redis.domain;

import lombok.Getter;
import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash;


@Getter
@RedisHash(value = "image")
public class ImageRedis {
@Id
private Long id;
private final byte[] imageData;

public ImageRedis(Long id, byte[] imageData) {
this.id = id;
this.imageData = imageData;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server.inuappcenter.kr.data.repository.redis;
package server.inuappcenter.kr.data.redis.repository;

import org.springframework.data.repository.CrudRepository;
import server.inuappcenter.kr.data.dto.response.BoardResponseDto;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package server.inuappcenter.kr.data.redis.repository;

import org.springframework.data.repository.CrudRepository;
import server.inuappcenter.kr.data.redis.domain.ImageRedis;

public interface ImageRedisRepository extends CrudRepository<ImageRedis, Long> {
}
20 changes: 17 additions & 3 deletions src/main/java/server/inuappcenter/kr/service/ImageService.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package server.inuappcenter.kr.service;

import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import server.inuappcenter.kr.data.domain.board.Image;
import server.inuappcenter.kr.data.redis.domain.ImageRedis;
import server.inuappcenter.kr.data.redis.repository.ImageRedisRepository;
import server.inuappcenter.kr.data.repository.ImageRepository;
import server.inuappcenter.kr.data.utils.ImageUtils;
import server.inuappcenter.kr.exception.customExceptions.CustomNotFoundException;
Expand All @@ -13,10 +14,23 @@
@RequiredArgsConstructor
public class ImageService {
private final ImageRepository imageRepository;
private final ImageRedisRepository imageRedisRepository;

@Transactional(readOnly = true)
public byte[] getImage(Long id) {
Image foundImage = imageRepository.findById(id).orElseThrow(() -> new CustomNotFoundException("The requested ID was not found."));
return ImageUtils.decompressImage(foundImage.getImageData());
return imageRedisRepository.findById(id)
.map(imageRedis -> ImageUtils.decompressImage(imageRedis.getImageData()))
.orElseGet(() -> {
Image foundImage = imageRepository.findById(id).orElseThrow(() -> new CustomNotFoundException("The requested ID was not found."));
ImageRedis imageRedis = new ImageRedis(foundImage.getId(), foundImage.getImageData());
imageRedisRepository.save(imageRedis);
return ImageUtils.decompressImage(foundImage.getImageData());
});
}

@Transactional
public void deleteByImageId(Long id) {
imageRedisRepository.deleteById(id);
imageRepository.deleteById(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
import server.inuappcenter.kr.data.domain.board.Image;
import server.inuappcenter.kr.data.dto.request.BoardRequestDto;
import server.inuappcenter.kr.data.dto.response.BoardResponseDto;
import server.inuappcenter.kr.data.redis.repository.ImageRedisRepository;
import server.inuappcenter.kr.data.repository.BoardRepository;
import server.inuappcenter.kr.data.repository.ImageRepository;
import server.inuappcenter.kr.data.repository.redis.BoardResponseRedisRepository;
import server.inuappcenter.kr.data.redis.repository.BoardResponseRedisRepository;
import server.inuappcenter.kr.exception.customExceptions.CustomNotFoundException;

import javax.servlet.http.HttpServletRequest;
Expand All @@ -27,6 +28,7 @@ public class BoardService {
private final ImageRepository imageRepository;
private final HttpServletRequest request;
private final BoardResponseRedisRepository<BoardResponseDto> boardResponseRedisRepository;
private final ImageRedisRepository imageRedisRepository;

@Transactional(readOnly = true)
public BoardResponseDto findBoard(Long id) {
Expand All @@ -48,13 +50,15 @@ public CommonResponseDto saveBoard(BoardRequestDto boardRequestDto) {

@Transactional
public CommonResponseDto deleteBoard(Long id) {
boardResponseRedisRepository.deleteById(id);
boardRepository.findById(id).orElseThrow(() -> new CustomNotFoundException("ID에 해당되는 보드가 없습니다."));
boardRepository.deleteById(id);
return new CommonResponseDto("id: " + id + " has been successfully deleted.");
}

@Transactional
public CommonResponseDto updateBoard(Long board_id, List<Long> image_id, BoardRequestDto boardRequestDto) {
// 캐시에서 보드를 삭제한다.
boardResponseRedisRepository.deleteById(board_id);
Board foundBoard = boardRepository.findById(board_id).orElseThrow(() -> new CustomNotFoundException("The requested ID was not found."));
// 사용자가 multipart를 같이 보냈는지 확인
Expand All @@ -74,6 +78,10 @@ public CommonResponseDto updateBoard(Long board_id, List<Long> image_id, BoardRe
// 찾아진 이미지 목록에서 id를 가져와 찾아진 id 목록에 추가함
foundImageIds.add(image.getId());
}

// 캐시에서 이미지를 삭제한다.
imageRedisRepository.deleteAllById(foundImageIds);

// 찾아진 ID 목록에 존재하지 않는 ID를 얻어야 하기 때문에 없는 이미지 ID 목록을 만들어줌
List<Long> missingImageIds = new ArrayList<>();
for (Long id : image_id) {
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/server/inuappcenter/kr/RedisTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.springframework.boot.test.context.SpringBootTest;
import server.inuappcenter.kr.data.dto.response.BoardResponseDto;
import server.inuappcenter.kr.data.dto.response.FaqBoardResponseDto;
import server.inuappcenter.kr.data.repository.redis.BoardResponseRedisRepository;
import server.inuappcenter.kr.data.redis.repository.BoardResponseRedisRepository;
import server.inuappcenter.kr.exception.customExceptions.CustomNotFoundException;

import java.time.LocalDateTime;
Expand Down
Loading