I have this configuration class:
@Configuration
@EnableCaching
public class CacheConfig {
@Bean
@Primary
public CacheManager cacheManager() {
CaffeineCacheManager cacheManager = new CaffeineCacheManager("dogsInHouse");
cacheManager.setCaffeine(Caffeine.newBuilder()
.initialCapacity(200)
.expireAfterAccess(Duration.ofDays(30))
.maximumSize(500));
return cacheManager;
}
}
in the properties file:
spring.jpa.show-sql=true
in the service:
@Service
@Transactional(readOnly = true)
@Slf4j
@CacheConfig(cacheNames = {"dogsInHouse"})
public class DogsInHouseService {
@Cacheable("dogsInHouse")
public DogsInHouse findDogHouseEnFromDB (String key) {
return dogsEnRepository.findByNameAndLangIs(key);
}
}
The only thing that I can always see in the console is select query but I don't see the cache logs.
Use
logging.level.org.springframework.cache=TRACEin your application.properties to see whether or not the value is picked from the cache.NOTE: When you hit the service for the first time, it will bring data from database and will put the data into the cache and next time, it will bring from the cache.
Please remove
@CacheConfigyou don't need that in Spring Boot App.I've used
spring-data-jpawith MySQL for demonstration.Same Config used. Just added for reference.
DogInHouse for testing:
DogInHouseRepository for testing:
My Service class for testing:
application.properties for testing:
My pom.xml had these dependencies:
My Application Log:
First Time Service Hit:
Second Time Service Hit:
Postman Output: