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

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

Spring Boot Caching Basics

Updated 2026-05-15
10 min read

Spring Boot Caching Basics

Introduction

In the world of web applications, performance optimization is crucial. One effective way to enhance the speed and responsiveness of your application is by implementing caching mechanisms. Caching involves storing frequently accessed data in a temporary storage area so that it can be quickly retrieved without having to recompute or fetch it from the original source.

Spring Boot provides built-in support for caching through its spring-boot-starter-cache module, which simplifies the integration of various caching technologies like EhCache, Hazelcast, Redis, and more. In this tutorial, we will explore the basics of caching in Spring Boot, how to enable it, and how to use it to improve the performance of your applications.

Concept

Caching works by storing data that is frequently accessed but expensive to compute or fetch. When a request comes in for the cached data, the application first checks if the data is available in the cache. If it is, the application retrieves the data from the cache instead of generating it again. This reduces the load on your system and improves response times.

Spring Boot's caching abstraction allows you to easily switch between different caching providers without changing much of your code. It provides a simple API for defining cacheable operations using annotations like @Cacheable, @CachePut, and @CacheEvict.

Examples

Step 1: Add Dependencies

First, you need to add the Spring Boot Starter Cache dependency to your project. If you are using Maven, add the following to your pom.xml:

XML
1<dependency>
2 <groupId>org.springframework.boot</groupId>
3 <artifactId>spring-boot-starter-cache</artifactId>
4</dependency>

If you are using Gradle, add this to your build.gradle:

groovy
1dependencies {
2 implementation 'org.springframework.boot:spring-boot-starter-cache'
3}

Step 2: Enable Caching

To enable caching in your Spring Boot application, you need to add the @EnableCaching annotation to one of your configuration classes. This tells Spring Boot to activate its caching infrastructure.

Java
1import org.springframework.cache.annotation.EnableCaching;
2import org.springframework.context.annotation.Configuration;
3
4@Configuration
5@EnableCaching
6public class CacheConfig {
7}

Step 3: Create a Service with Caching

Now, let's create a simple service that uses caching. We'll use the @Cacheable annotation to cache the results of a method.

Java
1import org.springframework.cache.annotation.Cacheable;
2import org.springframework.stereotype.Service;
3
4@Service
5public class MyService {
6
7 @Cacheable("myCache")
8 public String getData(String key) {
9 // Simulate an expensive operation
10 try {
11 Thread.sleep(2000);
12 } catch (InterruptedException e) {
13 Thread.currentThread().interrupt();
14 }
15 return "Data for " + key;
16 }
17}

In this example, the getData method is annotated with @Cacheable("myCache"). This means that the result of this method will be cached under the name "myCache". If the same method is called again with the same key within a short period, the cached result will be returned instead of executing the method again.

Step 4: Test the Caching

To test the caching mechanism, you can create a simple REST controller that invokes the getData method.

Java
1import org.springframework.beans.factory.annotation.Autowired;
2import org.springframework.web.bind.annotation.GetMapping;
3import org.springframework.web.bind.annotation.RequestParam;
4import org.springframework.web.bind.annotation.RestController;
5
6@RestController
7public class MyController {
8
9 @Autowired
10 private MyService myService;
11
12 @GetMapping("/data")
13 public String getData(@RequestParam String key) {
14 return myService.getData(key);
15 }
16}

Now, when you run your application and access the /data endpoint with a specific key, the response time will be slow initially due to the simulated expensive operation. However, subsequent requests with the same key will be much faster because the result is retrieved from the cache.

Terminal
$ curl http://localhost:8080/data?key=test
Data for test
# Wait 2 seconds for the first request
$ curl http://localhost:8080/data?key=test
Data for test
# This time, it's almost instant due to caching

What's Next?

In this tutorial, we covered the basics of caching in Spring Boot and how to use it to improve performance. In the next section, we will explore more advanced configurations and options available in Spring Boot's caching abstraction, such as configuring cache managers, handling cache eviction, and integrating with different caching providers.

By understanding these concepts, you can effectively optimize your Spring Boot applications for better performance and scalability.


PreviousCSRF Protection in Spring SecurityNext Configuring Caching with @EnableCaching

Recommended Gear

CSRF Protection in Spring SecurityConfiguring Caching with @EnableCaching