Commit 4a518ace authored by 技术部-任文超's avatar 技术部-任文超

重新优化,重定义缓存Key(读、写时更新缓存)

parent 382d8437
......@@ -7,16 +7,19 @@ import cn.quantgroup.xyqb.repository.IContactRepository;
import cn.quantgroup.xyqb.service.user.IContactService;
import cn.quantgroup.xyqb.util.ValidationUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.bcel.classfile.Constant;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
/**
......@@ -29,35 +32,30 @@ public class ContactServiceImpl implements IContactService {
private IContactRepository contactRepository;
@Override
@Cacheable(value = "contact", key = "'contact:' + #trim + #userId", unless = "#result == null or #result.size() == 0", cacheManager = "cacheManager")
public List<Contact> findByUserId(Long userId, boolean trim) {
List<Contact> contacts = findByUserId(userId);
List<Contact> contacts = contactRepository.findByUserId(userId);
if(trim){
return trim(contacts);
trim(contacts);
}
return contacts;
}
@Cacheable(value = "contact", key = "'contact' + #userId", unless = "#result == null or #result.size() == 0", cacheManager = "cacheManager")
public List<Contact> findByUserId(Long userId) {
return contactRepository.findByUserId(userId);
}
/**
* 清除非法联系人记录
* 过滤掉非法联系人记录
* @param contacts - 包含待清除记录的联系人列表
* @return 只包含合法联系人记录的列表
*/
private List<Contact> trim(List<Contact> contacts){
private void trim(List<Contact> contacts){
if(CollectionUtils.isEmpty(contacts)){
return Collections.emptyList();
return ;
}
List<Contact> trimList = new ArrayList<Contact>(contacts.size());
for(Contact contact : contacts){
if (contact.valid()) {
trimList.add(contact);
Iterator<Contact> iterator = contacts.iterator();
while(iterator.hasNext()){
Contact contact = iterator.next();
if (!contact.valid()) {
iterator.remove();
}
}
return trimList;
}
@Override
......@@ -66,7 +64,9 @@ public class ContactServiceImpl implements IContactService {
}
@Override
@CacheEvict(value = "contact", key = "'contact' + #userId", cacheManager = "cacheManager")
@Caching(evict = {
@CacheEvict(value = "contact", key = "'contact:true' + #userId", cacheManager = "cacheManager"),
@CacheEvict(value = "contact", key = "'contact:false' + #userId", cacheManager = "cacheManager")})
public List<Contact> save(Long userId, List<Contact> contacts) {
if(userId == null){
return null;
......@@ -77,7 +77,9 @@ public class ContactServiceImpl implements IContactService {
}
@Override
@CacheEvict(value = "contact", key = "'contact' + #contact.userId", cacheManager = "cacheManager")
@Caching(evict = {
@CacheEvict(value = "contact", key = "'contact:true' + #contact.userId", cacheManager = "cacheManager"),
@CacheEvict(value = "contact", key = "'contact:false' + #contact.userId", cacheManager = "cacheManager")})
public Contact save(Contact contact) {
return contactRepository.save(contact);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment