#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:
ade90036
2018-09-18 14:35:29 +01:00
committed by Mark Paluch
parent 445947db52
commit 2828e352fa
2 changed files with 16 additions and 3 deletions

View File

@@ -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")

View File

@@ -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);
}
}