Skip to content

Allow @Cacheable method to return java.util.Optional variant of cached value [SPR-14230] #18804

Closed
@spring-projects-issues

Description

@spring-projects-issues

Acacio Antonio Andruczewicz opened SPR-14230 and commented

class MyServiceImpl implements MyService {

    private static final String CACHE_NAME = "itemCache";      

    @Override
    @Cacheable(CACHE_NAME)
    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) {
        ...
    }

}

In the above example, a ClassCastException is thrown because insertItem puts an Item instance in the cache, and findById expects an Optional that may contain an Item instance. It would be nice if Spring cache abstraction supported Java 8 Optional.


Affects: 4.2.5

Reference URL: http://stackoverflow.com/questions/36844270/handling-java-8-optional-with-spring-cache

Issue Links:

Referenced from: commits 240f254, fdb31cd

0 votes, 5 watchers

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions