EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Spring Tutorial Spring Boot Cache
Secondary Sidebar
Spring Tutorial
  • Spring Boot
    • What is Spring Boot
    • Spring Boot flyway
    • Spring Boot framework
    • Spring Boot Logback
    • Spring Boot actuator endpoints
    • Spring Boot gRPC
    • Spring Boot jdbctemplate example
    • Spring Boot ehcache
    • Spring Boot Architecture
    • Spring Boot Port
    • Introduction of spring boot
    • Spring Boot ide
    • Spring Boot Netty
    • Spring Boot ORM
    • Spring Boot Versions
    • Spring Boot JUnit
    • Spring Boot Keycloak
    • Spring Boot gradle
    • Spring Boot Lombok
    • Spring Boot autowired
    • Spring Boot bean
    • Spring Boot hibernate
    • Spring Boot integration test
    • Spring Boot jdbc
    • Spring Boot MongoDB
    • Spring Boot postgresql
    • Spring Boot rest
    • Spring Boot swagger
    • Spring Boot thymeleaf
    • Spring Boot Unit Test
    • Spring Boot Webflux
    • Spring Boot webclient
    • Spring Boot kubernetes
    • Spring Boot Properties
    • Spring Boot Validation
    • Spring Boot Feature
    • Spring Boot Application
    • Spring Boot email
    • Spring Boot MVC
    • Spring Boot Exception Handling
    • Spring Boot Starter Parent
    • Spring Boot Docker
    • Spring Boot Logging
    • Spring Boot Query
    • Spring Boot Multiple Data Sources
    • Spring Boot Basic Authentication
    • Spring Boot Test
    • Spring Boot jwt
    • Spring Boot Liquibase
    • Spring Boot Prometheus
    • Spring Boot debug
    • Spring Boot GraalVM
    • Spring Boot Batch
    • Spring Boot controller
    • Spring Boot CLI
    • Spring Boot file upload
    • Spring Boot interceptor
    • Spring Boot Service
    • Spring Boot Configuration
    • Spring Boot Datasource Configuration
    • Spring Boot Annotations
    • Spring Boot Starter We
    • Spring Boot Actuator
    • Spring Boot DevTools
    • Spring Boot Repository
    • Spring Boot Dependencies
    • Spring Boot Path Variable
    • Spring Boot Microservices
    • Spring Boot Run Command
    • Spring Boot application.properties
    • Spring Boot Transaction Management
    • Spring Boot Banner
    • Spring Boot JPA
    • Spring Boot Change Port
    • Spring Boot RestTemplate
    • Spring Boot cors
    • Spring Boot HTTPS
    • Spring Boot OAuth2
    • Spring Boot Profiles
    • Spring Boot Interview Questions
    • Spring Boot filter
    • Spring boot logging level
    • Spring Boot Cache
    • Spring Boot Advantages
    • Spring Boot Scheduler
    • Spring Boot Initializr
    • Spring Boot Maven
    • Spring Boot Admin
    • Spring Boot Tomcat
    • Spring Boot WebSocket
    • Spring Boot Executable Jar
    • Spring Boot CommandLineRunner
    • Spring Boot DataSource
    • Spring Batch Scheduler
    • Spring Batch Example
    • Spring Batch Tasklet
    • Spring Batch Admin
    • Spring Batch
    • Spring Boot Qualifier
    • Spring Boot War
    • Spring Boot Test Configuration
  • Spring
    • What is Spring Framework?
    • Spring Architecture
    • What is Spring Integration?
    • IoC Containers
    • What is AOP?
    • Spring Modules
    • Spring Batch Processing
    • Spring Batch Partitioner
    • Spring Batch Job
    • Spring AOP
    • Spring Expression Language
    • Dependency Injection in Spring
    • Spring Batch Architecture
    • Spring framework Interview Questions
  • Spring Cloud Basics
    • What is Spring Cloud
    • Spring Cloud Contract
    • Spring Cloud Components
    • Spring Cloud Version
    • Spring Cloud Data Flow
    • Spring cloud stream
    • Spring Cloud Dependencies
    • Spring cloud microservices
    • spring cloud gateway
    • Spring Cloud Config
    • Spring Cloud Kubernetes
    • Spring Cloud Sleuth

Spring Boot Cache

Spring Boot Cache

Introduction to Spring Boot Cache

