Results 1 to 3 of 3

Thread: @Cacheable method ignored when called from within the same class

  1. #1
    Join Date
    Jan 2012
    Posts
    16

    Default @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. #2
    Join Date
    Jun 2011
    Posts
    35

    Default

    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

  3. #3
    Join Date
    Jan 2012
    Posts
    16

    Default

    Thanks, I will work around this then

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •