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

Can not access method result in condition closure #47

Open
igoraguiar opened this issue Oct 18, 2017 · 4 comments
Open

Can not access method result in condition closure #47

igoraguiar opened this issue Oct 18, 2017 · 4 comments

Comments

@igoraguiar
Copy link

I have some cache conditions where the value can only be cached if the value is not null.
Apparently there is now way to do it in version 4, since the result of the method call can not be accessed inside the condition closure.

Before, I had the following condition: "#result != null", now I would need to rewrite it to something like
{ result != null }

@jameskleeh
Copy link
Contributor

I don't understand the use case. Can you put together a more concrete example?

@igoraguiar
Copy link
Author

Of course.
I have a service with a method like this:

@Cacheable(value = "dataServiceCache.vehicleByLabelAndCompanyId", condition = "#result != null")
Vehicle findVehicleByLabelAndCompanyId(String label, long companyId) {

}

Now, with version 4, I have no way to do it using the condition closure.
I don't want to cache null values because, in the exemple, the vehicle may be inserted at some point, and the method will continue to return null.

Also, IMHO, I think it should be nice to keep this kind of compatibility with the previous version.

@kgeis
Copy link
Contributor

kgeis commented Jun 6, 2018

This could help with transitioning to newer versions of Grails that use ehcache 3. Version 2 allows null values, but 3 does not.

I think that Grails' Cacheable should try to follow Spring's. The solution would therefore be to add unless to mirror Spring's unless, in which "Unlike condition(), this expression is evaluated after the method has been called and can therefore refer to the result."

@KoloritnijClap
Copy link

KoloritnijClap commented Dec 26, 2019

The problem wasn't solved. If somebody came here with the same problem: solution that works for me is to override Ehcache.java file and add
if(value == null) { putObserver.end(PutOutcome.NOOP); }
Into put method

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

No branches or pull requests

4 participants