2013-11-04 2 views
1

스프링 프로젝트 3.2.4를 사용하여 자바 프로젝트를 작성 중입니다.설정 @cacheable에서 10 초 동안 캐시하기

10 초 동안 캐시해야하는 많은 SQL 쿼리가 있습니다.

나는 @cacheable 주석을 사용하여 함수 결과를 캐시 할 수 있음을 알고 있습니다.

내가 이해하지 못하는 것은 불과 10 초 동안 캐시하는 방법입니다. 캐싱 할 수있는 주석에 조건을 추가 할 수는 있지만 이러한 조건에 타이밍을 추가하는 방법을 찾는 것은 어렵습니다.

문제에 관한 정보는 크게 감사하겠습니다.

답변

2

스프링은이 상자를 제공하지 않지만 adapters을 지원하며 예를 들어 guava adapter을 사용할 수 있습니다. 특히 구성 만료 시간 제한을 허용합니다.

1

스케줄러를 사용하여 주기적으로 캐시를 제거하는 서비스 메소드를 호출 할 수 있습니다.

스케줄러 :

import org.springframework.scheduling.annotation.Scheduled; 
import org.springframework.beans.factory.annotation.Autowired; 

public class Scheduler { 

     @Autowired 
     private SomeService someService; 

     @Scheduled(fixedRate = 10000) 
     public void evictCaches() { 
       someService.evictCaches(); 
     } 
} 

서비스 :

import org.springframework.cache.annotation.CacheEvict; 
import org.springframework.transaction.annotation.Transactional; 

@Service 
@Transactional 
public class SomeService { 

     @CacheEvict(value = { "cache1", "cache2" }, allEntries = true) 
     public void evictAllCaches() { 
     } 
} 
관련 문제