Skip to content

Week5 #19

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 12 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ plugins {
id 'java'
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'org.dailystudio'
Expand All @@ -20,8 +22,16 @@ repositories {
}

dependencies {
compile('org.springframework.boot:spring-boot-starter-cache')
testCompile('org.springframework.boot:spring-boot-starter-test')

implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
compile('com.h2database:h2')

testCompile('org.assertj:assertj-core:3.9.0')

implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
compile 'org.projectlombok:lombok:+'
annotationProcessor 'org.projectlombok:lombok:+'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
@SpringBootApplication
public class SpringbootstudyApplication {

public static void main(String[] args) {
SpringApplication.run(SpringbootstudyApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(SpringbootstudyApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.dailystudio.springbootstudy.config;

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableCaching
public class CacheConfig {
}
28 changes: 28 additions & 0 deletions src/main/java/org/dailystudio/springbootstudy/domain/Board.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.dailystudio.springbootstudy.domain;

import lombok.NoArgsConstructor;

import javax.persistence.*;
import javax.validation.constraints.NotNull;

@Entity
@Table(name = "BOARD")
@NoArgsConstructor
public class Board {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column
@NotNull
private String content;

public String getContent() {
return content;
}

public Board(@NotNull String content) {
this.content = content;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.dailystudio.springbootstudy.repository;

import org.dailystudio.springbootstudy.domain.Board;
import org.springframework.data.jpa.repository.JpaRepository;

public interface BoardRepository extends JpaRepository<Board, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.dailystudio.springbootstudy.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dailystudio.springbootstudy.domain.Board;
import org.dailystudio.springbootstudy.repository.BoardRepository;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
@RequiredArgsConstructor
public class BoardService {

private final BoardRepository boardRepository;

@CacheEvict(value = "contents", allEntries = true)
public void saveContent(String content) {
Board board = new Board(content);
boardRepository.save(board);
}

@Cacheable(value = "contents")
public List<String> fetchContents() {
logCache();
return boardRepository.findAll().stream()
.map(Board::getContent)
.collect(Collectors.toList());
}

private void logCache() {
log.info("new cache.");
}
}
6 changes: 5 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@

spring.h2.console.path=/h2
spring.datasource.url=jdbc:h2:~/ds;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
spring.jpa.hibernate.ddl-auto=create-drop
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
@SpringBootTest
public class SpringbootstudyApplicationTests {

@Test
public void contextLoads() {
}
@Test
public void contextLoads() {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.dailystudio.springbootstudy.service;

import lombok.extern.slf4j.Slf4j;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class BoardServiceTest {

private static final String DEFAULT_CONTENT = "default";

@Autowired
private BoardService boardService;

private List<String> contents;

@After
public void tearDown() throws Exception {
log.info(contents.toString());
}

@Test
public void 캐시0() {
boardService.saveContent(DEFAULT_CONTENT);
contents = boardService.fetchContents();
}

@Test
public void 캐시1() {
contents = boardService.fetchContents();
}

@Test
public void 캐시2() {
contents = boardService.fetchContents();
}

@Test
public void 캐시3() {
boardService.saveContent("캐시3");
contents = boardService.fetchContents();
}

@Test
public void 캐시4() {
contents = boardService.fetchContents();
}
}