Reputation: 4315
I am trying to configure caching for my methods like this:
@Cacheable(value = USER_ENTITY_1_CACHE, key = "#userId")
public List<MyEntity1> findEntities1(Long userId) {
//...
}
And:
@Cacheable(value = USER_ENTITY_2_CACHE, key = "#userId")
public Set<MyEntity2> findEntities2(Long userId)
{
//..
}
@Cacheable(value = USER_ENTITY_2_CACHE, key = "#userId")
public Set<MyEntity2> findEntities2(Long userId, boolean deleted)
{
//..
}
And Evict for this:
@CacheEvict(cacheNames = USER_ENTITY_2_CACHE, key = "#userId", condition = "#userId != null")
Unfortunately, when I invoke any of this method with the same userId, the result for the second one is cached from the first method. This leads to casting problems and confusion.
For instance,
java.util.LinkedHashSet cannot be cast to java.util.List
In other words, when I invoke findEntities1 and then findEntities2, findEntities2 is not invoked, but taken from the cache and fails with class cast exception. The same work if I invoke them in reverse order. What I really want is that these two caches USER_ENTITY_1_CACHE, USER_ENTITY_2_CACHE don't know about each other and store their own method calls.
I am using RedisCacheManager.
Any ideas what might be wrong here?
Upvotes: 0
Views: 533
Reputation: 5283
Remove the key defined explicitly and try
Key will get automatically generated based on the argument based.
@Cacheable(value = USER_ENTITY_1_CACHE)
public List<MyEntity1> findEntities1(Long userId) {
//...
}
And:
@Cacheable(value = USER_ENTITY_2_CACHE)
public Set<MyEntity2> findEntities2(Long userId)
{
//..
}
Upvotes: 1