新增定时任务 Redis文章转MongoDB

This commit is contained in:
WangHao
2020-10-18 18:27:21 +08:00
parent e7b6e1f5dc
commit 046aba8a28
9 changed files with 90 additions and 18 deletions

View File

@ -104,7 +104,32 @@ public class MongdbApplicationTests extends BaseSpringBootTest{
*/
@Test
public void redisToMgDB(){
//模糊查询 获取所有的key
Collection<String> listNote= redisCache.keys(Constants.NM_NOTE_CONTENT+"*");
for(String str:listNote){
//文章UUID
String mgDbContentUUID=str.replace(Constants.NM_NOTE_CONTENT,"");
System.out.println("str:"+str);
//文章
String redisContent= redisCache.getCacheObject(str).toString();
//查询mongoDb 存在就修改 不存在就新增
List<NoteContentMgDb> listMgDbContent = noteRepositoryService.findById(mgDbContentUUID);
if (listMgDbContent!=null&&!listMgDbContent.isEmpty()){
//修改
NoteContentMgDb noteContentMgDb = new NoteContentMgDb();
noteContentMgDb.setId(Long.valueOf(mgDbContentUUID));
noteContentMgDb.setNoteContent(redisContent);
noteRepositoryService.update(noteContentMgDb);
}else {
//新增
NoteContentMgDb noteContentMgDb = new NoteContentMgDb();
noteContentMgDb.setId(Long.valueOf(mgDbContentUUID));
noteContentMgDb.setNoteContent(redisContent);
noteRepositoryService.save(noteContentMgDb);
}
//删除对应缓存
//redisCache.deleteObject(str);
}
}