4

I was wondering if it's possible to use @Cacheable annotations on the spring data mongo repositories. For example like this:

public interface UserRepository extends MongoRepository<User, String> {

    @Cacheable("byId")
    public interface UserRepository extends MongoRepository<User, String> {
        User findById(String id);
    }
}

I'd like to do it on the interface class itself and avoid having a wrapper class if possible. In addition, is there a sample for how to do the cache config for redis with java config (not xml)?

demig0d
  • 1,131
  • 1
  • 12
  • 24

1 Answers1

2

Yes, you can use Cacheable on any public method with spring aspects. You also have to use EnableCaching in any configuration class and optionally a CacheManager bean.

Ulises
  • 9,115
  • 2
  • 30
  • 27