How can you enable caching in a Spring Boot application?

Category : Interview Questions | Sub Category : Spring Boot Interview Questions | By Prasad Bonam Last updated: 2023-08-04 14:12:55 Viewed : 330


How can you enable caching in a Spring Boot application?

Enabling caching in a Spring Boot application is straightforward. Spring Boot integrates seamlessly with the Spring Frameworks caching abstraction, which allows you to cache method results or data to improve performance and reduce redundant processing.

To enable caching in a Spring Boot application, follow these steps:

  1. Add Caching Dependency: Include the appropriate caching dependency in your pom.xml or build.gradle file. Spring Boot supports various caching providers, such as Ehcache, Redis, Caffeine, etc. You need to add the corresponding caching starter dependency based on your choice.

    For example, to use the Caffeine caching provider, include the following in your pom.xml:

    xml
    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>com.github.ben-manes.caffeine</groupId> <artifactId>caffeine</artifactId> </dependency><dependency>
    <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>com.github.ben-manes.caffeine</groupId> <artifactId>caffeine</artifactId> </dependency>
  2. Enable Caching: In your main Spring Boot application class, annotate it with @EnableCaching to enable caching support.

    java
    import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; @SpringBootApplication @EnableCaching public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } }
  3. Annotate Methods for Caching: To enable caching for specific methods, annotate them with caching annotations, such as @Cacheable, @CachePut, or @CacheEvict.

    • @Cacheable: Used to cache the result of a method invocation based on its parameters. Subsequent calls to the same method with the same parameters will return the cached result instead of executing the method.

    • @CachePut: Used to update the cache with the result of a method invocation. It always executes the method and updates the cache with the new result.

    • @CacheEvict: Used to remove entries from the cache after a method invocation. It allows you to specify when and which cache entries should be removed.

    Example:

    java
    import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; @Service public class MyService { @Cacheable("dataCache") public String fetchDataFromRemoteServer(String key) { // Simulate fetching data from a remote server return "Data for key: " + key; } }

    In this example, the fetchDataFromRemoteServer method will be cached with the name "dataCache." Subsequent calls to this method with the same key parameter will return the cached result without actually executing the method.

  4. Configure Cache Properties: You can configure various caching properties, such as the cache manager, cache names, and cache eviction policies, in the application.properties or application.yml file.

    Example (application.properties):

    properties
    # Configure Caffeine cache properties spring.cache.cache-names=dataCache spring.cache.caffeine.spec=maximumSize=100,expireAfterAccess=600s

    In this example, we have set up a cache named "dataCache" using the Caffeine cache provider with a maximum size of 100 entries and an expiration time of 600 seconds.

With these steps, caching will be enabled in your Spring Boot application. The methods annotated with caching annotations will now store and retrieve results from the cache, which can significantly improve performance for repetitive operations or data retrieval.

Search
Related Articles

Leave a Comment: