Spring Boot Caching Introduction
In this article, I am going to discuss Spring Boot Caching Introduction. Please read our previous article where we discussed Spring Boot JDBC with Examples.
What is Caching?
Caching is done in programs to keep regularly accessed data in memory, rather than having to retrieve them from the storage device, such as SSD or HDD. This is because reading from SSD or HDD is slower and hence time-consuming.
Data access from memory is faster in comparison to fetching data from the database. It reduces both monetary costs and opportunity costs. Hence, data that does not change frequently needs to be stored in the cache. The cache is a part of temporary memory (RAM). It lies between the application and the persistence database. It stores recently used data that reduces the number of database hits.
In Spring Boot, cache abstraction is a mechanism that allows consistent use of various caching methods with minimal impact on the code.
What is Cache Abstraction?
The cache abstraction mechanism applies to Java methods. The main objective of using cache abstraction is to reduce the number of executions based on the information present in the cache. It applies to expensive methods such as CPU or IO bound.
Every time, when a method invokes, the abstraction applies a cache behavior to the method. It checks whether the method has already been executed for the given argument or not.
- If yes, the cached result is returned without executing the actual method.
- If no, first, the method executes, and the result is cached and returned to the user.
Following need to be defined for cache abstractions:
- Cache Declaration: It identifies the methods that need to be cached.
- Cache Configuration: The backing cache where the data is stored and read from.
Types of Caching
There are four types of caching are as follows:
- In-memory Caching
- Database Caching
- Web Server Caching
- CDN Caching
In-memory caching increases the performance of the application. This kind of caching is frequently used. Memcached and Redis are examples of in-memory caching. Redis is an in-memory, distributed, and advanced caching tool that allows a backup and restore facility. Also, we can manage cache in distributed clusters.
Database caching is a mechanism that generates web pages dynamically by fetching data from the database. It is used in a multi-tier environment that involves clients, web application servers, and databases. It improves scalability and performance by distributing a query workload. The most popular database caching is the first-level cache of Hibernate.
Web Server Caching
Web server caching is a mechanism that stores data for reuse. For example, a copy of a web page served by a web server. It is cached for the first time when a user visits the page. If the user requests the same next time, the cache serves a copy of the page. Web server caching enhances the page delivery speed and reduces the work to be done by the backend server.
Cache vs. Buffer
- Based on Least Recently Used.
- Lives for a long period.
- Is more often read from.
- Stores actual contents of the file.
- Improves read performance.
- Is based on First-In-First-Out.
- An in-memory raw block I/O buffer.
- Lives for a short period.
- Buffers are typically written to.
- Stores the metadata of the file.
- Improves write performance.
Caching in Spring Boot
It is a class-level annotation. We can enable caching in Spring Boot by using this annotation. It is defined in org.springframework.cache.annotation package. It is used together with @Configuration class. Autoconfiguration can be used to enable caching and set up a CacheManager (if one does not exist already). It scans for a specific provider, and if it does not find one, it creates an in-memory cache using concurrent HashMap.
It is a class-level annotation that provides a common cache-related setting. It tells Spring where to store the cache for the class. When we annotate a class with the annotation, it provides a set of default settings for any cache operation defined in that class. Using the annotation, we need not declare things multiple times.
It is a method-level annotation. It is used when we want to remove unused data from the cache. We can also specify a key or condition into it. If we want wide cache eviction, the @CacheEvict annotation provides a parameter called allEntries. It evicts all entries rather than one entry based on the key. One important point about @CacheEvict annotation is that it can be used with void methods because the method acts as a trigger. It avoids return values. On the other hand, the annotation @Cacheable requires a return value that adds/updates data in the cache.
It is a method-level annotation. It is used when we want to update the cache without interfering with the method execution. It means the method will execute, and its result will be placed into the cache. It supports the attributes of @Cacheable annotation. A slight difference between the @Cacheable and @CachePut annotations is that the @Cacheable annotation skips the method execution while the @CachePut annotation runs the method and put the result into the cache.
This is a method-level annotation. It is used when both @CachePut and @CacheEvict annotations are required together. We have to use this, rather than two annotations because Java does not allow two annotations for the same method.
It is a method-level annotation. It defines a cache for a method’s return value. Spring manages the requests and responses of the method to the cache that is specified in the annotation attribute. The @Cacheable annotation contains more options. For example, we can provide a cache name by using the cacheNames attribute. We can also specify the key attribute of the annotation that uniquely identifies each entry in the cache. If we do not specify the key, Spring uses the default mechanism to create the key.
In the next article, I am going to discuss Spring Boot Caching Examples. Here, in this article, I try to explain Spring Boot Caching Introduction. I hope you enjoy this Spring Boot Caching article.