codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🍃

Spring Boot

25 / 62 topics
23Spring Boot Caching Basics24Configuring Caching with @EnableCaching25Using Cache Annotations like @Cacheable
Tutorials/Spring Boot/Using Cache Annotations like @Cacheable
🍃Spring Boot

Using Cache Annotations like @Cacheable

Updated 2026-04-20
4 min read

Using Cache Annotations like @Cacheable

Introduction

In modern web applications, performance optimization is crucial for providing a seamless user experience. One effective way to improve performance is by implementing caching mechanisms. Caching helps reduce the load on your database and speeds up response times by storing frequently accessed data in memory.

Spring Boot provides several annotations to simplify the implementation of caching, with @Cacheable being one of the most commonly used. This tutorial will guide you through using @Cacheable and other related cache annotations in a Spring Boot application.

Prerequisites

Before diving into the implementation, ensure that you have:

  • A basic understanding of Spring Boot.
  • Java Development Kit (JDK) 8 or later installed.
  • An Integrated Development Environment (IDE) like IntelliJ IDEA or Eclipse.
  • Maven or Gradle for building and managing dependencies.

Setting Up a Spring Boot Application

First, create a new Spring Boot application. You can use Spring Initializr (https://start.spring.io/) to generate the project structure. Choose the following options:

  • Project: Maven Project
  • Language: Java
  • Spring Boot: Latest stable version
  • Project Metadata:
    • Group: com.example
    • Artifact: caching-demo
    • Name: caching-demo
    • Description: Demo project for Spring Boot caching
    • Package name: com.example.cachingdemo
  • Packaging: Jar
  • Java: 8 or later

Add the following dependencies:

  • Spring Web
  • Spring Data JPA
  • H2 Database (for simplicity, you can replace it with any other database)

Download and import the project into your IDE.

Configuring Caching in Spring Boot

To enable caching in your Spring Boot application, you need to add the @EnableCaching annotation to one of your configuration classes. This tells Spring that you want to use caching capabilities.

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableCaching
public class CacheConfig {
}

Next, configure the cache manager. For simplicity, we'll use an in-memory cache (SimpleCacheManager). In a production environment, you might want to use more robust solutions like Redis or Ehcache.

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

@Configuration
@EnableCaching
public class CacheConfig {

    @Bean
    public CacheManager cacheManager() {
        SimpleCacheManager cacheManager = new SimpleCacheManager();
        cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("books")));
        return cacheManager;
    }
}

Using the @Cacheable Annotation

The @Cacheable annotation is used to indicate that the result of a method should be cached. If the method is called again with the same parameters, the cached result will be returned instead of executing the method.

Example: Caching Book Data

Let's assume you have a simple REST API for managing books. We'll cache book data using @Cacheable.

First, define a Book entity:

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Book {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String title;

    private String author;

    // Getters and setters
}

Next, create a repository interface for Book:

import org.springframework.data.jpa.repository.JpaRepository;

public interface BookRepository extends JpaRepository<Book, Long> {
}

Now, implement a service class with caching:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class BookService {

    @Autowired
    private BookRepository bookRepository;

    @Cacheable("books")
    public Book getBookById(Long id) {
        return bookRepository.findById(id).orElse(null);
    }
}

In this example, the getBookById method is annotated with @Cacheable("books"). This means that when a book is fetched by its ID, the result will be cached under the "books" cache.

Example: Caching Book List

You can also cache lists of data. For instance, caching all books:

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {

    @Autowired
    private BookRepository bookRepository;

    @Cacheable("books")
    public List<Book> getAllBooks() {
        return bookRepository.findAll();
    }

    @Cacheable(value = "books", key = "#id")
    public Book getBookById(Long id) {
        return bookRepository.findById(id).orElse(null);
    }
}

In this case, getAllBooks caches the entire list of books under the "books" cache.

Using Other Cache Annotations

Spring Boot provides several other cache annotations to manage caching behavior:

  • @CachePut: Updates the cache with the result of a method execution.
  • @CacheEvict: Removes one or more entries from the cache.
  • @Caching: Combines multiple caching operations in a single method.

Example: Using @CachePut and @CacheEvict

Let's add methods to update and delete books, ensuring that the cache is updated accordingly:

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;

@Service
public class BookService {

    @Autowired
    private BookRepository bookRepository;

    @Cacheable("books")
    public List<Book> getAllBooks() {
        return bookRepository.findAll();
    }

    @Cacheable(value = "books", key = "#id")
    public Book getBookById(Long id) {
        return bookRepository.findById(id).orElse(null);
    }

    @CachePut(value = "books", key = "#book.id")
    public Book updateBook(Book book) {
        return bookRepository.save(book);
    }

    @CacheEvict(value = "books", allEntries = true)
    public void deleteBook(Long id) {
        bookRepository.deleteById(id);
    }
}
  • @CachePut is used in the updateBook method to update the cache with the new book data.
  • @CacheEvict is used in the deleteBook method to remove all entries from the "books" cache since deleting a book affects the entire list.

Best Practices

  1. Choose Appropriate Cache Keys: Use unique and meaningful keys for caching. The default key generation might not always be suitable.
  2. Cache Only Expensive Operations: Caching should be used for operations that are expensive in terms of computation or database access.
  3. Monitor Cache Performance: Regularly monitor cache hit/miss ratios to ensure that your caching strategy is effective.
  4. Use Conditional Caching: Use condition and unless attributes in annotations to control when data should be cached.
  5. Evict Stale Data: Ensure that stale data is evicted from the cache when necessary, especially after updates or deletions.

Conclusion

Caching is a powerful tool for optimizing performance in Spring Boot applications. By using annotations like @Cacheable, @CachePut, and @CacheEvict, you can easily manage caching behavior without writing complex code. This tutorial has covered the basics of setting up caching, using cache annotations, and best practices for effective caching.

Remember to test your caching implementation thoroughly to ensure that it behaves as expected under different scenarios. Happy coding!


PreviousConfiguring Caching with @EnableCachingNext Spring Boot Actuator Basics

Recommended Gear

Configuring Caching with @EnableCachingSpring Boot Actuator Basics