Skip to content

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

Closed
@spring-projects-issues

Description

@spring-projects-issues

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:

1 votes, 3 watchers

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions