I want to add \"Cacheable\" annotation in findOne method, and evict the cache when delete or happen methods happened.
How can I do that ?
I think basically @seven's answer is correct, but with 2 missing points:
We cannot define a generic interface, I'm afraid we have to declare every concrete interface separately since annotation cannot be inherited and we need to have different cache names for each repository.
save and delete should be CachePut, and findAll should be both Cacheable and CacheEvict
public interface CacheRepository extends CrudRepository {
@Cacheable("cacheName")
T findOne(String name);
@Cacheable("cacheName")
@CacheEvict(value = "cacheName", allEntries = true)
Iterable findAll();
@Override
@CachePut("cacheName")
T save(T entity);
@Override
@CacheEvict("cacheName")
void delete(String name);
}
Reference