Caching in spring boot app:
Caching can be used to provide a performance boost to your application users by avoiding the business logic processing involved again and again, load on your DB, requests to external systems if the users request data that's not changed frequently
Different types of caching:
We'll be focusing more on in-memory caching in this post i listed other options available to have an idea.
- In-memory caching
- You'll have a key-value data stores that stores the response of the request after it is served for the first time
- There are multiple systems like Redis, Memcached that do this distributed caching very well
- By default Spring provides concurrent hashmap as default cache, but you can override CacheManager to register external cache providers.
- Database caching
- Web server caching
Dependencies needed:
Maven
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> <version>2.5.0</version> </dependency>
Gradle
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-cache', version: '2.5.0'
Here are some important annotations used to configure caching in Spring:
@EnableCaching
- Enables caching on the application
@Cacheable
- Use it on methods to let spring know that the response of the method are cacheable
- You can specify the cache in the attribute and Spring will take care managing the request and response of the method into the specified cache. Example - @Cacheable("todos")
- You can also specify a key to use for the cache similar to the one below
@Cacheable(value = "todos", key = "#id") @Override public String getTodoById(int id) {
- Conditional caching - You can specify a condition to determine when to store the response in cache and when not to
@Cacheable(value="todos", condition="#user.subscription = premium") public Todo findTodoForUser (User user)
Now Let's get to some code and see Spring caching in action. Firstly, let's take a look at some refactoring I did from our previous code
- I moved a lot of code from controller to service which keeps all the business logic and for now handles data within memory as well for us
@Service
package com.artifactsbyrake.restdemo.service.impl; import com.artifactsbyrake.restdemo.service.TodoService; import org.springframework.cache.annotation.Cacheable; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; import org.springframework.util.ObjectUtils; import org.springframework.web.server.ResponseStatusException; import org.springframework.web.servlet.support.ServletUriComponentsBuilder; import java.net.URI; import java.util.HashMap; import java.util.Map; import java.util.stream.IntStream; @Service public class TodoServiceImpl implements TodoService { private int seq = 1; private Map<Integer, String> todos = createDefaultTodos(); private Map<Integer, String> createDefaultTodos() { Map todoMap = new HashMap(); IntStream .range(seq, 5) .forEach(i -> { seq = i; String todo = "Todo - " + seq; todoMap.put(seq, todo); }); return todoMap; } @Cacheable(value = "todos", key = "#id") @Override public String getTodoById(int id) { System.out.print("##### FETCHING TODOS FOR ID - " + id + "\n"); if (id < 0) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid ID - Please provide a valid ID and retry"); if (!todos.containsKey(id)) throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No todo found with ID"); return todos.get(id); } @Cacheable("todos") @Override public Map getTodosList() { System.out.print("##### FETCHING LIST OF TODOS ##### \n"); return todos; } @Override public Object addTodo(String todo) { if (ObjectUtils.isEmpty(todo)) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Empty todo"); todos.put(++seq, todo + " - " + seq); URI todoLoc = ServletUriComponentsBuilder.fromCurrentRequest() .path("/{id}") .buildAndExpand(seq) .toUri(); return todoLoc; } @Override public void deleteTodo(int id) { if (id < 0) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid ID - Please provide a valid ID and retry"); if (!todos.containsKey(id)) throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No todo found with ID"); todos.remove(id); } }
Notice the @Cacheable annotation that's been added to specify to the cache which methods response needs to be cached
@SpringBootApplication
package com.artifactsbyrake.restdemo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; @EnableCaching @SpringBootApplication public class RestdemoApplication { public static void main(String[] args) { SpringApplication.run(RestdemoApplication.class, args); } }
@EnableCaching has been added to let Spring know that it needs to enable caching functionality on this application
@Controller
package com.artifactsbyrake.restdemo.controller; import com.artifactsbyrake.restdemo.service.TodoService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.net.URI; import java.util.Map; @RestController @RequestMapping("/api/1/0") public class TodoController { @Autowired TodoService todoService; @GetMapping("/hello") public String sayHello() { return "Hello Rest demo"; } @GetMapping("/todos") public ResponseEntity<Map> getListOfTodos() { System.out.println("%%%%%% Finding ALL TODOS"); return ResponseEntity.ok(todoService.getTodosList()); } @GetMapping("/todos/{id}") public ResponseEntity<String> getTodo(@PathVariable int id) { System.out.println("%%%%%% Finding TODO for id - " + id); return ResponseEntity.ok(todoService.getTodoById(id)); } @PostMapping("/todos") public ResponseEntity<Object> addTodo(@RequestBody String todo) { return ResponseEntity.created((URI) todoService.addTodo(todo)).build(); } @DeleteMapping("/todos/{id}") public ResponseEntity<Object> deleteTodo(@PathVariable int id) { return ResponseEntity.noContent().build(); } }
Above is our refactored controller with lot less code I used @Autowired to inject the service to get a handle of TodoService inside controller. Also, notice the print statements i added in TodoController and TodoServiceImpl we'll use these to determine if our cache is working or not.
Time to run and test:
We enabled cache for the following two endpoints. Let us take a look at the log and observe the print statements while making continuous requests for the same endpoint
Endpoint#1: http://localhost:9000/api/1/0/todos
You can see in the log above that controller print statement is printed every-time where as print statement within service is printed only once that is for the first time when we process the request and response is then stored in the cache and served from cache for the following requests. This will be super helpful if you need to avoid network calls if you need to talk to external service or DB
Endpoint#2: http://localhost:9000/api/1/0/todos/{id}
Console log:
Code Reference - github
Happy Coding 👨💻
Comments
Post a Comment