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

24 / 62 topics
23Spring Boot Caching Basics24Configuring Caching with @EnableCaching25Using Cache Annotations like @Cacheable
Tutorials/Spring Boot/Configuring Caching with @EnableCaching
🍃Spring Boot

Configuring Caching with @EnableCaching

Updated 2026-04-20
4 min read

Introduction

In this tutorial, we will explore how to configure caching in a Spring Boot application using the @EnableCaching annotation. Caching is an essential technique for improving the performance of applications by storing the results of expensive operations and reusing them when the same inputs occur again. Spring Boot provides a robust framework for implementing caching with minimal configuration.

Prerequisites

Before we dive into configuring caching, ensure you have the following:

  • Basic understanding of Spring Boot.
  • A Spring Boot project set up in your preferred IDE (e.g., IntelliJ IDEA, Eclipse).
  • Java Development Kit (JDK) 8 or higher installed.

Enabling Caching in Spring Boot

To enable caching in a Spring Boot application, you need to add the @EnableCaching annotation to one of your configuration classes. This annotation tells Spring that it should look for cacheable methods and manage them accordingly.

Step 1: Add Dependencies

First, ensure your pom.xml (for Maven) or build.gradle (for Gradle) includes the necessary dependencies for caching. For this tutorial, we'll use EhCache as our caching provider.

Maven

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>
    <dependency>
        <groupId>org.ehcache</groupId>
        <artifactId>ehcache</artifactId>
    </dependency>
</dependencies>

Gradle

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-cache'
    implementation 'org.ehcache:ehcache'
}

Step 2: Enable Caching

Create a configuration class and annotate it with @EnableCaching.

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

@Configuration
@EnableCaching
public class CacheConfig {
    // Additional cache configuration can be added here if needed
}

Using @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 Service Method

Let's assume we have a service class that fetches user data from a database. We want to cache this data to improve performance.

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

@Service
public class UserService {

    @Cacheable("users")
    public User getUserById(Long id) {
        // Simulate a time-consuming operation, e.g., database query
        System.out.println("Fetching user from the database...");
        return new User(id, "John Doe");
    }
}

In this example, the getUserById method is annotated with @Cacheable("users"). This means that the result of this method will be stored in a cache named "users". If the method is called again with the same id, the cached result will be returned instead of executing the method.

Configuring Cache Manager

Spring Boot provides several built-in cache managers, including EhCache, Caffeine, and ConcurrentMap. You can configure these cache managers by creating a bean in your configuration class.

Example: Configuring EhCache

Here's how you can configure EhCache as your cache manager:

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableCaching
public class CacheConfig {

    @Bean
    public CacheManager cacheManager() {
        return new EhCacheCacheManager(ehCacheFactory().getObject());
    }

    @Bean
    public net.sf.ehcache.CacheManager ehCacheFactory() {
        return net.sf.ehcache.CacheManager.newInstance("classpath:ehcache.xml");
    }
}

In this example, we define a CacheManager bean that uses EhCache. We also specify the location of the EhCache configuration file (ehcache.xml).

Example: ehcache.xml

Here's a simple ehcache.xml configuration:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">

    <cache name="users"
           maxEntriesLocalHeap="1000"
           timeToLiveSeconds="3600"/>

</ehcache>

In this configuration, we define a cache named "users" with a maximum of 1000 entries in the local heap and a time-to-live of 3600 seconds.

Other Cache Annotations

Spring Boot provides several other annotations for caching:

  • @CachePut: Updates the cache without checking if it already exists.
  • @CacheEvict: Removes one or more cache entries based on certain conditions.
  • @Caching: Combines multiple caching operations in a single method.

Example: Using @CachePut and @CacheEvict

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

@Service
public class UserService {

    @Cacheable("users")
    public User getUserById(Long id) {
        // Simulate a time-consuming operation, e.g., database query
        System.out.println("Fetching user from the database...");
        return new User(id, "John Doe");
    }

    @CachePut(value = "users", key = "#user.id")
    public User updateUser(User user) {
        // Update user in the database
        System.out.println("Updating user in the database...");
        return user;
    }

    @CacheEvict(value = "users", key = "#id")
    public void deleteUser(Long id) {
        // Delete user from the database
        System.out.println("Deleting user from the database...");
    }
}

In this example, updateUser updates the cache with the new user data, and deleteUser removes the corresponding entry from the cache.

Best Practices

  1. Cache Only What's Worth Caching: Caching can improve performance but also increases memory usage. Cache only what is frequently accessed and has a high cost to compute or fetch.
  2. Use Appropriate Cache Keys: Choose cache keys wisely to avoid collisions and ensure that different inputs map to different cache entries.
  3. Monitor Cache Performance: Use monitoring tools to track cache hit rates, misses, and evictions. This will help you optimize your caching strategy.
  4. Configure Time-to-Live (TTL) Appropriately: Set a reasonable TTL for cached data to balance between performance and data freshness.

Conclusion

In this tutorial, we learned how to configure caching in a Spring Boot application using the @EnableCaching annotation. We explored how to use the @Cacheable, @CachePut, and @CacheEvict annotations to manage cached data effectively. By following best practices and configuring your cache manager appropriately, you can significantly improve the performance of your Spring Boot applications.

References

  • Spring Framework Documentation on Caching
  • EhCache Documentation

This comprehensive guide should provide you with a solid foundation for implementing caching in your Spring Boot applications.


PreviousSpring Boot Caching BasicsNext Using Cache Annotations like @Cacheable

Recommended Gear

Spring Boot Caching BasicsUsing Cache Annotations like @Cacheable