5

Using spring-boot and its caching mechanism, is it possible to automatically store all entities returned as a collection into the cache one by one?

For instance picture the following Repository method:

@Query("...")
List<Foo> findFooByBar(Bar bar);

I'd like to insert these in a Spring Cache, one by one, meaning there would be N insertions (one for each element in the list) rather than just one (the whole list).

Example:

@Query("...")
@CachePut(value = "foos", key = "result.each.id")
List<Foo> findFooByBar(Bar bar);
Wouter Lievens
  • 4,019
  • 5
  • 41
  • 66
  • I've logged an issue in Pivotal's JIRA because from a glance at the source code in `CacheAspectSupport` it seems like this is impossible. https://jira.spring.io/browse/SPR-15213 – Wouter Lievens Jan 31 '17 at 21:04

1 Answers1

4

Sometime ago, another person asked a similar/related question on SO and I provided an answer along with an example.

As you know, by default, out-of-the-box Spring does not handle multiple keys/values in the way that you suggested, though I like your thinking here and your example/UC is valid.

Often times, however, you can achieve what you want using an intermediate solution with just a bit of extra work. Spring is an excellent example of the Open/Closed principle and the 2 primary abstractions in Spring's Cache Abstraction is the Cache and CacheManager interfaces.

Typically, you can pick an existing implementation and "adapt" either the Cache or the CacheManager, or both, as I have done in my example.

Though not as ideal or convenient, hopefully this will give you some ideas until perhaps SPR-15213 is considered (though maybe not).

Cheers, John

Community
  • 1
  • 1
John Blum
  • 7,381
  • 1
  • 20
  • 30