Consider:
@Service
class MyService {
@Autowired
MyRepository repo;
List<Book> getBooks(List<String> ids) {
return repo.getBooks(ids);
}
}
@Repository
class MyRepository{
public List<Book> getBooks(List<String> ids) {
...
// execute bulk db call
}
}
Now I would want to use the spring cache framework on the above repository.
Consider it to be a Redis cache.
So the problem could be broken down into:
- How to make a bulk call to Redis given the list of ids.
- How to invoke the
getBooks
method in the repository only for ids that were cache missed while Redis lookup. - And then finally merge the results from method call and Redis lookup and return to the service.
This is a very common problem statement and is easily solvable by writing custom decorators over the repository but I wish to solve it via the spring framework.