I'm trying to integration the ehcache implementation of jcache to work with spring. So I have a facade defined like this:
@Component(value = "sampleFacade")
@CacheDefaults(cacheName = "default")
public class SampleFacadeImpl implements SampleFacade
{
@Override
@CacheResult(cacheName = "site")
public SitePojo getSiteForUid(final String uid)
{
System.out.println("getting the site for uid: " + uid);
final SitePojo pojo = new SitePojo();
pojo.setUid(uid);
pojo.setUrl(uid);
return pojo;
}
}
and a java based configuration that looks like this:
@Configuration
@EnableCaching(mode = AdviceMode.PROXY)
@ComponentScan(basePackages = { "com.test" })
public class TestConfig implements CachingConfigurer
{
@Resource
public ApplicationContext context;
@Override
@Bean(name = { "defaultKeyGenerator", "keyGenerator" })
public KeyGenerator keyGenerator() {
return new SimpleKeyGenerator();
}
@Override
@Bean(name = { "defaultCacheManager", "cacheManager" })
public CacheManager cacheManager() {
final JCacheCacheManager cacheManager = new JCacheCacheManager();
cacheManager.setCacheManager((javax.cache.CacheManager) context.getBean("cacheManagerFactoryBean"));
return cacheManager;
}
@Bean(name = { "defaultCacheManagerFactoryBean", "cacheManagerFactoryBean" })
protected JCacheManagerFactoryBean defaultCacheManagerFactoryBean() {
return new JCacheManagerFactoryBean();
}
}
and a test that calls the facade 10 times:
@Test
public void testGetSiteForUid() {
for (int i = 0; i < 10; i++) {
assertNotNull(sampleFacade.getSiteForUid("uid"));
}
}
but the result is passing through the method 10 times:
getting the site for uid: uid
getting the site for uid: uid
getting the site for uid: uid
getting the site for uid: uid
getting the site for uid: uid
getting the site for uid: uid
getting the site for uid: uid
getting the site for uid: uid
getting the site for uid: uid
getting the site for uid: uid
You can find a sample project to reproduce it here: https://github.com/paranoiabla/spring-cache-test