-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from pavelfomin/feature/enable-cache
Enable caching using `@EnableCaching`
- Loading branch information
Showing
6 changed files
with
125 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/com/droidablebee/springboot/rest/service/CacheableService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.droidablebee.springboot.rest.service; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.cache.annotation.Cacheable; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class CacheableService { | ||
|
||
@Autowired | ||
private CacheableServiceClient cacheableServiceClient; | ||
|
||
@Cacheable("CacheableClient") | ||
public Page<Result> retrieve(Pageable pageable) { | ||
|
||
return cacheableServiceClient.retrieve(pageable); | ||
} | ||
|
||
interface Result { | ||
} | ||
|
||
@Component | ||
public static class CacheableServiceClient { | ||
|
||
public Page<Result> retrieve(Pageable pageable) { | ||
return Page.empty(pageable); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/test/groovy/com/droidablebee/springboot/rest/ApplicationSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.droidablebee.springboot.rest | ||
|
||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.context.ApplicationContext | ||
import spock.lang.Specification | ||
|
||
@SpringBootTest | ||
class ApplicationSpec extends Specification { | ||
|
||
@Autowired | ||
ApplicationContext context | ||
|
||
def "context initializes successfully"() { | ||
|
||
expect: | ||
context | ||
context.environment | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/test/groovy/com/droidablebee/springboot/rest/service/CacheableServiceSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.droidablebee.springboot.rest.service | ||
|
||
import com.droidablebee.springboot.rest.domain.Person | ||
import org.spockframework.spring.SpringBean | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.data.domain.Page | ||
import org.springframework.data.domain.PageRequest | ||
import spock.lang.Specification | ||
|
||
@SpringBootTest | ||
class CacheableServiceSpec extends Specification { | ||
|
||
@Autowired | ||
CacheableService cacheableService | ||
|
||
@SpringBean | ||
CacheableService.CacheableServiceClient cacheableServiceClient = Mock() | ||
|
||
void setup() { | ||
|
||
0 * _ | ||
} | ||
|
||
def "retrieve - cached"() { | ||
|
||
given: | ||
PageRequest pageRequest1 = PageRequest.of(0, 10) | ||
PageRequest pageRequest2 = PageRequest.of(0, 20) | ||
Page<CacheableService.Result> page1 = Mock() | ||
Page<CacheableService.Result> page2 = Mock() | ||
|
||
when: "first service call is made" | ||
Page<CacheableService.Result> result = cacheableService.retrieve(pageRequest1) | ||
|
||
then: "client method is called" | ||
1 * cacheableServiceClient.retrieve(pageRequest1) >> page1 | ||
|
||
result == page1 | ||
|
||
when: "subsequent service call is made" | ||
Page<CacheableService.Result> cached = cacheableService.retrieve(pageRequest1) | ||
|
||
then: "cached value is returned w/out client method call" | ||
cached == result | ||
|
||
when: "different parameter is used" | ||
result = cacheableService.retrieve(pageRequest2) | ||
|
||
then: "client method is called" | ||
1 * cacheableServiceClient.retrieve(pageRequest2) >> page2 | ||
|
||
result == page2 | ||
|
||
when: "subsequent service call is made" | ||
cached = cacheableService.retrieve(pageRequest2) | ||
|
||
then: "cached value is returned w/out client method call" | ||
cached == result | ||
} | ||
|
||
} |