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,30 +32,36 @@ 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
// 게시판 이름으로 게시판을 조회. 없을경우 CResourceNotExistException 처리
38
+ @ Cacheable (value = CacheKey .BOARD , key = "#boardName" , unless = "#result == null" )
30
39
public Board findBoard (String boardName ) {
31
40
return Optional .ofNullable (boardJpaRepo .findByName (boardName )).orElseThrow (CResourceNotExistException ::new );
32
41
}
33
42
34
- // 게시판 이름으로 게시물 리스트 조회.
43
+ // 게시판 이름으로 게시글 리스트 조회.
44
+ @ Cacheable (value = CacheKey .POSTS , key = "#boardName" , unless = "#result == null" )
35
45
public List <Post > findPosts (String boardName ) {
36
46
return postJpaRepo .findByBoard (findBoard (boardName ));
37
47
}
38
48
39
- // 게시물ID로 게시물 단건 조회. 없을경우 CResourceNotExistException 처리
49
+ // 게시글ID로 게시글 단건 조회. 없을경우 CResourceNotExistException 처리
50
+ @ Cacheable (value = CacheKey .POST , key = "#postId" , unless = "#result == null" )
40
51
public Post getPost (long postId ) {
41
52
return postJpaRepo .findById (postId ).orElseThrow (CResourceNotExistException ::new );
42
53
}
43
54
44
- // 게시물을 등록합니다. 게시물의 회원UID가 조회되지 않으면 CUserNotFoundException 처리합니다.
55
+ // 게시글을 등록합니다. 게시글의 회원UID가 조회되지 않으면 CUserNotFoundException 처리합니다.
56
+ @ CacheEvict (value = CacheKey .POSTS , key = "#boardName" )
45
57
public Post writePost (String uid , String boardName , ParamsPost paramsPost ) {
46
58
Board board = findBoard (boardName );
47
59
Post post = new Post (userJpaRepo .findByUid (uid ).orElseThrow (CUserNotFoundException ::new ), board , paramsPost .getAuthor (), paramsPost .getTitle (), paramsPost .getContent ());
48
60
return postJpaRepo .save (post );
49
61
}
50
62
51
- // 게시물을 수정합니다. 게시물 등록자와 로그인 회원정보가 틀리면 CNotOwnerException 처리합니다.
63
+ // 게시글을 수정합니다. 게시글 등록자와 로그인 회원정보가 틀리면 CNotOwnerException 처리합니다.
64
+ //@CachePut(value = CacheKey.POST, key = "#postId") 갱신된 정보만 캐시할경우에만 사용!
52
65
public Post updatePost (long postId , String uid , ParamsPost paramsPost ) {
53
66
Post post = getPost (postId );
54
67
User user = post .getUser ();
@@ -57,16 +70,18 @@ public Post updatePost(long postId, String uid, ParamsPost paramsPost) {
57
70
58
71
// 영속성 컨텍스트의 변경감지(dirty checking) 기능에 의해 조회한 Post내용을 변경만 해도 Update쿼리가 실행됩니다.
59
72
post .setUpdate (paramsPost .getAuthor (), paramsPost .getTitle (), paramsPost .getContent ());
73
+ cacheSevice .deleteBoardCache (post .getPostId (), post .getBoard ().getName ());
60
74
return post ;
61
75
}
62
76
63
- // 게시물을 삭제합니다. 게시물 등록자와 로그인 회원정보가 틀리면 CNotOwnerException 처리합니다.
77
+ // 게시글을 삭제합니다. 게시글 등록자와 로그인 회원정보가 틀리면 CNotOwnerException 처리합니다.
64
78
public boolean deletePost (long postId , String uid ) {
65
79
Post post = getPost (postId );
66
80
User user = post .getUser ();
67
81
if (!uid .equals (user .getUid ()))
68
82
throw new CNotOwnerException ();
69
83
postJpaRepo .delete (post );
84
+ cacheSevice .deleteBoardCache (post .getPostId (), post .getBoard ().getName ());
70
85
return true ;
71
86
}
72
87
}
0 commit comments