0

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.

demig0d
  • 1,131
  • 1
  • 12
  • 24

1 Answers1

0

FYI, the exception does not match the code (the name of the cache is different).

The key attribute on findById is useless. The cache abstraction is going to use the method arguments by default to compute the key. Since you have only one argument, it will use said argument. You can rewrite the method as follow and get the exact same result:

@Cacheable(value="byId")
User findById(String id);

I can't comment on the other issue. If the cache abstraction complains about something being private, it must be on the cache evict, not on this method. So posting an exception that's consistent with the description will help.

Stephane Nicoll
  • 31,977
  • 9
  • 97
  • 89
  • I will try that thank you for some reason it wasn't evicting earlier with this way of doing it. – demig0d Aug 27 '16 at 16:13