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 ehcache
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

Related Courses

Spring Boot Certification Course

Spring Framework Course Training

All in One Data Science Course

Spring boot ehcache

Spring boot ehcache

Introduction to Spring boot ehcache

In spring boot, we have ehcache, which simply means we can implement the caching mechanism in our application. This external or open-source library helps us implement caching in spring boot; this library is written in Java; also, this open source library is very compatible with the javax.cache, this compatibility of ehcache makes it very easy to implement or integrate with spring boot, hibernate, etc. Furthermore, enabling caching in applications increases its performance because it acts as the intermediate storage, ensuring that data is available quickly and quickly. We can easily implement caching where we are requesting similar data, again and again, so instead of going to the database, it will collect and fetch it from the cache; in the coming section, we will see how we can implement it using ehcache in spring boot.

Syntax

As we know, it is an open-source library used to implement the caching mechanism in our application. Also, let’s take a look at the syntax for better understanding and implementation of ehcache in spring boot to see below;

@Configuration
@EnableCaching
public class Demo {
}

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,754 ratings)

As you can see in the above example, we are using the @EnableCaching annotation to enable caching in our project; also, we need to make more configurations to make the caching mechanism work inside our application. This whole configuration we will see in the next section of the tutorial will better understand it.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

How ehcache works in Spring boot?

We already know that to make the ehcache works in spring boot, we have to use its dependency because it is an open-source library that helps us implement the chaching mechanism for us. Also, we have made some more configurations to enable it inside the application, but first, we will check what components are and how ehcache internally works, also one practice example where we see the need for chaching mechanism let’s get stared see below;

1) Ehchache tires: In the case of Ehcache, we have different memory areas where we can easily configure the caching for the application. Suppose we use more than one memory area, then that case area will be arranged in a hierarchical order. We have the lowest and highest order inside this hierarchical order; let’s understand the tier in detail for better understanding, see below;

These are the memory areas supported by ehcahce to implement cache;

a) On-Heap Store: This memory storge uses the heap memory area to implement the ehcahce for us. By implementing the cache for this memory area, garbage collection will also scan it, as we know it is very fast but very limited.

b) Disk store: This memory area uses the hard disk area to implement the cache; this is very slower than the RAM.

c) Off-Heap store: This memory area make use of RAM to cheche the entries, but the garbage collection will not scan this; if we look at it performance-wise, it is fast but a bit slower than the On-Heap store because it performs one more step like moving of cache entries before they can be used.

Below we have the flow chart to represent the different memory areas for ehcache in the spring boot application.

Off-Heap-store

2) Practice usage: we can use caching medium to inverse the perforce of the system, but how does it improve. What it does internally makes a local copy of the data and return it every time when requested by the user. For example, suppose we have one application where a similar type of data is being requested by the user again and again. So we have to call the database and retrieve the record whenever the records are requested; it may reduce the application’s performance and increase the cost of db calls here. So caching provides us the benefit of storing such kinds of data inside the local memory and making use of them whenever needed. Also, this record is often called as the cache entries in general. So this is how it works and benefits the application.

3) demo: let’s take a look at the ehcahce part; in this part, we will take one simple example to make this work in our application and what steps are required to step the caching for application; let get started;

1) First, we have to create the project by using a spring initializer. Fill up all the necessary details and generate the project. Then, import the project inside the editor.

URL :

2) We have to add the dependency for ehchace in the spring boot build file; below is the reference; add this inside your project as well. We will need all these dependencies to make this work.

e.g.:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
<version>2.4.0</version></dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.8.1</version>
</dependency>

Web dependency should also be there in order to run this on the web.

3) Now we have controller try to call the method from it on service see below;

e.g.:

@RestController
@RequestMapping("/demo")
public class DemoController {
@Autowired
private DemoService demoService;
@GetMapping(path = "/get/{num}")
public int getsum(@PathVariable int num) {
demoService.sum(num);
}
}

4) Now, we will create a service in order to get the sum of the number we passed; for reference, see below code;

e.g.:

@Service
public class DemoService {
@Cacheable(
value = "sumCahe",
key = "#num")
public int sum(int num) {
return num + 100;
}
}

5) The main class should be a it is; there should be no changes required.

6) we have to create one lass for cache configuration; for reference, see below;

e.g.:

@Configuration
@EnableCaching
public class demoConfig {
}

7) Now, try to hit the URL we have cerate and run the application to see if it is running fine; you will see below as the output for it.

Output:

Spring boot ehcache output

Conclusion

By using it, we can make the response time of our application fast, which also helps us improve the performance of the overall system. We have seen the configuration required to make this work; it is easy to implement, handle, and understandable by the developers.

Recommended Articles

This is a guide to Spring boot ehcache. Here we discuss the working of the Spring boot ehcache. You may also have a look at the following articles to learn more –

  1. Spring Boot Actuator
  2. Spring Boot Annotations
  3. Spring Boot DevTools
  4. Spring Boot Architecture
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
0 Shares
Share
Tweet
Share
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

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

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

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

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

EDUCBA Login

Forgot Password?

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

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

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

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

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

Let’s Get Started

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