In this post, we will delve into the utilization of caching in a Spring Boot application. Enhancing application performance is a prevalent concern in software development, prompting the adoption of various mechanisms. Among these, caching emerges as a valuable tool for boosting performance. By storing the outcomes of executions, caching mitigates the need for redundant processing, contributing to more efficient application performance.

Handling the cache in a Spring Boot application can be a complex undertaking, as improper usage may lead to unpredictable application outcomes. The initial challenge involves gaining a comprehensive understanding of the cache, encompassing its types, usage methodologies, and suitable scenarios for implementation. In this context, we will delve into the intricacies of caching with a straightforward example, providing detailed insights into its utilization.

What is Cache

A cache serves as a compact, temporary memory storage solution designed to retain frequently accessed data, thereby preventing the need for the application to execute redundant processes. This mechanism is instrumental in enhancing application performance by mitigating the repetition of executions with identical input data, yielding consistent output. Whether implemented as a rapid-access software or hardware component, caches are adept at swiftly delivering data in response to requests. Their versatile application extends across various layers of the system, effectively curbing repetitive executions and facilitating expedited data retrieval.

Types of Cache

In a Spring Boot application, caches are categorized into four types, each classified based on the location of the cache within the application.

  1. In memory cache
  2. Database cache
  3. Webserver cache
  4. CDN (Content Delivery Network) cache

1. In-memory Cache

The in-memory cache is instrumental in enhancing application performance by storing frequently accessed data in a high-speed memory region. When this data is requested, the in-memory cache facilitates rapid retrieval, delivering quick access. Various techniques, such as storing recently used or repeatedly accessed data, are employed to efficiently manage the storage and retrieval processes within the in-memory cache.

When dealing with a substantial volume of data, a common technique involves transferring less critical data to persistent storage. In-memory caching solutions like Memcached and Redis are frequently employed for this purpose. In-memory caching operates by storing data in the form of key-value pairs, where each key is unique and serves as the identifier for retrieving the corresponding value from the in-memory caches.

2. Database Cache

The Database Cache serves as an intermediary storage between the application and the database. This type of caching is designed to enhance data retrieval from databases by mitigating the need for continuous database connections. Unaltered values in the database are efficiently served from the database cache, relieving the application from frequent database access for the same data.

In the application, only modified data in the database is accessed. ORM (Object-Relational Mapping) tools like Hibernate leverage the database cache. The first level of cache in Hibernate plays a crucial role in improving application performance by minimizing the necessity for frequent database calls.

3. Web Server Cache

The Web Server Cache is typically situated between user requests and the application server. Positioned on the web server, this cache stores requested pages. When a user requests the same page again, the web server serves the page directly from the cache, eliminating the need to connect to the application server for the execution of static pages. This approach significantly enhances web page response times, contributing to a more efficient and faster user experience.

4. CDN (Content Delivery Network) cache

Generally situated outside of the application or application servers, the CDN (Content Delivery Network) cache is configured in client-side areas such as the browser, operating system, or network. When a request is made, it initially interacts with the CDN cache. If the required data is present in the cache, it is promptly retrieved; otherwise, the application is informed to provide the data. This setup ensures efficient content delivery and optimizes the overall performance of the system.

When data is cached in the browser, subsequent requests for the same data are served directly from the browser’s cache memory rather than being fetched from the application. This is particularly beneficial for static content such as JavaScript files, CSS files, images, and videos. Such static data is stored in the browser’s memory or in a designated storage area, such as Amazon S3 buckets, optimizing the retrieval process and contributing to a faster and more responsive user experience.

Annotations used for Spring Boot Cache

The Spring Boot Starter Cache simplifies the inclusion of necessary JARs to enable Spring Boot cache functionality in a Spring Boot application. Spring Boot offers a variety of cache annotations designed for use at different levels. These annotations are user-friendly and effectively manage the entire cache mechanism. The comprehensive set of Spring Boot cache annotations will be presented here for easy reference and implementation.

1. @EnableCaching Annotation

The @EnableCaching annotation serves to activate the caching mechanism within a Spring Boot application. If caching is not explicitly enabled using this annotation, other cache-related annotations will not take effect. This annotation is typically applied to the main class of the Spring Boot application. @EnableCaching facilitates caching and presents a HashMap in key-value format to store cached values. It’s important to note that the default concurrent HashMap can be replaced with any alternative cache implementation as needed.

2. @CacheConfig Annotation

The @CacheConfig annotation is employed at the class level to establish class-level caches. This annotation generates the default cache implementation that stores values in key-value format. By using @CacheConfig at the class level, it avoids the need to declare multiple cache configurations in class methods. Since the caching mechanism is enabled at the class level, there’s no necessity to explicitly declare caching at the method level.

3. @Cacheable Annotation

The @Cacheable annotation is applied at the method level to facilitate caching within a class’s methods. This annotation caches the return value of the method based on its parameters. When the method is invoked multiple times with the same parameters, it retrieves the value from the cache instead of executing the method and returning the value anew. The @Cacheable annotation offers various parameters, including value, key, conditional caching, and more, providing flexibility in configuring caching behavior.

4. @CacheEvict Annotation

The @CacheEvict annotation, applied at the method level, is designed to clear all cached values. Utilized for removing cached data, this annotation has the capability to eliminate one or more cache elements. It’s important to note that the @CacheEvict annotation is specifically applicable to methods with a return type of void. The method serves as a trigger, initiating the removal of the specified cache element(s).

5. @CachePut Annotation

For updating or modifying the cache, the @CachePut annotation is used. When the key does not exist, this annotation adds the specified value to the cache. If the key is already present in the cache, the value associated with that key will be updated, and the previous value will be replaced by the new one. Typically, the @CachePut annotation is applied to methods responsible for updating values in the persistent layer. Consequently, if data in the underlying layer undergoes modification, the cache is appropriately updated.

6. @Caching Annotation

The @Caching annotation is utilized when a combination of two annotations, namely @CacheEvict and @CachePut, is desired. This annotation enables the simultaneous application of multiple annotations to a single method, providing flexibility and customization in cache-related behaviors.

Leave a Reply