1.clean code

main
Gary 10 months ago
parent c7795a7ba0
commit 75ff660319

@ -8,7 +8,6 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ -25,6 +24,7 @@ import com.luoo.music.dao.JournalDao;
import com.luoo.music.dao.JournalSongDao;
import com.luoo.music.dao.TagDao;
import com.luoo.music.ik.lucene.IKAnalyzer;
import com.luoo.music.listener.CleanJournalCache;
import com.luoo.music.pojo.Journal;
import com.luoo.music.pojo.JournalSong;
import com.luoo.music.util.Constants;
@ -44,6 +44,9 @@ public class SaticScheduleTask {
private CacheChannel cacheChannel;
@Autowired
private TagDao tagDao;
@Autowired
private CleanJournalCache cleanJournalCache;
@PostConstruct
private void init() {
@ -53,11 +56,7 @@ public class SaticScheduleTask {
// 1.凌晨4点执行更新期刊tag
@Scheduled(cron = "0 0 4 * * *")
private void updateJouranlTag() {
AtomicLong updateCounter = new AtomicLong();
journalDao.findValidJournals().parallelStream().forEach(j -> updateJouranlTag(j, updateCounter));
if (updateCounter.get() > 0) {
cacheChannel.clear(constants.Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE);
}
journalDao.findValidJournals().parallelStream().forEach(this::updateJouranlTag);
}
// 2.凌晨4:20点执行更新分词
@ -99,16 +98,14 @@ public class SaticScheduleTask {
return map;
}
private void updateJouranlTag(Journal journal, AtomicLong updateCounter) {
private void updateJouranlTag(Journal journal) {
List<String> tags = tagDao.getTagNameChByJournalId(journal.getId());
if (!tags.isEmpty()) {
String tagStr = tags.stream().distinct().sorted().collect(Collectors.joining(Constants.COMMA));
if (!tagStr.equals(journal.getNameChTags())) {
journal.setNameChTags(tagStr);
journalDao.save(journal);
this.cacheChannel.evict(constants.Constants.J2CACHE_REGION_JOURNAL_ID, journal.getId());
this.cacheChannel.evict(constants.Constants.J2CACHE_REGION_JOURNAL_NO,journal.getJournalNo());
updateCounter.incrementAndGet();
cleanJournalCache.clean(journal);
}
}
}

@ -0,0 +1,67 @@
package com.luoo.music.listener;
import constants.Constants;
import net.oschina.j2cache.CacheChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.luoo.music.dao.JournalDao;
import com.luoo.music.dto.request.JournalQueryReq;
import com.luoo.music.pojo.Journal;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@Component
public class CleanJournalCache {
private static final int DEFAULT_PAGE_SIZE = 10;
@Autowired
private CacheChannel cacheChannel;
@Autowired
private JournalDao journalDao;
public void clean(String journalId) {
Optional<Journal> optional = journalDao.findById(journalId);
if (!optional.isPresent()) {
return;
}
cacheChannel.evict(constants.Constants.J2CACHE_REGION_JOURNAL_SONG_LIST, journalId);
clean(optional.get());
}
public void clean(Journal journal) {
String journalQueryPageKey = getJournalQueryPageKey(journal);
cacheChannel.evict(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE, journalQueryPageKey);
cacheChannel.evict(Constants.J2CACHE_REGION_JOURNAL_ID, journal.getId());
cacheChannel.evict(Constants.J2CACHE_REGION_JOURNAL_NO, journal.getJournalNo());
}
private String getJournalQueryPageKey(Journal journal) {
List<Integer> journalNos = journalDao.findValidJournals().stream().map(j -> Integer.valueOf(j.getJournalNo()))
.sorted().collect(Collectors.toList());
Collections.reverse(journalNos);
JournalQueryReq journalQueryReq = new JournalQueryReq();
journalQueryReq.setPageNum(getPageNum(Integer.valueOf(journal.getJournalNo()), journalNos));
journalQueryReq.setPageSize(DEFAULT_PAGE_SIZE);
return journalQueryReq.toString();
}
private int getPageNum(int value, List<Integer> list) {
if (value > list.get(0)) {
return 0;
}
int pageSize = DEFAULT_PAGE_SIZE;
for (int pageNum = 1; (pageNum - 1) * pageSize < list.size(); pageNum++) {
int startIndex = (pageNum - 1) * pageSize;
int endIndex = pageNum * pageSize - 1;
if (endIndex > list.size()) {
return pageNum;
}
if (list.get(startIndex) >= value && list.get(endIndex) <= value) {
return pageNum;
}
}
return 0;
}
}

@ -1,68 +1,19 @@
package com.luoo.music.listener;
import constants.Constants;
import net.oschina.j2cache.CacheChannel;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.luoo.music.dao.JournalDao;
import com.luoo.music.dto.request.JournalQueryReq;
import com.luoo.music.pojo.Journal;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@Component
@RabbitListener(queues = Constants.RABBIT_MESSAGE_CLEAN_JOURANL_QUERY_PAGE)
public class CleanJournalQueryPageListener {
private static final int DEFAULT_PAGE_SIZE = 10;
@Autowired
private CacheChannel cacheChannel;
@Autowired
private JournalDao journalDao;
private CleanJournalCache cleanJournalCache;
@RabbitHandler
public void excuteClearCache(String journalId) {
Optional<Journal> optional = journalDao.findById(journalId);
if (!optional.isPresent()) {
return;
}
String journalQueryPageKey = getJournalQueryPageKey(optional.get());
cacheChannel.evict(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE, journalQueryPageKey);
cacheChannel.evict(Constants.J2CACHE_REGION_JOURNAL_ID,journalId);
cacheChannel.evict(constants.Constants.J2CACHE_REGION_JOURNAL_SONG_LIST, journalId);
cacheChannel.evict(Constants.J2CACHE_REGION_JOURNAL_NO,optional.get().getJournalNo());
}
private String getJournalQueryPageKey(Journal journal) {
List<Integer> journalNos = journalDao.findValidJournals().stream().map(j -> Integer.valueOf(j.getJournalNo()))
.sorted().collect(Collectors.toList());
Collections.reverse(journalNos);
JournalQueryReq journalQueryReq = new JournalQueryReq();
journalQueryReq.setPageNum(getPageNum(Integer.valueOf(journal.getJournalNo()), journalNos));
journalQueryReq.setPageSize(DEFAULT_PAGE_SIZE);
return journalQueryReq.toString();
}
private int getPageNum(int value, List<Integer> list) {
if (value > list.get(0)) {
return 0;
}
int pageSize = DEFAULT_PAGE_SIZE;
for (int pageNum = 1; (pageNum - 1) * pageSize < list.size(); pageNum++) {
int startIndex = (pageNum - 1) * pageSize;
int endIndex = pageNum * pageSize - 1;
if (endIndex > list.size()) {
return pageNum;
}
if (list.get(startIndex) >= value && list.get(endIndex) <= value) {
return pageNum;
}
}
return 0;
cleanJournalCache.clean(journalId);
}
}

@ -1,26 +1,20 @@
package com.luoo.music.listener;
import constants.Constants;
import net.oschina.j2cache.CacheChannel;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.luoo.music.dao.JournalDao;
import com.luoo.music.dto.request.JournalQueryReq;
import com.luoo.music.pojo.Journal;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@Component
@RabbitListener(queues = Constants.RABBIT_MESSAGE_USER_COLLECT_JOURNAL)
public class UserCollectJournalListener {
private static final int DEFAULT_PAGE_SIZE = 10;
@Autowired
private CacheChannel cacheChannel;
private CleanJournalCache cleanJournalCache;
@Autowired
private JournalDao journalDao;
@ -39,41 +33,6 @@ public class UserCollectJournalListener {
int userCollectCount = journal.getUserCollectCount();
journal.setUserCollectCount(isReduceCount ? (userCollectCount - 1) : (userCollectCount + 1));
journalDao.save(journal);
excuteClearCache(journal);
}
private void excuteClearCache(Journal journal) {
String journalQueryPageKey = getJournalQueryPageKey(journal);
cacheChannel.evict(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE, journalQueryPageKey);
cacheChannel.evict(Constants.J2CACHE_REGION_JOURNAL_ID, journal.getId());
cacheChannel.evict(Constants.J2CACHE_REGION_JOURNAL_NO, journal.getJournalNo());
}
private String getJournalQueryPageKey(Journal journal) {
List<Integer> journalNos = journalDao.findValidJournals().stream().map(j -> Integer.valueOf(j.getJournalNo()))
.sorted().collect(Collectors.toList());
Collections.reverse(journalNos);
JournalQueryReq journalQueryReq = new JournalQueryReq();
journalQueryReq.setPageNum(getPageNum(Integer.valueOf(journal.getJournalNo()), journalNos));
journalQueryReq.setPageSize(DEFAULT_PAGE_SIZE);
return journalQueryReq.toString();
}
private int getPageNum(int value, List<Integer> list) {
if (value > list.get(0)) {
return 0;
}
int pageSize = DEFAULT_PAGE_SIZE;
for (int pageNum = 1; (pageNum - 1) * pageSize < list.size(); pageNum++) {
int startIndex = (pageNum - 1) * pageSize;
int endIndex = pageNum * pageSize - 1;
if (endIndex > list.size()) {
return pageNum;
}
if (list.get(startIndex) >= value && list.get(endIndex) <= value) {
return pageNum;
}
}
return 0;
cleanJournalCache.clean(journal);
}
}

Loading…
Cancel
Save