Spring Cache Abstraction with multi-value queries
Asked Answered
K

3

3

Does Spring Cache abstraction support multi-value queries?

Instead of:

@Cacheable("books") public Book findBook(ISBN isbn) {...}

Imagine a query that goes like this:

@Cacheable("books") public List< Book > findBook(List< ISBN > isbns) {...}

Is this supported? Will it individually cache each book returned in the collection?

Thanks!

Kamseen answered 1/11, 2012 at 14:3 Comment(1)
See a related post (#33658381) to which I provided 1 possible solution.Libidinous
T
4

Spring Cache stores whole result under single cache key, so it is not possible to store individually each object returned in the collection. In case of caching result of a JPA Query you may use query cache. In other cases if memcached is an option for you, you can try Simple Spring Memcached and ReadThroughMultiCache annotation. It will store each element of the collection individually under dedicated cache key.

Tessie answered 2/11, 2012 at 12:15 Comment(0)
S
2

The query cache can indeed cache a list of results per query input. Be aware thought, that only the IDs of the returned entities will be saved in the query cache. You have to have the entity cache enabled separately for the returned entity type itself, if you want the properties to be cached too.

Swish answered 1/11, 2012 at 14:24 Comment(0)
S
0

Worked for me. Here's a link to my answer. https://mcmap.net/q/376052/-spring-cache-jsr107-list-collection-argument-as-part-of-the-key

TL:DR

@Cacheable(cacheNames = "test", key = "#p0")
public List<String> getTestFunction(List<String> someIds) {

My example is with String and not custom object.

Hope it helps at least a bit :)

Sober answered 2/4, 2020 at 13:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.