#389 - Fix Cache eviction example.
Declare SpEL path to the key property on save(…) method accepting the entity. Original pull request: #412.
This commit is contained in:
@@ -28,7 +28,7 @@ import org.springframework.data.repository.CrudRepository;
|
||||
public interface CachingUserRepository extends CrudRepository<User, Long> {
|
||||
|
||||
@Override
|
||||
@CacheEvict("byUsername")
|
||||
@CacheEvict(value="byUsername", key="#p0.username")
|
||||
<S extends User> S save(S entity);
|
||||
|
||||
@Cacheable("byUsername")
|
||||
|
||||
@@ -36,13 +36,13 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
@RunWith(SpringRunner.class)
|
||||
@Transactional
|
||||
@SpringBootTest
|
||||
public abstract class CachingRepositoryTests {
|
||||
public class CachingRepositoryTests {
|
||||
|
||||
@Autowired CachingUserRepository repository;
|
||||
@Autowired CacheManager cacheManager;
|
||||
|
||||
@Test
|
||||
public void cachesValuesReturnedForQueryMethod() {
|
||||
public void checkCachedValue() {
|
||||
|
||||
User dave = new User();
|
||||
dave.setUsername("dmatthews");
|
||||
@@ -55,4 +55,17 @@ public abstract class CachingRepositoryTests {
|
||||
Cache cache = cacheManager.getCache("byUsername");
|
||||
assertThat(cache.get("dmatthews").get()).isEqualTo(dave);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void checkCacheEviction() {
|
||||
|
||||
User dave = new User();
|
||||
dave.setUsername("dmatthews");
|
||||
dave = repository.save(dave);
|
||||
|
||||
// Verify entity evicted on cache
|
||||
Cache cache = cacheManager.getCache("byUsername");
|
||||
assertThat(cache.get("dmatthews")).isEqualTo(null);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user