Skip to content

Commit 6ff5cef

Browse files
committed
[FIX] #26 SpringBootCache
1 parent d0bfad0 commit 6ff5cef

File tree

11 files changed

+162
-28
lines changed

11 files changed

+162
-28
lines changed

SpringBootCache/build.gradle

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,38 @@
1-
buildscript {
2-
ext {
3-
springBootVersion = '2.1.3.RELEASE'
4-
}
5-
repositories {
6-
mavenCentral()
7-
}
8-
dependencies {
9-
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
10-
}
1+
plugins {
2+
id 'org.springframework.boot' version '2.1.3.RELEASE'
3+
id 'org.jetbrains.kotlin.jvm' version '1.2.71'
4+
id 'org.jetbrains.kotlin.plugin.spring' version '1.2.71'
115
}
126

13-
apply plugin: 'java'
14-
apply plugin: 'eclipse'
15-
apply plugin: 'org.springframework.boot'
167
apply plugin: 'io.spring.dependency-management'
178

189
group = 'com.example'
1910
version = '0.0.1-SNAPSHOT'
20-
sourceCompatibility = 1.8
11+
sourceCompatibility = '1.8'
2112

2213
repositories {
2314
mavenCentral()
2415
}
2516

2617
dependencies {
27-
compile('org.springframework.boot:spring-boot-starter-cache')
28-
testCompile("org.springframework.boot:spring-boot-starter-test")
18+
implementation 'org.springframework.boot:spring-boot-starter-cache'
19+
implementation 'com.fasterxml.jackson.module:jackson-module-kotlin'
20+
implementation 'org.jetbrains.kotlin:kotlin-reflect'
21+
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
22+
23+
testImplementation 'org.springframework.boot:spring-boot-starter-test'
24+
}
25+
26+
compileKotlin {
27+
kotlinOptions {
28+
freeCompilerArgs = ['-Xjsr305=strict']
29+
jvmTarget = '1.8'
30+
}
31+
}
32+
33+
compileTestKotlin {
34+
kotlinOptions {
35+
freeCompilerArgs = ['-Xjsr305=strict']
36+
jvmTarget = '1.8'
37+
}
2938
}

SpringBootCache/src/main/java/com/example/SpringBootCacheApplication.java renamed to SpringBootCache/src/main/java/com/example/java/SpringBootCacheApplication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.example;
1+
package com.example.java;
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;

SpringBootCache/src/main/java/com/example/component/BookRepository.java renamed to SpringBootCache/src/main/java/com/example/java/component/BookRepository.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package com.example.component;
1+
package com.example.java.component;
22

3-
import com.example.domain.Book;
3+
import com.example.java.domain.Book;
44

55
public interface BookRepository {
66

SpringBootCache/src/main/java/com/example/component/SimpleBookRepository.java renamed to SpringBootCache/src/main/java/com/example/java/component/SimpleBookRepository.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package com.example.component;
1+
package com.example.java.component;
22

3-
import com.example.domain.Book;
3+
import com.example.java.domain.Book;
44
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
66
import org.springframework.cache.annotation.CacheEvict;

SpringBootCache/src/main/java/com/example/domain/Book.java renamed to SpringBootCache/src/main/java/com/example/java/domain/Book.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.example.domain;
1+
package com.example.java.domain;
22

33
public class Book {
44

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example.kotlin
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication
4+
import org.springframework.boot.runApplication
5+
import org.springframework.cache.annotation.EnableCaching
6+
7+
@SpringBootApplication
8+
@EnableCaching
9+
class SpringBootCacheApplication
10+
11+
fun main(args: Array<String>) {
12+
runApplication<SpringBootCacheApplication>(*args)
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.example.kotlin.component
2+
3+
import com.example.kotlin.domain.Book
4+
5+
interface BookRepository {
6+
7+
fun getByIsbn(isbn: String): Book
8+
9+
fun refresh(isbn: String)
10+
11+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.example.kotlin.component
2+
3+
import com.example.kotlin.domain.Book
4+
import org.slf4j.LoggerFactory
5+
import org.springframework.cache.annotation.CacheEvict
6+
import org.springframework.cache.annotation.Cacheable
7+
import org.springframework.stereotype.Component
8+
9+
@Component
10+
class SimpleBookRepository : BookRepository {
11+
12+
companion object {
13+
private val logger = LoggerFactory.getLogger(SimpleBookRepository::class.java)
14+
private const val CACHE_BOOK = "book"
15+
}
16+
17+
@Cacheable(value = [CACHE_BOOK], key = "#isbn")
18+
override fun getByIsbn(isbn: String): Book {
19+
simulateSlowService()
20+
return Book(isbn, "Some book")
21+
}
22+
23+
private fun simulateSlowService() {
24+
try {
25+
Thread.sleep(3000L)
26+
} catch (e: InterruptedException) {
27+
throw IllegalStateException(e)
28+
}
29+
30+
}
31+
32+
@CacheEvict(value = [CACHE_BOOK], key = "#isbn")
33+
override fun refresh(isbn: String) {
34+
logger.info("cache clear => $isbn")
35+
}
36+
}
37+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.example.kotlin.domain
2+
3+
data class Book(val isbn: String, val title: String) {
4+
constructor() : this("", "")
5+
}

SpringBootCache/src/test/java/com/example/SpringBootCacheApplicationTests.java renamed to SpringBootCache/src/test/java/com/example/java/SpringBootCacheApplicationTests.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package com.example;
1+
package com.example.java;
22

3-
import com.example.component.BookRepository;
3+
import com.example.java.component.BookRepository;
44
import org.junit.After;
55
import org.junit.Before;
66
import org.junit.Test;
@@ -12,14 +12,13 @@
1212
import org.springframework.test.context.junit4.SpringRunner;
1313

1414
@RunWith(SpringRunner.class)
15-
@SpringBootTest
15+
@SpringBootTest(classes = SpringBootCacheApplication.class)
1616
public class SpringBootCacheApplicationTests {
1717

1818
@Autowired
1919
private BookRepository repository;
2020

2121
private long startTime;
22-
private long endTime;
2322

2423
private static final Logger logger = LoggerFactory.getLogger(SpringBootCacheApplicationTests.class);
2524

@@ -30,8 +29,7 @@ public void onBefore() {
3029

3130
@After
3231
public void onAfter() {
33-
endTime = System.currentTimeMillis();
34-
logger.info("소요시간: {}ms", endTime - startTime);
32+
logger.info("소요시간: {}ms", System.currentTimeMillis() - startTime);
3533
}
3634

3735
@Test

0 commit comments

Comments
 (0)