2013-03-12 3 views
1

나는 @Cacheable로 주석을 달았지만 캐시는 전혀 작동하지 않는다. 나는 방법 안에 로그 메시지를 넣었다.Spring @Cacheable Not Working

<cache:annotation-driven mode="proxy" proxy-target-class="true" cache-manager="cacheManager" /> 
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> 
     <property name="configLocation" value="WEB-INF/ehcache/ehcache.xml"></property> 
     <property name="shared" value="true"></property> 
    </bean> 

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> 
     <property name="cacheManager" ref="ehcache"></property> 
    </bean> 

@Controller 
@RequestMapping(value = "/analytics") 
public class AnalyticsController { 

    @Autowired 
    private ReportDao reportDao; 

    /** 
    * 
    */ 
    public AnalyticsController() { 
    } 

    @RequestMapping(value = "/lcr-report", method = RequestMethod.GET) 
    public String viewCostReport(ModelMap map) { 

     List<Country> countryList = reportDao.getAllCountry(); 

     map.put("countryList", countryList); 

     return "lcrReport"; 
    } 

} 


@Repository 
@Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.DEFAULT, 
    rollbackFor={DataAccessException.class, SQLException.class, Exception.class}) 
public class ReportDao { 

    @Autowired 
    private JdbcTemplate dao; 

    /** 
    * 
    */ 
    public ReportDao() { 
    } 

    @Cacheable(value = {"reportDao"}/*, key= "T(Country).hash(#List<Country>)"*/) 
    @Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.DEFAULT, readOnly=true, 
      rollbackFor={DataAccessException.class, SQLException.class, Exception.class}) 
    public List<Country> getAllCountry() { 
     List<Country> countryList = null; 
     BeanPropertyRowMapper<Country> mapper = new BeanPropertyRowMapper<Country>(Country.class); 
     PreparedStatementCreator psc = new GenericPreparedStatementCreator("select c.country_code as countryCode, c.name as countryName from country c"); 
     System.out.println("Not from cache"); 
     countryList = dao.query(psc, mapper); 

     return countryList; 
    } 

} 
+0

방법에 대한 목록 에 대한 키를 추가하는 방법은 무엇입니까? – peterwkc

+0

'ehcache.xml'에 문제가 있다고 생각합니다. 게시 해주세요. –

답변

2

getAllCountry의 매개 변수를 사용하여 키를 만들어야합니다.

@Transactional(readOnly = true) 
@Cacheable(value = CACHE_NAME, key = "'countries'") 

을하고지도 캐시를 사용하여 작동 여부를 확인 : 당신이 이런 식으로 할 수 있도록 귀하의 경우는, 비어

@Configuration 
@EnableCaching(proxyTargetClass = true) 
public class CacheProducer { 

@Bean 
public CacheManager cacheManager() { 
     SimpleCacheManager result = new SimpleCacheManager(); 
     result.setCaches(Arrays.asList(new ConcurrentMapCache(DictionaryServiceImpl.CACHE_NAME))); 
     return result; 
    } 
} 

를 작동하는 경우 - 당신의 echache의 설정을 확인하는 시간입니다.

+0

나는 logger로 다중 호출을 테스트했으며 작동한다. – peterwkc