2.clean journal_query_page by journal add/delete/update/schedule publishmain
parent
5adc76030a
commit
31b975a593
@ -0,0 +1,65 @@
|
||||
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;
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package com.luoo.music.listener;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class CleanJournalQueryPageListenerTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
List<Integer> list = new ArrayList<>();
|
||||
for (int i = 1; i < 1024; i++) {
|
||||
list.add(i);
|
||||
}
|
||||
Collections.reverse(list);
|
||||
assertTrue(0 == getPageNum(1024, list));
|
||||
assertTrue(103 == getPageNum(1, list));
|
||||
}
|
||||
|
||||
private int getPageNum(int value, List<Integer> list) {
|
||||
if (value > list.get(0)) {
|
||||
return 0;
|
||||
}
|
||||
int pageSize = 10;
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue