I hava some ids to find users :


List<User> users = ids.stream().map(id -> {
    return getUserById(id);
})
.collect(Collectors.toList());

@Cacheable(key = "#p0", unless = "#result == null")
public User getUserById(Long id) {
...
}

I can also use :


List<User> users = ids.stream().map(id -> {
    return getUserById(id);
})
.collect(Collectors.toList());

@Cacheable(key = "#ids.hash")
public Collection<User> getUsersByIds(Collection<Long> ids) {
...
}

but getUsersByIds(Collection ids) cache and getUserById(Long id) cache cannot be shared

why does spring cache not support ids cache?

I want them to share the cache

Comment From: shenjianeng

@Cacheable(key = "#ids.hash")
public Collection<User> getUsersByIds(Collection<Long> ids) {
...
}

RedisCache key: ids.hash value: list

invoke method:

void updateUser(Long id)

I need clear RedisCache(key:ids.hash)

it is not graceful

Comment From: snicoll

Thanks for the report. The cache abstraction has no notion of such state, if you return a Collection, that's effectively what you're asking to store in the cache. Nothing forces you to keep the same item type for a given cache either so that kind of assumptions is not a great candidate for such a high-level abstraction.

The discussion in #23221 is also relevant.

Comment From: ms100

You can use Cache As Multi

Comment From: shenjianeng

这是来自邮箱的自动回复邮件,我已收到邮件。-------------------------------------------------