2017-11-22 12 views
0

누군가가 아래의 구현 문제가 무엇인지 말해 줄 수 있습니까? 둘째, 전체 캐시를 삭제하려고합니다. 그러면 캐시를 미리 채우거나 프라임하려고합니다. 그러나 아래에 나와있는 두 가지 방법은 두 캐시를 모두 삭제하는 것입니다.하지만 캐시를 미리 채우거나 프라이밍하지는 않습니다. 어떤 생각?전체 캐시를 제거한 다음 캐시를 미리 채우는 방법은 무엇입니까?

import org.springframework.cache.annotation.CacheEvict; 
import org.springframework.cache.annotation.Cacheable; 
import org.springframework.cache.annotation.Caching; 

@Cacheable(cacheNames = "cacheOne") 
List<User> cacheOne() throws Exception {...} 

@Cacheable(cacheNames = "cacheOne") 
List<Book> cacheTwo() throws Exception {...} 

@Caching (
     evict = { 
       @CacheEvict(cacheNames = "cacheOne", allEntries = true), 
       @CacheEvict(cacheNames = "CacheTwo", allEntries = true) 
     } 
) 
void clearAndReloadEntireCache() throws Exception 
{ 
    // Trying to reload cacheOne and cacheTwo inside this method 
    // Is this even possible? if not what is the correct approach? 
    cacheOne(); 
    cacheTwo(); 
} 

나는 봄 부팅 응용 프로그램 (v1.4.0을)를했습니다, 더 중요한 것은, 다음과 같은 종속성을 활용 : 당신이 clearAndReloadEntireCache() 메소드를 호출하면

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-cache</artifactId> 
</dependency> 
<dependency> 
    <groupId>org.ehcache</groupId> 
    <artifactId>ehcache</artifactId> 
    <version>3.3.0</version> 
</dependency> 
<dependency> 
    <groupId>javax.cache</groupId> 
    <artifactId>cache-api</artifactId> 
    <version>1.0.0</version> 
</dependency> 

답변

1

만이 방법은 캐싱에 의해 처리됩니다 인터셉터. 동일한 객체의 다른 메소드 (cacheOne()cacheTwo())를 호출해도 런타임시 캐시 차단이 발생하지 않지만 두 객체 모두 @Cacheable으로 주석 처리됩니다. 당신은 cacheOne 두 개의 메서드 호출에 cacheTwo를 다시로드하여 원하는 기능을 달성 할 수

다음과 같습니다 :

@Caching(evict = {@CacheEvict(cacheNames = "cacheOne", allEntries = true, beforeInvocation = true)}, 
     cacheable = {@Cacheable(cacheNames = "cacheOne")}) 
public List<User> cleanAndReloadCacheOne() { 
    return cacheOne(); 
} 

@Caching(evict = {@CacheEvict(cacheNames = "cacheTwo", allEntries = true, beforeInvocation = true)}, 
     cacheable = {@Cacheable(cacheNames = "cacheTwo")}) 
public List<Book> cleanAndReloadCacheTwo() { 
    return cacheTwo(); 
}