Category: java
what is redis
Published on 16 Jul 2026
Explanation
Redis is an in-memory data store used for caching frequently accessed data. Caching reduces database queries and significantly improves REST API performance.
Code:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
Explanation
Enable caching in the Spring Boot application by using the @EnableCaching annotation. This activates Spring's cache abstraction.
Code:
@SpringBootApplication
@EnableCaching
public class Application {
}
Explanation
Use the @Cacheable annotation to cache the results of frequently executed methods. If the data is already in Redis, the database is not queried again.
Code:
@Cacheable("students")
public List<Student> getStudents() {
return repository.findAll();
}
Explanation
Use the @CachePut annotation to update the cache whenever a record is modified. This keeps cached data synchronized with the database.
Code:
@CachePut(value = "students", key = "#student.id")
public Student updateStudent(Student student) {
return repository.save(student);
}
Explanation
Use the @CacheEvict annotation to remove outdated cache entries after deleting or updating records. This prevents stale data from being returned.
Code:
@CacheEvict(value = "students", allEntries = true)
public void deleteStudent(Long id) {
repository.deleteById(id);
}