1.evict journal_query_page by comment update

2.clean journal_query_page by journal add/delete/update/schedule publish
main
Gary 9 months ago
parent 5adc76030a
commit 31b975a593

@ -2,6 +2,10 @@ package com.luoo.music.config;
import com.luoo.music.dao.JournalDao;
import com.luoo.music.pojo.Journal;
import constants.Constants;
import net.oschina.j2cache.CacheChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
@ -25,6 +29,9 @@ public class StartupRunner implements ApplicationRunner {
public static Map<String, ScheduledFuture<?>> taskMap = new HashMap<>();
@Autowired
private CacheChannel cacheChannel;
@Autowired
private JournalDao journalDao;
@Autowired
@ -63,6 +70,7 @@ public class StartupRunner implements ApplicationRunner {
@Override
public void run() {
journalDao.updatePubById(id, pubTime);
cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE);
taskMap.remove(id);
}
}

@ -2,6 +2,8 @@ package com.luoo.music.controller;
import api.PageResult;
import api.Result;
import constants.Constants;
import com.luoo.music.dto.request.cms.JournalAddModel;
import com.luoo.music.dto.request.cms.JournalPublishReq;
import com.luoo.music.dto.request.cms.JournalQueryModel;
@ -12,6 +14,8 @@ import com.luoo.music.service.S3Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.oschina.j2cache.CacheChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@ -31,6 +35,9 @@ public class CMSJournalController {
private CMSJournalService journalService;
@Autowired
private S3Service s3Service;
@Autowired
private CacheChannel cacheChannel;
@ApiOperation(value = "查询期刊列表", notes = "查询期刊列表")
@RequestMapping(value="/search/{page}/{size}", method= RequestMethod.POST)
@ -43,6 +50,7 @@ public class CMSJournalController {
@ApiOperation(value = "新增期刊", notes = "新增期刊")
@RequestMapping(value = "/add", method = RequestMethod.POST)
public Result add(@ApiParam(value = "期刊信息", required = true) @Valid @RequestBody JournalAddModel addModel){
cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE);
return journalService.add(addModel);
}
@ -56,6 +64,7 @@ public class CMSJournalController {
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public Result update(@ApiParam(value = "期刊ID", required = true) @PathVariable String id,
@ApiParam(value = "期刊信息", required = true) @RequestBody JournalAddModel updateModel){
cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE);
return journalService.update(id, updateModel);
}
@ -70,12 +79,14 @@ public class CMSJournalController {
@RequestMapping(value="/update/state/{id}",method= RequestMethod.PUT)
public Result updateColumnState(@ApiParam(value = "期刊ID", required = true) @PathVariable String id,
@ApiParam(value = "期刊启停状态0:停用1:启用", required = true) @RequestBody String state){
cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE);
return journalService.updateJournalState(id, state);
}
@ApiOperation(value = "删除单条期刊", notes = "物理删除")
@RequestMapping(value="/{id}", method= RequestMethod.DELETE)
public Result delete(@ApiParam(value = "期刊ID", required = true) @PathVariable String id){
cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE);
return journalService.deleteById(id);
}

@ -2,13 +2,17 @@ package com.luoo.music.dto.request;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import util.StringTools;
import java.io.Serializable;
/**
*
*/
@Data
@Getter
@Setter
@ApiModel
public class JournalQueryReq implements Serializable {
private static final long serialVersionUID = -1198060864891902188L;
@ -20,4 +24,13 @@ public class JournalQueryReq implements Serializable {
private Integer pageNum = 1;
@ApiModelProperty(value = "分页: 每页数量", example = "10")
private Integer pageSize = 10;
@Override
public String toString() {
return pageNum + "_" + pageSize + "_" + getString(categoryId) + "_" + getString(journalNoRange);
}
private String getString(String value) {
return StringTools.isEmpty(value) ? null : value;
}
}

@ -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…
Cancel
Save