1.async clean cache when update/delete/add journal

main
Gary 5 months ago
parent c2616cc918
commit dfd43ffae5

@ -13,6 +13,8 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableAsync;
import util.IdWorker;
import util.JwtUtil;
@ -27,6 +29,7 @@ import java.net.UnknownHostException;
@EnableFeignClients
@EnableJpaAuditing
@EnableCaching
@EnableAsync
public class MusicApplication {
static Logger logger= LoggerFactory.getLogger(MusicApplication.class);
public static void main(String[] args) throws UnknownHostException {

@ -9,6 +9,7 @@ import com.luoo.music.dto.request.cms.JournalPublishReq;
import com.luoo.music.dto.request.cms.JournalQueryModel;
import com.luoo.music.dto.response.cms.JournalListRespDto;
import com.luoo.music.dto.response.cms.JournalRespDto;
import com.luoo.music.listener.CleanJournalCache;
import com.luoo.music.service.CMSJournalService;
import com.luoo.music.service.S3Service;
import io.swagger.annotations.Api;
@ -35,9 +36,8 @@ public class CMSJournalController {
private CMSJournalService journalService;
@Autowired
private S3Service s3Service;
@Autowired
private CacheChannel cacheChannel;
private CleanJournalCache cleanJournalCache;
@ApiOperation(value = "查询期刊列表", notes = "查询期刊列表")
@RequestMapping(value="/search/{page}/{size}", method= RequestMethod.POST)
@ -50,20 +50,10 @@ public class CMSJournalController {
@ApiOperation(value = "新增期刊", notes = "新增期刊")
@RequestMapping(value = "/add", method = RequestMethod.POST)
public Result add(@ApiParam(value = "期刊信息", required = true) @Valid @RequestBody JournalAddModel addModel){
cleanCache();
cleanJournalCache.cleanCache();
return journalService.add(addModel);
}
private void cleanCache() {
try {
cacheChannel.evict("default", "journal_filter");
cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE);
cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_QUERY_COUNT);
}catch(Exception e) {
e.printStackTrace();
}
}
@ApiOperation(value = "查询期刊详情", notes = "查询期刊详情")
@RequestMapping(value="/{id}",method= RequestMethod.GET)
public Result<JournalRespDto> findOne(@ApiParam(value = "期刊ID", required = true) @PathVariable String id){
@ -74,7 +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){
cleanCache(id);
cleanJournalCache.cleanCache(id);
return journalService.update(id, updateModel);
}
@ -82,36 +72,22 @@ public class CMSJournalController {
@RequestMapping(value="/publish/{id}",method= RequestMethod.PUT)
public Result publish(@ApiParam(value = "期刊ID", required = true) @PathVariable String id,
@ApiParam(value = "期刊发布请求对象", required = true) @RequestBody JournalPublishReq queryModel){
cleanCache(id);
cleanJournalCache.cleanCache(id);
return journalService.publish(id, queryModel);
}
private void cleanCache(String id) {
try {
cacheChannel.evict("default", "journal_filter");
cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE);
cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_QUERY_COUNT);
cacheChannel.evict(constants.Constants.J2CACHE_REGION_JOURNAL_ID, id);
String journalNo=journalService.findJournalNoById(id);
cacheChannel.evict(constants.Constants.J2CACHE_REGION_JOURNAL_SONG_LIST, journalNo);
cacheChannel.evict(constants.Constants.J2CACHE_REGION_JOURNAL_NO,journalNo);
}catch(Exception e) {
e.printStackTrace();
}
}
@ApiOperation(value = "更新期刊启停状态", notes = "更新期刊启停状态")
@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){
cleanCache(id);
cleanJournalCache.cleanCache(id);
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){
cleanCache(id);
cleanJournalCache.cleanCache(id);
return journalService.deleteById(id);
}

@ -3,6 +3,7 @@ package com.luoo.music.listener;
import constants.Constants;
import net.oschina.j2cache.CacheChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import com.luoo.music.dao.JournalDao;
@ -20,7 +21,37 @@ public class CleanJournalCache {
private CacheChannel cacheChannel;
@Autowired
private JournalDao journalDao;
@Async
public void cleanCache() {
try {
cacheChannel.evict("default", "journal_filter");
cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE);
cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_QUERY_COUNT);
}catch(Exception e) {
e.printStackTrace();
}
}
@Async
public void cleanCache(String id) {
try {
cacheChannel.evict("default", "journal_filter");
cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE);
cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_QUERY_COUNT);
cacheChannel.evict(constants.Constants.J2CACHE_REGION_JOURNAL_ID, id);
Optional<Journal> optional = journalDao.findById(id);
if (!optional.isPresent()) {
return;
}
String journalNo=optional.get().getJournalNo();
cacheChannel.evict(constants.Constants.J2CACHE_REGION_JOURNAL_SONG_LIST, journalNo);
cacheChannel.evict(constants.Constants.J2CACHE_REGION_JOURNAL_NO,journalNo);
}catch(Exception e) {
e.printStackTrace();
}
}
public void clean(String journalId) {
Optional<Journal> optional = journalDao.findById(journalId);
if (!optional.isPresent()) {

Loading…
Cancel
Save