Spring cache @Cacheable method ignored when called from within the same class

前端 未结 3 1047
无人共我
无人共我 2020-11-27 04:08

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

@Cacheable(value = \"defaultCache\", key = \"#id\")
public Person findPerson         


        
3条回答
  •  孤街浪徒
    2020-11-27 04:26

    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 findPersons(int[] ids) {
            List list = new ArrayList();
            for (int id : ids) {
                list.add(_personDao.findPerson(id));
            }
            return list;
        }
    }
    

提交回复
热议问题