Spring缓存不使用EHCache+JCache。

时间:2021-07-12 05:19:50

I'm trying to integration the ehcache implementation of jcache to work with spring. So I have a facade defined like this:

我正在尝试集成jcache的ehcache实现与spring一起工作。所以我有一个这样的外观:

@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:

基于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();
   }
}

and a test that calls the facade 10 times:

还有一项测试,称其为正面10次:

@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:

但是结果是通过了这个方法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

You can find a sample project to reproduce it here: https://github.com/paranoiabla/spring-cache-test

您可以在这里找到一个复制它的示例项目:https://github.com/paranoiabla/spring-cache-test。

1 个解决方案

#1


2  

JCache support is a new feature of Spring 4.1. You are using 4.0.4 which does not have this support yet.

JCache支持是Spring 4.1的一个新特性。您使用的是4.0.4,目前还没有这种支持。

Spring Framework 4.1 has not been released yet. You can try a snapshot by adding the following to your project

Spring Framework 4.1尚未发布。您可以通过在项目中添加以下内容来尝试快照。

<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

和春天。版本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).

注意:您的JSR-107缓存管理器是错误的。您应该创建一个javax.cache。一旦你有了它,你就应该把它包装到一个Spring的CacheManager上。请记住,您也可以在那里声明任何CacheManager,它可以工作(SimpleCacheManager、GuavaCacheManager等)。

#1


2  

JCache support is a new feature of Spring 4.1. You are using 4.0.4 which does not have this support yet.

JCache支持是Spring 4.1的一个新特性。您使用的是4.0.4,目前还没有这种支持。

Spring Framework 4.1 has not been released yet. You can try a snapshot by adding the following to your project

Spring Framework 4.1尚未发布。您可以通过在项目中添加以下内容来尝试快照。

<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

和春天。版本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).

注意:您的JSR-107缓存管理器是错误的。您应该创建一个javax.cache。一旦你有了它,你就应该把它包装到一个Spring的CacheManager上。请记住,您也可以在那里声明任何CacheManager,它可以工作(SimpleCacheManager、GuavaCacheManager等)。