Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Spring Boot FAQ: Top Questions

39. How does caching work in Spring Boot?

Spring Boot supports caching abstraction which can be backed by various providers like EhCache, Redis, or simple memory cache.

πŸ—ΊοΈ Setup:

  1. Enable caching using @EnableCaching.
  2. Use @Cacheable, @CachePut, @CacheEvict annotations.

πŸ“₯ Example:

@Cacheable("books")
public Book findBookById(String id) {
  simulateSlowService();
  return bookRepository.findById(id);
}

πŸ† Expected Output:

Subsequent calls return cached data without delay.

πŸ› οΈ Use Cases:

  • Improving performance by reducing repeated calls.
  • Caching frequently accessed data like product lists or settings.