What is the role of the @Cacheable annotation in Spring?
Table of Contents
Introduction
The @Cacheable annotation in Spring is a powerful feature used to improve the performance of Spring applications by caching the results of method calls. When applied to a method, it tells Spring to check if the result is already available in the cache before executing the method. If the result is cached, the method execution is skipped, and the cached result is returned. If not, the method is executed, and the result is stored in the cache for future use.
In this guide, we will explore the role of the @Cacheable annotation, how it works, and practical examples of its usage in Spring applications.
How Does the @Cacheable Annotation Work?
1. Basic Concept of Caching
Caching is a technique used to temporarily store frequently accessed data in memory to avoid the overhead of repeatedly fetching or computing the same data. The @Cacheable annotation allows Spring to manage this caching automatically. When you annotate a method with @Cacheable, Spring stores the result in a cache. On subsequent calls with the same parameters, the cached result is returned without executing the method.
2. Using @Cacheable in Spring
To use @Cacheable, you need to configure Spring's cache management feature and specify which method results should be cached.
Steps to Use @Cacheable:
-
Enable Caching in the Spring Configuration:
First, enable caching in your Spring application by adding the@EnableCachingannotation in your configuration class. -
Annotate Methods with @Cacheable:
Apply the@Cacheableannotation to any method whose result you want to cache. You must also specify a cache name where the results will be stored.In this example, the result of
getEmployeeByIdis cached inemployeeCache. On subsequent calls with the sameid, the cached result is returned. -
Configure the Cache:
Spring provides different cache providers, including EhCache, Caffeine, and Hazelcast, among others. You can choose any of these to manage the caching.Example configuration using EhCache:
3. Advanced Features of @Cacheable
-
Custom Cache Key:
By default, the method parameters are used as the cache key. However, you can customize the cache key using thekeyattribute. -
Condition:
You can add a condition to cache results only when certain criteria are met.In this case, the result will only be cached if
idis greater than 10. -
Unless:
Theunlessattribute can be used to prevent caching under specific circumstances.Here, the result will not be cached if the employee's salary is less than 5000.
Practical Examples
Example 1: Basic Caching in a Service Class
In this example, calling getEmployeeById for the first time will take 3 seconds due to the simulated delay. Subsequent calls with the same id will return the cached result immediately.
Example 2: Using Condition for Caching
In this example, the result is only cached if the product price is greater than 100.
Example 3: Custom Cache Key
In this example, the cache key is the orderNumber, which ensures that results are cached based on this value.
Conclusion
The @Cacheable annotation in Spring is a highly effective way to boost the performance of your application by reducing redundant method executions and database calls. It allows you to cache the results of method calls with minimal configuration, ensuring that expensive operations are performed only when necessary. With advanced features like custom cache keys, conditions, and the ability to prevent caching under specific circumstances, @Cacheable provides flexibility and control over caching behavior in your Spring applications.