1

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

下面的方法是一个接口的实现,控制器根据映射 jsp 的请求调用该接口URL.cacheID始终是常量,我的想法是将检索到的值存储在 hashmap 中,并在应用程序需要时访问这些映射。

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

弹簧上下文.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>
4

1 回答 1

0

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

于 2012-11-06T03:36:47.733 回答