3
3
import com .rest .api .advice .exception .CNotOwnerException ;
4
4
import com .rest .api .advice .exception .CResourceNotExistException ;
5
5
import com .rest .api .advice .exception .CUserNotFoundException ;
6
+ import com .rest .api .common .CacheKey ;
6
7
import com .rest .api .entity .User ;
7
8
import com .rest .api .entity .board .Board ;
8
9
import com .rest .api .entity .board .Post ;
9
10
import com .rest .api .model .board .ParamsPost ;
10
11
import com .rest .api .repo .UserJpaRepo ;
11
12
import com .rest .api .repo .board .BoardJpaRepo ;
12
13
import com .rest .api .repo .board .PostJpaRepo ;
14
+ import com .rest .api .service .cache .CacheSevice ;
13
15
import lombok .RequiredArgsConstructor ;
16
+ import lombok .extern .slf4j .Slf4j ;
17
+ import org .springframework .cache .annotation .CacheEvict ;
18
+ import org .springframework .cache .annotation .CachePut ;
19
+ import org .springframework .cache .annotation .Cacheable ;
14
20
import org .springframework .stereotype .Service ;
15
21
16
22
import javax .transaction .Transactional ;
17
23
import java .util .List ;
18
24
import java .util .Optional ;
19
25
26
+ @ Slf4j
20
27
@ Service
21
28
@ Transactional
22
29
@ RequiredArgsConstructor
@@ -25,34 +32,40 @@ public class BoardService {
25
32
private final BoardJpaRepo boardJpaRepo ;
26
33
private final PostJpaRepo postJpaRepo ;
27
34
private final UserJpaRepo userJpaRepo ;
35
+ private final CacheSevice cacheSevice ;
28
36
29
37
public Board insertBoard (String boardName ) {
30
38
return boardJpaRepo .save (Board .builder ().name (boardName ).build ());
31
39
}
32
40
33
41
// 게시판 이름으로 게시판을 조회. 없을경우 CResourceNotExistException 처리
42
+ @ Cacheable (value = CacheKey .BOARD , key = "#boardName" , unless = "#result == null" )
34
43
public Board findBoard (String boardName ) {
35
44
return Optional .ofNullable (boardJpaRepo .findByName (boardName )).orElseThrow (CResourceNotExistException ::new );
36
45
}
37
46
38
- // 게시판 이름으로 게시물 리스트 조회.
47
+ // 게시판 이름으로 게시글 리스트 조회.
48
+ @ Cacheable (value = CacheKey .POSTS , key = "#boardName" , unless = "#result == null" )
39
49
public List <Post > findPosts (String boardName ) {
40
50
return postJpaRepo .findByBoardOrderByPostIdDesc (findBoard (boardName ));
41
51
}
42
52
43
- // 게시물ID로 게시물 단건 조회. 없을경우 CResourceNotExistException 처리
53
+ // 게시글ID로 게시글 단건 조회. 없을경우 CResourceNotExistException 처리
54
+ @ Cacheable (value = CacheKey .POST , key = "#postId" , unless = "#result == null" )
44
55
public Post getPost (long postId ) {
45
56
return postJpaRepo .findById (postId ).orElseThrow (CResourceNotExistException ::new );
46
57
}
47
58
48
- // 게시물을 등록합니다. 게시물의 회원UID가 조회되지 않으면 CUserNotFoundException 처리합니다.
59
+ // 게시글을 등록합니다. 게시글의 회원UID가 조회되지 않으면 CUserNotFoundException 처리합니다.
60
+ @ CacheEvict (value = CacheKey .POSTS , key = "#boardName" )
49
61
public Post writePost (String uid , String boardName , ParamsPost paramsPost ) {
50
62
Board board = findBoard (boardName );
51
63
Post post = new Post (userJpaRepo .findByUid (uid ).orElseThrow (CUserNotFoundException ::new ), board , paramsPost .getAuthor (), paramsPost .getTitle (), paramsPost .getContent ());
52
64
return postJpaRepo .save (post );
53
65
}
54
66
55
- // 게시물을 수정합니다. 게시물 등록자와 로그인 회원정보가 틀리면 CNotOwnerException 처리합니다.
67
+ // 게시글을 수정합니다. 게시글 등록자와 로그인 회원정보가 틀리면 CNotOwnerException 처리합니다.
68
+ //@CachePut(value = CacheKey.POST, key = "#postId") 갱신된 정보만 캐시할경우에만 사용!
56
69
public Post updatePost (long postId , String uid , ParamsPost paramsPost ) {
57
70
Post post = getPost (postId );
58
71
User user = post .getUser ();
@@ -61,16 +74,18 @@ public Post updatePost(long postId, String uid, ParamsPost paramsPost) {
61
74
62
75
// 영속성 컨텍스트의 변경감지(dirty checking) 기능에 의해 조회한 Post내용을 변경만 해도 Update쿼리가 실행됩니다.
63
76
post .setUpdate (paramsPost .getAuthor (), paramsPost .getTitle (), paramsPost .getContent ());
77
+ cacheSevice .deleteBoardCache (post .getPostId (), post .getBoard ().getName ());
64
78
return post ;
65
79
}
66
80
67
- // 게시물을 삭제합니다. 게시물 등록자와 로그인 회원정보가 틀리면 CNotOwnerException 처리합니다.
81
+ // 게시글을 삭제합니다. 게시글 등록자와 로그인 회원정보가 틀리면 CNotOwnerException 처리합니다.
68
82
public boolean deletePost (long postId , String uid ) {
69
83
Post post = getPost (postId );
70
84
User user = post .getUser ();
71
85
if (!uid .equals (user .getUid ()))
72
86
throw new CNotOwnerException ();
73
87
postJpaRepo .delete (post );
88
+ cacheSevice .deleteBoardCache (post .getPostId (), post .getBoard ().getName ());
74
89
return true ;
75
90
}
76
91
}
0 commit comments