Updated March 30, 2023
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 {
}
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.
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.
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:
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 –