春に動作するように jcache の ehcache 実装を統合しようとしています。したがって、次のように定義されたファサードがあります。
@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;
}
}
そして、次のような Java ベースの構成:
@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();
}
}
そして、ファサードを 10 回呼び出すテスト:
@Test
public void testGetSiteForUid() {
for (int i = 0; i < 10; i++) {
assertNotNull(sampleFacade.getSiteForUid("uid"));
}
}
しかし、結果はメソッドを 10 回通過しています。
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
ここでそれを再現するためのサンプル プロジェクトを見つけることができます: https://github.com/paranoiabla/spring-cache-test