Category: spring_boot
Caching REST API Responses with Redis
Published on 08 Jul 2026
Explanation
Redis stores frequently accessed data in memory to improve API performance.
Code:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
Explanation
Enable caching in Spring Boot.
Code:
@SpringBootApplication
@EnableCaching
public class Application{}
Explanation
Cache API responses using @Cacheable.
Code:
@Cacheable("students")
public List<Student> getStudents(){
return repository.findAll();
}
Explanation
Update cache when data changes.
Code:
@CachePut("students")
public Student update(Student student){
return repository.save(student);
}
Explanation
Remove cache entries after deleting records.
Code:
@CacheEvict(value="students",allEntries=true)
public void delete(Long id){
repository.deleteById(id);
}