1

*Ehcache3 not working with spring boot - I tried out with approach given below. Spring boot never caches the value mentioned in the component.It is getting called n - no of times no matter the cache is enable or not. In the logs it shows cache is added to cache manager but thats not the case here

ehcache.xml

<ehcache:config>
  <ehcache:cache-template name="myDefaultTemplate">
    <ehcache:expiry>
      <ehcache:none/>
    </ehcache:expiry> 
  </ehcache:cache-template>

  <ehcache:cache alias="customer" uses-template="myDefaultTemplate">
     <ehcache:key-type>java.lang.Long</ehcache:key-type>
     <ehcache:value-type>com.controller.Customer</ehcache:value-type>
     <ehcache:expiry>
       <ehcache:tti unit="seconds">30</ehcache:tti>
     </ehcache:expiry>

     <ehcache:heap unit="entries">200</ehcache:heap>
  </ehcache:cache>
</ehcache:config>

In my pom.xml i have the following configurations -

     <?xml version="1.0" encoding="UTF-8"?>
      <project xmlns="http://maven.apache.org/POM/4.0.0" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
       http://maven.apache.org/xsd/maven-4.0.0.xsd">
       <modelVersion>4.0.0</modelVersion>
       <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> 
       </parent>
       <groupId>com.test</groupId>
       <artifactId>test</artifactId>
       <version>0.0.1-SNAPSHOT</version>
      <name>test</name>
       <description>Demo project for Spring Boot</description>
       <properties>
        <java.version>1.8</java.version>
       </properties>
       <dependencies>    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <dependency>
            <groupId>org.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>3.6.2</version>
        </dependency>
        <dependency>
            <groupId>javax.cache</groupId>
            <artifactId>cache-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
     </project>

Application.java which starts spring boot app

@SpringBootApplication
@ComponentScan(basePackages = {"com.service"})
@EnableCaching
public class TestApplication {
  public static void main(String[] args) {
    SpringApplication.run(TestApplication.class, args);
  }
}

Component class for caching -

@Component
public class CustomerService {

   @Cacheable(cacheNames = "customer",key="#id")
   public Customer getCustomer(final Long id){
     System.out.println("Returning customer information for customer id 
     {} 
   "+id);
    Customer customer = new Customer();
    customer.setCustomerId(id);
    customer.setFirstName("Test");
    customer.setEmail("contact-us@test.com");
    return  customer;
   }
}

I tried with couple of approaches by adding component scan in the application but didn't worked out.

Spring boot starts and it shows cache has been added to cache manager.

Marko Previsic
  • 1,820
  • 16
  • 30
vikas
  • 73
  • 1
  • 1
  • 5

2 Answers2

0

I got it working by changing from @Component to @Service. I don't understand why caching is not working under component and works in service layer

vikas
  • 73
  • 1
  • 1
  • 5
-1

If you want to add Caching annotation at the Repository layer then just remove @Repository Annotation, If you want to add Caching at Service Layer then use @Service Annotation instead of @Component Annotation.

Vikram
  • 179
  • 1
  • 7