Redis Cache annotation

This commit is contained in:
kimyonghwa
2019-08-08 01:52:39 +09:00
parent e729ff1504
commit 945b44fdc7
7 changed files with 150 additions and 10 deletions

View File

@@ -15,6 +15,7 @@ import com.rest.api.service.cache.CacheSevice;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@@ -39,19 +40,19 @@ public class BoardService {
return Optional.ofNullable(boardJpaRepo.findByName(boardName)).orElseThrow(CResourceNotExistException::new);
}
// 게시판 이름으로 게시 리스트 조회.
// 게시판 이름으로 게시 리스트 조회.
@Cacheable(value = CacheKey.POSTS, key = "#boardName", unless = "#result == null")
public List<Post> findPosts(String boardName) {
return postJpaRepo.findByBoard(findBoard(boardName));
}
// 게시ID로 게시 단건 조회. 없을경우 CResourceNotExistException 처리
// 게시ID로 게시 단건 조회. 없을경우 CResourceNotExistException 처리
@Cacheable(value = CacheKey.POST, key = "#postId", unless = "#result == null")
public Post getPost(long postId) {
return postJpaRepo.findById(postId).orElseThrow(CResourceNotExistException::new);
}
// 게시을 등록합니다. 게시의 회원UID가 조회되지 않으면 CUserNotFoundException 처리합니다.
// 게시을 등록합니다. 게시의 회원UID가 조회되지 않으면 CUserNotFoundException 처리합니다.
@CacheEvict(value = CacheKey.POSTS, key = "#boardName")
public Post writePost(String uid, String boardName, ParamsPost paramsPost) {
Board board = findBoard(boardName);
@@ -59,7 +60,8 @@ public class BoardService {
return postJpaRepo.save(post);
}
// 게시을 수정합니다. 게시 등록자와 로그인 회원정보가 틀리면 CNotOwnerException 처리합니다.
// 게시을 수정합니다. 게시 등록자와 로그인 회원정보가 틀리면 CNotOwnerException 처리합니다.
//@CachePut(value = CacheKey.POST, key = "#postId") 갱신된 정보만 캐시할경우에만 사용!
public Post updatePost(long postId, String uid, ParamsPost paramsPost) {
Post post = getPost(postId);
User user = post.getUser();
@@ -72,7 +74,7 @@ public class BoardService {
return post;
}
// 게시을 삭제합니다. 게시 등록자와 로그인 회원정보가 틀리면 CNotOwnerException 처리합니다.
// 게시을 삭제합니다. 게시 등록자와 로그인 회원정보가 틀리면 CNotOwnerException 처리합니다.
public boolean deletePost(long postId, String uid) {
Post post = getPost(postId);
User user = post.getUser();