我正在使用Spring Boot, Spring Data, with QueryDSL
。我的大部分查询均基于QueryDSL
。我想实现一个存储User
个查询的简单缓存,一旦单个实体updated/saved
,就会清除整个Cache
。
所以这是我的存储库接口:
@Repository
@CacheConfig(cacheNames = "us")
public interface UserRepository extends JpaRepository<User, Long>, QueryDslPredicateExecutor<User> {
@Cacheable
List<User> findAll();
@Override
@Cacheable
List<User> findAll(Sort sort);
@Override
@Cacheable
List<User> findAll(Iterable<Long> longs);
@Override
@CacheEvict(allEntries = true)
<S extends User> List<S> save(Iterable<S> entities);
@Override
@CacheEvict(allEntries = true)
<S extends User> S saveAndFlush(S entity);
@Override
@Cacheable
Page<User> findAll(Pageable pageable);
@Override
@CacheEvict(allEntries = true)
<S extends User> S save(S entity);
@Override
@Cacheable
User findOne(Long aLong);
@Override
@Cacheable
Iterable<User> findAll(Predicate predicate);
@Override
@Cacheable
User findOne(Predicate predicate);
@Override
@Cacheable
Iterable<User> findAll(Predicate predicate, Sort sort);
@Override
@Cacheable
Page<User> findAll(Predicate predicate, Pageable pageable);
}
如您所见,我已缓存所有“查找”方法并使用“保存方法”来删除所有条目。通常使用的是:
Page<User> findAll(Predicate predicate, Pageable pageable);
<S extends User> S save(S entity);
Caching
仅在存储库上设置,在服务/控制器等上没有设置。
这是我的配置:
@Bean
public CacheManager cacheManager() {
GuavaCacheManager manager = new GuavaCacheManager();
CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder()
.removalListener(new RemovalListener<Object, Object>() {
@Override
public void onRemoval(RemovalNotification<Object, Object> notification) {
LOG.info("Cache evicted: " + notification);
}
})
.maximumSize(100);
manager.setCacheBuilder(builder);
return manager;
}
@Bean
public KeyGenerator keyGenerator() {
return new SimpleKeyGenerator();
}
我还应该补充说User
实体有几个子类,但是我用UserRepository
问题是即使我的搜索请求已成功缓存,当我执行保存时,缓存从不驱逐。
我做错了什么?