Added caching.

This commit is contained in:
neviyn 2018-10-10 11:10:07 +01:00
parent 691f585485
commit cbdcd80fef
4 changed files with 17 additions and 1 deletions

View File

@ -56,6 +56,10 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId> <artifactId>spring-boot-starter-security</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency> <dependency>
<groupId>com.fasterxml.jackson.module</groupId> <groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId> <artifactId>jackson-module-kotlin</artifactId>
@ -95,6 +99,11 @@
<artifactId>usertype.core</artifactId> <artifactId>usertype.core</artifactId>
<version>7.0.0.CR1</version> <version>7.0.0.CR1</version>
</dependency> </dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.6.2</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>

View File

@ -2,6 +2,7 @@ package uk.co.neviyn.observationdatabase
import org.joda.time.LocalDate import org.joda.time.LocalDate
import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Autowired
import org.springframework.cache.annotation.Cacheable
import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.PostMapping
@ -118,6 +119,7 @@ class Controller {
* Get all observations that are within the constraints provided in [observationsRequest]. * Get all observations that are within the constraints provided in [observationsRequest].
*/ */
@PostMapping("/observations") @PostMapping("/observations")
@Cacheable("observations")
fun getObservations(@Valid @RequestBody observationsRequest: ObservationsRequest): List<Observation> { fun getObservations(@Valid @RequestBody observationsRequest: ObservationsRequest): List<Observation> {
if (observationsRequest.tutor != null) { if (observationsRequest.tutor != null) {
return tutorRepository.findById(observationsRequest.tutor).map { return tutorRepository.findById(observationsRequest.tutor).map {

View File

@ -2,8 +2,10 @@ package uk.co.neviyn.observationdatabase
import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication import org.springframework.boot.runApplication
import org.springframework.cache.annotation.EnableCaching
@SpringBootApplication @SpringBootApplication
@EnableCaching
class ObservationDatabaseApplication { class ObservationDatabaseApplication {
companion object { companion object {
@JvmStatic @JvmStatic

View File

@ -1,4 +1,7 @@
spring.jpa.properties.jadira.usertype.autoRegisterUserTypes = true spring.jpa.properties.jadira.usertype.autoRegisterUserTypes = true
spring.datasource.url=jdbc:h2:file:./database spring.datasource.url=jdbc:h2:file:./database
spring.jpa.hibernate.ddl-auto=update spring.jpa.hibernate.ddl-auto=update
obsdb.password=observation obsdb.password=observation
spring.cache.type=caffeine
spring.cache.cache-names=observations
spring.cache.caffeine.spec=maximumSize=500,expireAfterAccess=600s