@Caching( evict = { @CacheEvict( value = UserEmailPreferenceRule.USER_EMAIL_PREF_RULE_CACHE_REGION, allEntries = true), @CacheEvict(value = Email.EMAILS_CACHE_KEY, allEntries = true) }) public UserEmailPreferenceRule save(String userId, String ruleId) { UserEmailPreferenceRule entity = new UserEmailPreferenceRule(); if (Strings.isNullOrEmpty(userId)) { entity.setUserId(SecurityContextHolder.getContext().getAuthentication().getName()); } else { entity.setUserId(userId); } if (findRuleForUser(entity.getUserId(), ruleId) != null) { return findRuleForUser(entity.getUserId(), ruleId); } entity.setEmailPreferenceRule(EmailPreferenceRuleDao.instance().find(ruleId)); return em.merge(entity); }