Spring boot provides us strong caching method which can be used to prevent the number of executions of a particular code. By using a cache, we can store the data into a disk or the memory itself, which prevents us from the unnecessary access to a similar type of data in the code. A cache can improve the performance of the code by reducing the number of execution for the same data again and again with the same information. In spring boot, to implement this concept, we have caching abstraction in general, which is easily used to implement the cache mechanism.

Syntax of Spring Boot Cache

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

As we know that we need to follow the caching abstraction to use this, we will have a closer look at the syntax for we can configure caching in the spring boot application.

@Cacheable(value="your cache name", key="name of key")
public void Method_name()
{
// logic goes here //
}

As you can see in the above line of syntax, we have used @Cacheable annotation, which is used to define the method as cache method in spring boot. So it will cache the mentioned key in the parameter and return us the result from the cache if the key is the same for that and hence prevent the execution time.

Let’s take a look at the sample piece of the syntax of the cache configuration in spring-boot:

Example:

@Cacheable(value="cachedemo", key="#rollNo")
public String employeedetail()
{
// logic goes here. //
}

How does Cache work in Spring Boot?

As of now, we already know that cache can prevent the number of execution for the same record or the same key by storing the data into memory or disk. Also, it helps us to increase the application performance as well. We can easily implement this in the spring boot application by following the simple steps.

Here we will first see the step required and the code changes that we have to make in order to cache the data into the memory or disk in our application.

1. @EnableCaching

First, we need to annotate our main spring boot class with this annotation. This annotation will simply create a cache manager for us. Suppose you have not yet created any cachemanager instance to create an in-memory cachemanager for us by using the HashMap from Java. So this annotation is required to setup the cache mechanism in the spring boot application.

Let’s taken a look at the syntax required to configure this:

@SpringBootApplication
@EnableCaching
public class CachingApplicationDemo
{
public static void main(String[] args)
{
SpringApplication.run(CachingApplicationDemo.class, args);
}
}

As you can see in the above piece of code, we have used to @EnableCaching annotation; also, in order to use this, we have to import the below-mentioned package into our application like below:

import org.springframework.cache.annotation;

2. @Cacheable

This annotation can be used if you want to enable the cache at the method level in an application; also, we can define the parameter that will create a cache for us with some specified name and key. This key is required to identify the records from the cache list uniquely.

Let’s see its syntax how we can use this while programming:

@Cacheable(value="cacheempdetails", key="#empid")
public List<Employee> employeeData()
{
// our code will goes here //
return result;
}

Here as you can see, we have defined the name of the cache by using the value attribute, which means we can use the cache by this name. Also, we have given one key here to identify the records uniquely.

3. @Caching

We can use this annotation at the method level; it is mainly used when we required two annotations such as cacheevict and cacheput.

Below see the syntax to use this while programming:

@Caching(evict = {@CacheEvict("some value"), @CacheEvict(value="value", key="your key") })
public String msg(Employee emp)
{
// logic goes here //
}

We can use the cacheevict annotation on the method, and it is used to remove or delete the unused data from the cache. if we want to delete all entries from the cache, then we can use all entries attribute of the cacheevict by mentioning its value as true. @cacheput is also used on the methods; as the name suggests, it is used to update the cache, and it will not disturb the method execution that is currently being going on. Both this annotation are easy to use and handle in spring boot application.

Steps are mentioned below to make use of cache in spring boot application, also mentioned the required dependency; you can use this as per the build tool you are using in your application to run and build the application we have added for maven.

1. We need to add the following dependency in our pom.xml file in order to use the above-defined cache annotations in the spring boot application.

Example:

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

2. you can create the spring boot application by using the spring initializer online and extract the zip.

3. Import the project into your editor and start making the configuration and changes which is required to cache the data.

Conclusion

By the use of a cache, we can store the frequent access data into the memory or disk, which can reduce the number of execution; this execution can be so expensive, which can further impact the performic of the application, so we can easily configure this caching mechanism in our code by simple steps and start using it.

Recommended Articles

This is a guide to Spring Boot Cache. Here we discuss the introduction and how cache works in spring boot? For better understanding. You may also have a look at the following articles to learn more –

  1. Maven Repository Spring
  2. Spring Boot DevTools
  3. Spring AOP
  4. Spring Cloud Components
Popular Course in this category
Spring Boot Training Program (2 Courses, 3 Project)
  2 Online Courses |  3 Hands-on Projects |  22+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Spring Framework Training (4 Courses, 6 Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more