嗨我有方法執行時乾淨的緩存問題。 這裏是我的配置和高速緩存方法:Spring Cache Evict不起作用
@Configuration
@EnableCaching
@AutoConfigureAfter(value = {MetricsConfiguration.class, DatabaseConfiguration.class})
@Profile("!" + Constants.SPRING_PROFILE_FAST)
public class CacheConfiguration {
private final Logger log = LoggerFactory.getLogger(CacheConfiguration.class);
public static final String STOCK_DETAILS_BY_TICKER_CACHE = "stockDetailsByTickerCache";
public static final String RSS_NEWS_BY_TYPE_CACHE = "rssNewsByTypeCache";
@Bean
public CacheManager cacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager();
List<Cache> caches = new ArrayList<Cache>();
caches.add(new ConcurrentMapCache(STOCK_DETAILS_BY_TICKER_CACHE));
caches.add(new ConcurrentMapCache(RSS_NEWS_BY_TYPE_CACHE));
cacheManager.setCaches(caches);
return cacheManager;
}
}
這個方法我想緩存:
@Cacheable(cacheNames = CacheConfiguration.RSS_NEWS_BY_TYPE_CACHE, key = "#type")
public ResponseEntity<List<NewsDetailsDTO>> getLatestNewsMessageByType(RssType type) {
Pageable pageable = new PageRequest(0, 5, Sort.Direction.DESC, "createdDate");
List<NewsMessage> latestNewsMessage = newsMessageRepository.findByType(type, pageable).getContent();
return new ResponseEntity<List<NewsDetailsDTO>>(mapToDTO(latestNewsMessage), HttpStatus.OK);
}
在此方法的執行我想按類型清理緩存:
@CacheEvict(cacheNames={CacheConfiguration.RSS_NEWS_BY_TYPE_CACHE}, beforeInvocation = true, key = "#news.type")
public void save(NewsMessage news) {
newsMessageRepository.save(news);
}
和NewsMessage對象如下所示:
@Entity
@Table(name = "NEWS_MESSAGE")
public class NewsMessage extends ChatMessage {
<other fileds>
@NotNull
@Enumerated(EnumType.STRING)
private RssType type;
}
緩存事情工作正常,第一次有一個查詢數據庫第二次和下一個數據是從緩存中獲取。問題是當我更新數據@CacheEvict不清除緩存。我試圖使用這個註釋清除所有緩存: @CacheEvict(cacheNames = {CacheConfiguration.RSS_NEWS_BY_TYPE_CACHE},allEntries = true) 但它也不起作用。你可以幫幫我嗎?
我有以下的getter: \t公共RssType的getType(){ \t \t返回類型; \t} 所以它應該很好。但請注意,allEntires = true的cacheEvict也不起作用。 – Pulkownik