Spring Boot и EhCache 3 не работают на сервисном уровне - PullRequest
0 голосов
/ 22 апреля 2020

Я реализую Ehcache с простым вариантом использования и не могу заставить его работать на слое @Service. Я не уверен, что не так.

Это мой класс обслуживания:

@Service
public class AccountsServiceImpl implements AccountsService {

    private final AccountsRepository reactiveAccountsRepository;


    @Autowired
    public AccountsServiceImpl(AccountsRepository reactiveAccountsRepository){
        this.reactiveAccountsRepository = reactiveAccountsRepository;
    }

    @Override
    @Cacheable(value = "accounts", key = "#root.methodName")
    public Mono<AccountsListResponse> getAccounts(String codeType) {
        return reactiveAccountsRepository.findByCodeType(codeType).map(this::convertToAccountsListResponse);
    }

    @Cacheable(value = "accounts", key = "#root.methodName")
    private AccountsListResponse convertToAccountsListResponse(Accounts accounts){
        return AccountsListResponse.builder().accounts(accounts.getCodeList().stream()
                                                                            .map(codes->AccountsResponse.builder().code(codes.getCode()).description(codes.getDescription())
                                                                            .build()).collect(Collectors.toList()))
                .build();
    }

}

ehcache. xml:

<config
        xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
        xmlns='http://www.ehcache.org/v3'
        xmlns:jsr107='http://www.ehcache.org/v3/jsr107'>

    <cache alias="accounts">
        <expiry>
            <none/>
        </expiry>
        <resources>
            <heap unit="entries">10000</heap>
        </resources>
    </cache>
</config>

Добавил эту строку в приложение. свойства

#eh-cache
spring.cache.jcache.config=classpath:ehcache.xml
@SpringBootApplication
@EnableReactiveCouchbaseRepositories
@EnableCaching
public class AccountsApplication{
    public static void main(String[] args) {
        SpringApplication.run(AccountsApplication.class, args);
    }

}

Pom зависимости:

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>

    <dependency>
      <groupId>org.ehcache</groupId>
      <artifactId>ehcache</artifactId>
    </dependency>

    <dependency>
      <groupId>javax.cache</groupId>
      <artifactId>cache-api</artifactId>
</dependency>

Я вижу консоль и кажется, что она создана:

2020-04-22 10:23:39.664  INFO 16512 --- [           main] org.ehcache.core.EhcacheManager          : 
Cache 'accounts' created in EhcacheManager. 
2020-04-22 10:23:40.056  INFO 16512 --- [           main] org.ehcache.jsr107.Eh107CacheManager     : 
Registering Ehcache MBean javax.cache:type=CacheStatistics,CacheManager=file./C./Proyectos/Core/core-accounts-boot/target/classes/ehcache.xml,Cache=accounts 
2020-04-22 10:23:40.064  INFO 16512 --- [           main] org.ehcache.jsr107.Eh107CacheManager     : 
Registering Ehcache MBean javax.cache:type=CacheStatistics,CacheManager=file./C./Proyectos/Core/core-accounts-boot/target/classes/ehcache.xml,Cache=accounts

но при тестировании он всегда идет в базу данных для получения данных. Я что-то пропустил? Спасибо!

...