@cacheable of com.googlecode.ehcache.annotations.Cacheable不能使用spring

时间:2012-11-05 00:56:56

标签: spring ehcache

我是ehcache和spring注释集成的新手。我使用 com.googlecode.ehcache.annotations.Cacheable 注释,如下所示 spring 2.5 ,它永远不会缓存返回值。 cache.getKeys()返回一个空列表,每次控制器进行调用时都会调用该方法。 请帮帮我。

以下方法是控制器根据映射的jsp URL.cacheID的请求调用的接口的实现始终是常量的,并且我的想法是将检索到的值存储在散列映射中并在访问这些映射时在申请中需要。

 @Override
 @Cacheable(cacheName="partnerMapping")
 public String retrievePartnerMappings(String cacheID) {
   partnerCodeToNameMapping = new HashMap<String, String>();
   partnerNameToCodeMapping = new HashMap<String, String>();
   Cache cache = cacheManager.getCache("partnerMapping");
   log.debug(cache.getKeys().toString());
   try {
   log.debug("Querying for partner mappings...");
   Collection<PartnerMapping> partnerMappings = this.getSimpleJdbcTemplate()
      .query(
          sqlStatements.getProperty("selectAllSql"),
          ParameterizedBeanPropertyRowMapper
              .newInstance(PartnerMapping.class));
   for (PartnerMapping mapping : partnerMappings) {
     partnerCodeToNameMapping.put(mapping.getPartnerCode().toUpperCase(),
        mapping.getPartnerName());
     partnerNameToCodeMapping.put(mapping.getPartnerName(), mapping
        .getPartnerCode().toUpperCase());
   }
   return "success";
 } catch (DataAccessException e) {
  log.error("Unable to retrieve the partner mappings!!", e);
  return "fail";
 }
}

ehcache.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">

<cache name="partnerMapping" maxElementsInMemory="100" eternal="false"
   overflowToDisk="false" timeToLiveSeconds="120" timeToIdleSeconds="120"/>
</ehcache>

弹簧context.xml中

<?xml version="1.0" encoding="UTF-8"?>
<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:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans                         
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd        
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring 
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">

  <ehcache:annotation-driven />
  <context:annotation-config />
  <context:component-scan base-package="com.spring.ehcache" />

  <ehcache:config cache-manager="cacheManager">
    <ehcache:evict-expired-elements interval="60" />
  </ehcache:config>

  <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation"  value="/WEB-INF/ehcache.xml"/>
  </bean>

</beans>

1 个答案:

答案 0 :(得分:0)

设法让这个工作。我在主dispatcherServlet xml中包含了ehcache配置,而不是单独的xml配置,然后包含在spring上下文中。