@cacheable не работает с весенней загрузкой. Использование ehcache3 - PullRequest
0 голосов
/ 26 июня 2019

* Ehcache3 не работает с пружинной загрузкой - я попробовал с подходом, приведенным ниже. Spring boot никогда не кэширует значение, упомянутое в компоненте. Он вызывается n - ни разу, независимо от того, включен кеш или нет. В журналах показано, что кеш добавляется в диспетчер кеша, но это не так

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>

В моем pom.xml у меня есть следующие конфигурации -

     <?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, который запускает приложение весенней загрузки

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

Компонент класса для кеширования -

@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;
   }
}

Я попробовал с несколькими подходами, добавив компонентное сканирование в приложение но не сработало.

Весенняя загрузка запускается и показывает, что кеш добавлен в менеджер кеша.

1 Ответ

0 голосов
/ 26 июня 2019

Я заработал, изменив @Component на @Service. Я не понимаю, почему кеширование не работает под компонентом и работает в слое обслуживания

...