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

@Cacheable caches empty Optionals but documentation states otherwise #27184

Closed
mwojterski opened this issue Jul 16, 2021 · 2 comments
Closed
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: documentation A documentation task
Milestone

Comments

@mwojterski
Copy link

mwojterski commented Jul 16, 2021

@Cacheable javadoc states:

Note that Java8's Optional return types are automatically handled and its content is stored in the cache if present. (...)

This seems to suggest only present optionals are cached, that's not true. English isn't my first lang, so please forgive if I misunderstood, I guess "if present" may refer to the cache.

spring-context v5.3.1

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jul 16, 2021
@sbrannen sbrannen added in: core Issues in core modules (aop, beans, core, context, expression) type: documentation A documentation task labels Jul 16, 2021
@sbrannen sbrannen changed the title @Cacheable caches empty Optionals, javadoc says otherwise? @Cacheable caches empty Optionals but Javadoc states otherwise Jul 16, 2021
@sbrannen sbrannen changed the title @Cacheable caches empty Optionals but Javadoc states otherwise @Cacheable caches empty Optionals but documentation states otherwise Jul 16, 2021
@sbrannen
Copy link
Member

sbrannen commented Jul 16, 2021

You're correct. The wording is slightly confusing. This applies to the Javadoc for @Cacheable as well as the documentation in the reference manual.

If an Optional return value is empty, null will be stored in the Cache just as if the return value itself were null.

Internally, this behavior is handled by org.springframework.util.ObjectUtils.unwrapOptional(Object).

@sbrannen sbrannen added this to the 5.3.10 milestone Jul 16, 2021
@sbrannen sbrannen removed the status: waiting-for-triage An issue we've not yet triaged or decided on label Jul 16, 2021
@sbrannen sbrannen self-assigned this Jul 16, 2021
@sbrannen
Copy link
Member

@mwojterski, thanks for opening your first issue for the Spring Framework.

We will update the documentation accordingly.

lxbzmy pushed a commit to lxbzmy/spring-framework that referenced this issue Mar 26, 2022
This commit improves the documentation for @Cacheable to point out that
`null` will be stored in the cache for an empty `Optional` return value.

Closes spring-projectsgh-27184
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: documentation A documentation task
Projects
None yet
Development

No branches or pull requests

3 participants