File tree Expand file tree Collapse file tree 11 files changed +162
-28
lines changed 
kotlin/com/example/kotlin 
kotlin/com/example/kotlin Expand file tree Collapse file tree 11 files changed +162
-28
lines changed Original file line number Diff line number Diff line change 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' ' 2.1.3.RELEASE' 
3+     id ' org.jetbrains.kotlin.jvm' ' 1.2.71' 
4+     id ' org.jetbrains.kotlin.plugin.spring' ' 1.2.71' 
115}
126
13- apply plugin : ' java' 
14- apply plugin : ' eclipse' 
15- apply plugin : ' org.springframework.boot' 
167apply plugin : ' io.spring.dependency-management' 
178
189group =  ' com.example' 
1910version =  ' 0.0.1-SNAPSHOT' 
20- sourceCompatibility =  1.8 
11+ sourceCompatibility =  ' 1.8' 
2112
2213repositories  {
2314    mavenCentral()
2415}
2516
2617dependencies  {
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}
Original file line number Diff line number Diff line change 1- package  com .example ;
1+ package  com .example . java ;
22
33import  org .springframework .boot .SpringApplication ;
44import  org .springframework .boot .autoconfigure .SpringBootApplication ;
Original file line number Diff line number Diff line change 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
55public  interface  BookRepository  {
66
Original file line number Diff line number Diff line change 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 ;
44import  org .slf4j .Logger ;
55import  org .slf4j .LoggerFactory ;
66import  org .springframework .cache .annotation .CacheEvict ;
Original file line number Diff line number Diff line change 1- package  com .example .domain ;
1+ package  com .example .java . domain ;
22
33public  class  Book  {
44
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ 
Original file line number Diff line number Diff line change 1+ package  com.example.kotlin.domain 
2+ 
3+ data class  Book (val  isbn :  String , val  title :  String ) {
4+     constructor () :  this (" " " " 
5+ }
Original file line number Diff line number Diff line change 1- package  com .example ;
1+ package  com .example . java ;
22
3- import  com .example .component .BookRepository ;
3+ import  com .example .java . component .BookRepository ;
44import  org .junit .After ;
55import  org .junit .Before ;
66import  org .junit .Test ;
1212import  org .springframework .test .context .junit4 .SpringRunner ;
1313
1414@ RunWith (SpringRunner .class )
15- @ SpringBootTest 
15+ @ SpringBootTest ( classes  =  SpringBootCacheApplication . class ) 
1616public  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 
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments