I am trying to cache using spring's @Cacheable. I'm also using spring mongodb. I keep getting the following error:
java.lang.IllegalArgumentException: Null key returned for cache operation (maybe you are using named params on classes without debug info?) CacheableOperation[public abstract test.models.User test.repositories.UserRepository.findById(java.lang.String)] caches=[userById] | key='#id' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless=''
Here's the code that I'm using. It seems that whether I use "#id" or "id" it doesn't seem to work. Either I get the IllegalArgumentException or it complains about id possibly not being public, but I'd like to keep "id" as private in my User model.
public interface UserRepository extends MongoRepository<User, String> {
@Override
@CacheEvict(value="byId", key="#entity.id")
<S extends User> S save(S entity);
@Cacheable(value="byId", key="#id")
User findById(String id);
public User findByUsername(String username);
}
Could someone please point out what I'm doing wrong? Essentially, I want to cache in redis all "users" underneath users but cache it with the User "id" as the key. Then, I'd like to also evict based on the same id when a user is saved.