zchen
zchen

Reputation: 119

How to cache a list in spring?

I am using spring. I want to cache following method:

@Cacheable(cacheName = "xxx", key = "'foo_'.concat(#va1)")
public void foo(String var1, List<String> var2) { ... }

I want to include the value set of var2 into cache key. I know
key = "'foo_'.concat(#va1).concat('_').concat(#var2)" is wrong. (Because (#var2) will return its memory address, not values).

What should I do?

Upvotes: 0

Views: 872

Answers (1)

kism3t
kism3t

Reputation: 1361

You could get the hashCode of the List:

key = "'foo_'.concat(#va1).concat('_').concat(#var2.hashCode())"

OR

key = "'foo_'.concat(#va1).concat('_').concat(#var2.toString())"

give it a try.

With the second one you are risking that your key might get to long according to the size of the list.

Upvotes: 1

Related Questions