Skip to content

Commit

Permalink
Merge pull request #27 from pavelfomin/feature/enable-cache
Browse files Browse the repository at this point in the history
Enable caching using `@EnableCaching`
  • Loading branch information
pavelfomin committed Aug 25, 2023
2 parents ba9c494 + c71e676 commit 737828a
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 1 deletion.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ dependencies {
implementation "org.springframework.boot:spring-boot-starter-data-jpa"
implementation "org.springframework.boot:spring-boot-starter-security"
implementation "org.springframework.boot:spring-boot-starter-oauth2-resource-server"
// implementation 'org.springframework.boot:spring-boot-starter-cache'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.0'

implementation "com.h2database:h2:2.1.210"
// implementation 'com.github.ben-manes.caffeine:caffeine'

testImplementation ("org.springframework.boot:spring-boot-starter-test") {
// exclude group: 'org.mockito'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@EnableCaching
public class Application {
@SuppressWarnings("unused")
private static final Logger log = LoggerFactory.getLogger(Application.class);
Expand Down
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);
}
}
}
7 changes: 6 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ spring:
datasource:
platform: h2
driverClassName: org.h2.Driver
url: jdbc:h2:mem:test;DB_CLOSE_ON_EXIT=FALSE;MODE=MYSQL;INIT=CREATE SCHEMA IF NOT EXISTS ddb
url: jdbc:h2:mem:test;DB_CLOSE_ON_EXIT=FALSE;MODE=MYSQL;INIT=CREATE SCHEMA IF NOT EXISTS ddb

cache:
cache-names: CacheableClient
caffeine:
spec: expireAfterAccess=5m
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
}
}
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
}

}

0 comments on commit 737828a

Please sign in to comment.