Announcement Announcement Module
Collapse
No announcement yet.
@Cacheable method ignored when called from within the same class Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Cacheable method ignored when called from within the same class

    Hi there,

    I'm trying to call a @cacheable method from within the same class:

    Code:
    @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?

    Thanks,

    David

  • #2
    Spring Cache is implemented using Sping AOP proxies so calls done between methods in the same object will not be passed through the proxy and there for not cached.
    See the spring reference manual[1] for more information.

    You can possibly solve this by forcing the use of CGILIB or switch to use AspectJ instead of the Spring AOP [2].

    [1] http://static.springsource.org/sprin...ng-aop-proxies
    [2] http://static.springsource.org/sprin...uction-proxies

    Best regards
    Pontus Ullgren

    Comment


    • #3
      Thanks, I will work around this then

      Comment

      Working...
      X