From 9a8b5b16d3b628fc1b21e862ab1ad0b6fa4c648a Mon Sep 17 00:00:00 2001 From: Gustavo Date: Fri, 29 Jun 2018 16:05:37 -0300 Subject: [PATCH] Add redis cache. --- pom.xml | 16 ++-------------- .../quem/me/ajuda/QuemMeAjudaApplication.java | 2 ++ .../me/ajuda/controllers/StudentController.java | 12 ++++++++---- .../quem/me/ajuda/models/MinimalStudent.java | 6 +++++- src/main/java/quem/me/ajuda/models/Student.java | 2 ++ .../me/ajuda/security/SecurityConfiguration.java | 1 + src/main/resources/application.properties | 10 +++++++++- 7 files changed, 29 insertions(+), 20 deletions(-) diff --git a/pom.xml b/pom.xml index c4e2e97..6e4e817 100644 --- a/pom.xml +++ b/pom.xml @@ -37,16 +37,12 @@ org.springframework.boot - spring-boot-starter-cache + spring-boot-starter-data-redis org.springframework.boot spring-boot-starter-data-jpa - - org.springframework.boot - spring-boot-starter-hateoas - org.springframework.boot spring-boot-starter-security @@ -55,10 +51,7 @@ org.springframework.boot spring-boot-starter-web-services - - org.springframework.boot - spring-boot-starter-webflux - + org.springframework.boot @@ -75,11 +68,6 @@ spring-boot-starter-test test - - io.projectreactor - reactor-test - test - org.springframework.restdocs spring-restdocs-mockmvc diff --git a/src/main/java/quem/me/ajuda/QuemMeAjudaApplication.java b/src/main/java/quem/me/ajuda/QuemMeAjudaApplication.java index 57c395d..d116123 100644 --- a/src/main/java/quem/me/ajuda/QuemMeAjudaApplication.java +++ b/src/main/java/quem/me/ajuda/QuemMeAjudaApplication.java @@ -2,7 +2,9 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.annotation.EnableCaching; +@EnableCaching @SpringBootApplication public class QuemMeAjudaApplication { diff --git a/src/main/java/quem/me/ajuda/controllers/StudentController.java b/src/main/java/quem/me/ajuda/controllers/StudentController.java index 2496f69..a3929d6 100644 --- a/src/main/java/quem/me/ajuda/controllers/StudentController.java +++ b/src/main/java/quem/me/ajuda/controllers/StudentController.java @@ -1,10 +1,12 @@ package quem.me.ajuda.controllers; -import java.util.List; +import java.util.Collection; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.Cacheable; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity.BodyBuilder; @@ -25,12 +27,13 @@ @RestController @CrossOrigin -@RequestMapping(value = Endpoints.STUDENT_ENDPONT) +@RequestMapping(Endpoints.STUDENT_ENDPONT) public class StudentController { @Autowired private StudentService service; @PostMapping + @CacheEvict(value = Endpoints.STUDENT_ENDPONT, allEntries = true) public ResponseEntity create(@Valid @RequestBody Student student) { return new ResponseEntity<>(this.service.create(student), HttpStatus.CREATED); } @@ -41,8 +44,9 @@ public ResponseEntity update(@PathVariable Long id, @Valid @RequestBody } @GetMapping - public ResponseEntity> getAll() { - return ResponseEntity.ok(this.service.getAll()); + @Cacheable(Endpoints.STUDENT_ENDPONT) + public Collection getAll() { + return this.service.getAll(); } @GetMapping(value = "/{id}") diff --git a/src/main/java/quem/me/ajuda/models/MinimalStudent.java b/src/main/java/quem/me/ajuda/models/MinimalStudent.java index eeaa52f..d0a8059 100644 --- a/src/main/java/quem/me/ajuda/models/MinimalStudent.java +++ b/src/main/java/quem/me/ajuda/models/MinimalStudent.java @@ -1,10 +1,14 @@ package quem.me.ajuda.models; +import java.io.Serializable; + import lombok.Getter; import lombok.Setter; @Getter @Setter -public class MinimalStudent { +public class MinimalStudent implements Serializable{ + private static final long serialVersionUID = 1L; + private String name; private String email; private Boolean isTutor; diff --git a/src/main/java/quem/me/ajuda/models/Student.java b/src/main/java/quem/me/ajuda/models/Student.java index 82db849..8d04792 100644 --- a/src/main/java/quem/me/ajuda/models/Student.java +++ b/src/main/java/quem/me/ajuda/models/Student.java @@ -19,11 +19,13 @@ import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; +import lombok.ToString; @Entity @Table(name = "students") @Getter @Setter @EqualsAndHashCode(of = {"registration"}) +@ToString @NoArgsConstructor @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) // Lombok in probably creating gets/sets for internal properties of string public class Student { diff --git a/src/main/java/quem/me/ajuda/security/SecurityConfiguration.java b/src/main/java/quem/me/ajuda/security/SecurityConfiguration.java index 9f1484c..d2a0e35 100644 --- a/src/main/java/quem/me/ajuda/security/SecurityConfiguration.java +++ b/src/main/java/quem/me/ajuda/security/SecurityConfiguration.java @@ -28,6 +28,7 @@ protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/login").permitAll() .antMatchers(HttpMethod.POST, "/students").permitAll() + .antMatchers(HttpMethod.GET, "/students").permitAll() .antMatchers(HttpMethod.OPTIONS, "**").permitAll() .anyRequest().authenticated() .and() diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 54c3b6a..56c4129 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,4 +1,12 @@ + +# Database spring.datasource.url= jdbc:postgresql://localhost:5432/meajuda spring.datasource.username=meajuda spring.datasource.password=meajuda -spring.jpa.hibernate.ddl-auto=update \ No newline at end of file +spring.jpa.hibernate.ddl-auto=create-drop + +# Cache +spring.cache.type=redis +spring.redis.host=127.0.0.1 +spring.redis.port=6379 +spring.redis.timeout=30 \ No newline at end of file