Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 790 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Spring cache @Cacheable method ignored when called from within the same class

#1
I'm trying to call a `@Cacheable` method from within the same class:

@Cacheable(value = "defaultCache", key = "#id")
public Person findPerson(int id) {
return getSession().getPerson(id);
}

public List<Person> findPersons(int[] ids) {
List<Person> list = new ArrayList<Person>();
for (int id : ids) {
list.add(findPerson(id));
}
return list;
}

and hoping that the results from `findPersons` are cached as well, but the `@Cacheable` annotation is ignored, and `findPerson` method got executed everytime.

Am I doing something wrong here, or this is intended?
Reply

#2
For anyone using the **Grails Spring Cache** plugin, [a workaround is described in the documentation][1]. I had this issue on a grails app, but unfortunately the accepted answer seems to be unusable for Grails. The solution is ugly, IMHO, but it works.

The example code demonstrates it well:

class ExampleService {
def grailsApplication

def nonCachedMethod() {
grailsApplication.mainContext.exampleService.cachedMethod()
}

@Cacheable('cachedMethodCache')
def cachedMethod() {
// do some expensive stuff
}
}

Simply replace *exampleService.cachedMethod()* with your own service and method.

[1]:

[To see links please register here]

Reply

#3
This is because of the way proxies are created for handling caching, transaction related functionality in Spring. This is a very good reference of how Spring handles it - [Transactions, Caching and AOP: understanding proxy usage in Spring][1]

In short, a self call bypasses the dynamic proxy and any cross cutting concern like caching, transaction etc which is part of the dynamic proxies logic is also bypassed.

The fix is to use AspectJ compile time or load time weaving.


[1]:

[To see links please register here]

Reply

#4
Here is what I do for small projects with only marginal usage of method calls within the same class. In-code documentation is strongly advidsed, as it may look strage to colleagues. But its easy to test, simple, quick to achieve and spares me the full blown AspectJ instrumentation. However, for more heavy usage I'd advice the AspectJ solution.

@Service
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
class PersonDao {

private final PersonDao _personDao;

@Autowired
public PersonDao(PersonDao personDao) {
_personDao = personDao;
}

@Cacheable(value = "defaultCache", key = "#id")
public Person findPerson(int id) {
return getSession().getPerson(id);
}

public List<Person> findPersons(int[] ids) {
List<Person> list = new ArrayList<Person>();
for (int id : ids) {
list.add(_personDao.findPerson(id));
}
return list;
}
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through