I am trying to add Caching support to my project so that static data is cached and database is not contacted everytime static data is needed
My applicationContext.xml
looks like
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">
<context:component-scan base-package="com.yahoo.comma"/>
<bean id="liquibase" class="liquibase.integration.spring.SpringLiquibase">
<property name="dataSource" ref="dataSource"/>
<property name="changeLog" value="classpath:liquibase/changelog.xml"/>
<property name="defaultSchema" value="pryme"/>
</bean>
<cache:annotation-driven/>
<!-- generic cache manager -->
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="targetingAttributes"/>
</set>
</property>
</bean>
</beans>
and my AgeRepository
class looks like
@Component
@Transactional
public class AgeRepositoryService {
private static final Logger LOGGER = LoggerFactory.getLogger(AgeRepositoryService.class);
private AgeRepository ageRepository;
@SuppressWarnings("UnusedDeclaration")
public AgeRepositoryService() {
}
@Autowired
public AgeRepositoryService(@Nonnull final AgeRepository ageRepository) {
this.ageRepository = ageRepository;
}
@Nonnull
public Age save(@Nonnull final Age age) {
LOGGER.debug("adding age {}", age);
return ageRepository.saveAndFlush(age);
}
@Cacheable("targetingAttributes")
@Nonnull
public List<Age> getAges() {
return ageRepository.findAll();
}
}
I have Integration tests that test via hitting REST
endpoints that data is received.
Question
But how to I test that Caching is indeed working as expected? any advices?
You could just log every object creation. Cached objects should not be created again.
public Age(){
System.out.println("Age created"); // or use static int and count instances
}
the other option is to create your own ConcurrentMapCacheFactoryBean. Implementation you can find on github
public class MYConcurrentMapCacheFactoryBean implements FactoryBean<ConcurrentMapCache>, BeanNameAware, InitializingBean{
// code from github
public String toString(){
// todo show content of ConcurrentMapCache
}
}
Finally change the bean definition in your applicationContext.xml
to package.MYConcurrentMapCacheFactoryBean
I think the best way to show the content of your cache is to get the cache instance using ApplicationContext:
@Autowired
private ApplicationContext appContext;
public void printCacheContent(){
SimpleCacheManager cacheMng = (SimpleCacheManager) appContext.getBean("cacheManager");
System.out.println(cacheMng.loadCaches().toString());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With