Skip to content

Spring Cache does not support List key cache #24139

Closed
@shenjianeng

Description

@shenjianeng

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

Activity

shenjianeng

shenjianeng commented on Dec 5, 2019

@shenjianeng
Author
@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

snicoll

snicoll commented on Dec 7, 2019

@snicoll
Member

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.

added
status: declinedA suggestion or change that we don't feel we should currently apply
and removed on Dec 7, 2019
ms100

ms100 commented on Feb 3, 2023

@ms100

You can use Cache As Multi

shenjianeng

shenjianeng commented on Feb 3, 2023

@shenjianeng
Author
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: declinedA suggestion or change that we don't feel we should currently apply

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @snicoll@ms100@spring-projects-issues@shenjianeng

        Issue actions

          Spring Cache does not support List key cache · Issue #24139 · spring-projects/spring-framework