Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow @Cacheable method to return java.util.Optional variant of cached value with @Cacheable(sync=true) [SPR-14853] #19419

Closed
spring-projects-issues opened this issue Oct 26, 2016 · 1 comment
Assignees
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Oct 26, 2016

Craig opened SPR-14853 and commented

#18804 is still reproducible if "sync=true" is set on the @Cacheable annotation.

For example, in

class MyServiceImpl implements MyService {

    private static final String CACHE_NAME = "itemCache";      

    @Override
    @Cacheable(value=CACHE_NAME, sync=)
    public Optional<Item> findById(Long id) {
        // access the repository to retrieve the item
    }

    @Override
    @CachePut(cacheNames = CACHE_NAME, key = "#item.id")
    public Item insertItem(Item item) {
        ...
    }

}

a ClassCastException is thrown.

The problem is in https://github.com/spring-projects/spring-framework/blob/v4.3.3.RELEASE/spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java#L358 - when sync=true, a diffferent path is taken that does not do the Optional wrapping/unwrapping which takes place at https://github.com/spring-projects/spring-framework/blob/v4.3.3.RELEASE/spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java#L404


Affects: 4.3.3

Issue Links:

  • #18804 Allow @Cacheable method to return java.util.Optional variant of cached value

1 votes, 3 watchers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants