1

I've got next method in UserService:

@Cacheable(value = "user", key="#p0")
public User find(String user) {
    return userRepository.findByUser(User);
}

It caches well. In other service I have:

@Transactional
public void updateToken(int id, String token) {
    Group group = groupRepository.findOne(id);
    group.getMembers().forEach(member -> {
        member.getUser().setToken(token);
        removeUserCacheByName(member.getUser().getName());
    });
    groupRepository.save(group);
}

@CacheEvict(value = "user", key="#p0")
public void removeUserCacheByName(String name) {
    log.debug("Removing user cache by name {}.", name);
}

After updateToken method, cache does not clear.

Boris
  • 4,944
  • 7
  • 36
  • 69
  • see [this](http://stackoverflow.com/a/14077831/1910582) – Bond - Java Bond Jul 26 '16 at 06:37
  • 1
    It looks like your configuration is try – Sam Jul 26 '16 at 07:40
  • I've tried @EnableCaching(mode = AdviceMode.ASPECTJ) and it doesn't work too. – Boris Jul 26 '16 at 08:57
  • Try all entry eviction, this will help to isolate if there is any issues with key. @CacheEvict(value = "user", allEntries=true) – Sam Jul 27 '16 at 05:29
  • This is not the solution, because then there is no sense in cache at all in my case. – Boris Jul 27 '16 at 05:31
  • you are right. I believe there is some issue with the key setting, if it is confirmed we can look for options to solve the issues with key – Sam Jul 27 '16 at 05:51
  • It may refer to my [another question](http://stackoverflow.com/questions/38581039/ehcache-with-spring-cache-assigns-wrong-key). – Boris Jul 27 '16 at 05:54

1 Answers1

2

What you're seeing is normal. You're calling removeUserCacheByName() from within the Proxy object so the catching aspect doesn't execute. You have this behaviour explained in the documentation.

You can do some things to work around this:

1) Take the evict method (removeUserCacheByName) to another bean, autowire it in updateToken()'s class, and call it.

2) An ugly but useful one, autowire the ApplicationContext, get the same object from it and call the method, e.g.:

public class UserService{

    @Autowired
    private ApplicationContext ac;

    @Transactional
    public void updateToken(int id, String token) {
        Group group = groupRepository.findOne(id);
        group.getMembers().forEach(member -> {
            member.getUser().setToken(token);
            UserService sameBean = ac.getBean(UserService.class);
            sameBean.removeUserCacheByName(member.getUser().getName());
        });
        groupRepository.save(group);
    }

    @CacheEvict(value = "user", key="#p0")
    public void removeUserCacheByName(String name) {
        log.debug("Removing user cache by name {}.", name);
    }

}
Robin
  • 1,438
  • 2
  • 19
  • 29
codependent
  • 23,193
  • 31
  • 166
  • 308
  • Both doesn't work for me. In the first case, I moved 'removeUserCacheByName' to UserService and invoked 'updateToken' from GroupService. In the second, I stored both methods together in one service. May it refer to my [another question](http://stackoverflow.com/questions/38581039/ehcache-with-spring-cache-assigns-wrong-key)? – Boris Jul 26 '16 at 06:59