您拥有的代码无法正常工作的原因
@Cache不是要这样工作。如果要缓存查询方法执行的结果,最简单的方法是使用Spring的缓存抽象。
interface PromotionServiceXrefRepository extends PagingAndSortingRepository<PromotionServiceXref, Integer> { @Query("…") @Cacheable("servicesByCustomerId") Set<PromotionServiceXref> findByCustomerId(int customerId); @Override @CacheEvict(value = "servicesByCustomerId", key = "#p0.customer.id") <S extends PromotionServiceXref> S save(S service);}此设置将导致呼叫结果
findByCustomerId(…)由客户标识符缓存。请注意,我们
@CacheEvict在覆盖的
save(…)方法中添加了,这样,只要保存了实体,就将驱逐用查询方法填充的缓存。这可能也必须传播到
delete(…)方法中。
现在,您可以继续进行专用配置
CacheManager(有关详细信息,请参见参考文档),以插入您喜欢的任何缓存解决方案(
ConcurrentHashMap在此处使用普通格式)。
@Configuration @EnableCaching class CachingConfig { @Bean CacheManager cacheManager() { SimpleCacheManager cacheManager = new SimpleCacheManager(); cacheManager.addCaches(Arrays.asList(new ConcurrentMapCache("servicesByCustomerId))); return cacheManager; } }


