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
JCache support is a new feature of Spring 4.1. You are using 4.0.4 which does not have this support yet.
Spring Framework 4.1 has not been released yet. You can try a snapshot by adding the following to your project
<repositories>
<repository>
<id>spring-snapshot</id>
<name>Springframework Snapshot Repository</name>
<url>http://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
And flip the spring.version
to 4.1.0.BUILD-SNAPSHOT
I have forked your project and updated it here so that it works as it should. Checking what I've changed would help you understand what was missing.
NOTE: your JSR-107 cache manager is wrong. You should create a javax.cache.CacheManager
and once you have it you should wrap it to a Spring's CacheManager
. Keep in mind you could just as well declare any CacheManager
there and it would work ( SimpleCacheManager
, GuavaCacheManager
, etc).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.