Skip to content

Commit

Permalink
Add redis cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
GustavoDinizMonteiro committed Oct 29, 2018
1 parent 52f016e commit 9a8b5b1
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 20 deletions.
16 changes: 2 additions & 14 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,12 @@
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
Expand All @@ -55,10 +51,7 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
Expand All @@ -75,11 +68,6 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-mockmvc</artifactId>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/quem/me/ajuda/QuemMeAjudaApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
12 changes: 8 additions & 4 deletions src/main/java/quem/me/ajuda/controllers/StudentController.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Student> create(@Valid @RequestBody Student student) {
return new ResponseEntity<>(this.service.create(student), HttpStatus.CREATED);
}
Expand All @@ -41,8 +44,9 @@ public ResponseEntity<Student> update(@PathVariable Long id, @Valid @RequestBody
}

@GetMapping
public ResponseEntity<List<MinimalStudent>> getAll() {
return ResponseEntity.ok(this.service.getAll());
@Cacheable(Endpoints.STUDENT_ENDPONT)
public Collection<MinimalStudent> getAll() {
return this.service.getAll();
}

@GetMapping(value = "/{id}")
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/quem/me/ajuda/models/MinimalStudent.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/quem/me/ajuda/models/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
10 changes: 9 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -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
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

0 comments on commit 9a8b5b1

Please sign in to comment.