文章缓存 redis定时转移到mongoDb

This commit is contained in:
WangHao
2020-10-12 23:29:13 +08:00
parent 11dc911d2b
commit 9ab209fe6b
15 changed files with 467 additions and 117 deletions

View File

@ -8,8 +8,7 @@ import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.ruoyi.common.annotation.Excel;
import javax.persistence.Column;
import javax.persistence.Table;
import javax.persistence.*;
import java.util.Date;
/**
@ -27,6 +26,8 @@ public class NmNote
private static final long serialVersionUID = 1L;
/** Note便签ID */
@Id
@GeneratedValue(generator = "JDBC")//返回自增长主键
@Column(name = "note_id")
@Excel(name = "Note便签ID")
private Long noteId;
@ -112,4 +113,12 @@ public class NmNote
private Date updateTime;
@Column(name = "tiymce_ueditor")
private Long tiymceUeditor;
/**富文本文章内容*/
@Transient
private String UeditorContent;
}

View File

@ -66,4 +66,20 @@ public interface INmNoteService
* @return 便签管理
*/
public NmNote selectNmNoteuserById(Long noteId,Long userID);
/**
* 用户便签内容 实时储存到数据库
*
* @param nmNote
* @return
*/
public int userUpdateNote(NmNote nmNote);
/**
* redis的文章 转移更新到MongoDb
*
* @param
* @return
*/
public void redisToMongonDB();
}

View File

@ -1,6 +1,10 @@
package com.ruoyi.note.service;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Filters;
import com.ruoyi.note.domain.NoteContentMgDb;
import com.sun.corba.se.spi.ior.ObjectId;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.List;
@ -18,4 +22,7 @@ public interface INoteRepositoryService {
List<NoteContentMgDb> findAll();
void delete(Integer id);
//根据id查询
List<NoteContentMgDb> findById(String id);
}

View File

@ -1,13 +1,31 @@
package com.ruoyi.note.service.impl;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import cn.hutool.core.date.DateUtil;
import com.github.wujun234.uid.UidGenerator;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.note.domain.NmNoteContent;
import com.ruoyi.note.domain.NoteContentMgDb;
import com.ruoyi.note.service.INmNoteContentService;
import com.ruoyi.note.service.INoteRepositoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.stereotype.Service;
import com.ruoyi.note.mapper.NmNoteMapper;
import com.ruoyi.note.domain.NmNote;
import com.ruoyi.note.service.INmNoteService;
import javax.annotation.Resource;
/**
* 便签管理Service业务层处理
*
@ -15,11 +33,23 @@ import com.ruoyi.note.service.INmNoteService;
* @date 2020-09-12
*/
@Service
public class NmNoteServiceImpl implements INmNoteService
{
public class NmNoteServiceImpl implements INmNoteService {
@Autowired
private NmNoteMapper nmNoteMapper;
@Resource
private UidGenerator defaultUidGenerator;
@Autowired
private INmNoteContentService nmNoteContentService;
@Autowired
private INoteRepositoryService noteRepositoryService;
@Autowired
private RedisCache redisCache;
/**
* 查询便签管理
*
@ -27,8 +57,7 @@ public class NmNoteServiceImpl implements INmNoteService
* @return 便签管理
*/
@Override
public NmNote selectNmNoteById(Long noteId)
{
public NmNote selectNmNoteById(Long noteId) {
return nmNoteMapper.selectNmNoteById(noteId);
}
@ -39,8 +68,7 @@ public class NmNoteServiceImpl implements INmNoteService
* @return 便签管理
*/
@Override
public List<NmNote> selectNmNoteList(NmNote nmNote)
{
public List<NmNote> selectNmNoteList(NmNote nmNote) {
return nmNoteMapper.selectNmNoteList(nmNote);
}
@ -51,10 +79,11 @@ public class NmNoteServiceImpl implements INmNoteService
* @return 结果
*/
@Override
public int insertNmNote(NmNote nmNote)
{
public int insertNmNote(NmNote nmNote) {
nmNote.setCreateTime(DateUtils.getNowDate());
return nmNoteMapper.insertNmNote(nmNote);
nmNote.setTitle(DateUtil.now());
nmNote.setTiymceUeditor(defaultUidGenerator.getUID());
return nmNoteMapper.insertSelective(nmNote);
}
/**
@ -64,8 +93,7 @@ public class NmNoteServiceImpl implements INmNoteService
* @return 结果
*/
@Override
public int updateNmNote(NmNote nmNote)
{
public int updateNmNote(NmNote nmNote) {
nmNote.setUpdateTime(DateUtils.getNowDate());
return nmNoteMapper.updateNmNote(nmNote);
}
@ -77,8 +105,7 @@ public class NmNoteServiceImpl implements INmNoteService
* @return 结果
*/
@Override
public int deleteNmNoteByIds(Long[] noteIds)
{
public int deleteNmNoteByIds(Long[] noteIds) {
return nmNoteMapper.deleteNmNoteByIds(noteIds);
}
@ -89,8 +116,7 @@ public class NmNoteServiceImpl implements INmNoteService
* @return 结果
*/
@Override
public int deleteNmNoteById(Long noteId)
{
public int deleteNmNoteById(Long noteId) {
return nmNoteMapper.deleteNmNoteById(noteId);
}
@ -101,11 +127,71 @@ public class NmNoteServiceImpl implements INmNoteService
* @return 便签管理
*/
@Override
public NmNote selectNmNoteuserById(Long noteId,Long userID) {
public NmNote selectNmNoteuserById(Long noteId, Long userID) {
NmNote nmNote = new NmNote();
nmNote.setNoteId(noteId);
nmNote.setUserId(userID);
return nmNoteMapper.selectOne(nmNote);
NmNote isnmNote1 = nmNoteMapper.selectOne(nmNote);
//查询对应的文章数据
//1.查redis缓存
String noteContent = redisCache.getCacheObject(Constants.NM_NOTE_CONTENT + isnmNote1.getTiymceUeditor());
if (noteContent != null && !"".equals(noteContent)) {
isnmNote1.setUeditorContent(noteContent);
} else {
// 2不存在就走mogodb
List<NoteContentMgDb> NoteContentMgDb = noteRepositoryService.findById(isnmNote1.getTiymceUeditor() + "");
isnmNote1.setUeditorContent(NoteContentMgDb.get(0).getNoteContent());
}
return isnmNote1;
}
/**
* 实时修改数据 保存到缓存中
*
* @param nmNote
* @return int
*/
@Override
public int userUpdateNote(NmNote nmNote) {
//储存到redis中 只缓存频繁操作的文章内容
redisCache.setCacheObject(Constants.NM_NOTE_CONTENT+nmNote.getTiymceUeditor(),nmNote.getUeditorContent());
return 1;
}
/**
* redis的文章 转移更新到MongoDb
*
* @param
* @return
*/
@Override
public void redisToMongonDB() {
//模糊查询 获取所有的key
Collection<String> listNote= redisCache.keys(Constants.NM_NOTE_CONTENT+"*");
for(String str:listNote){
//文章UUID
String mgDbContentUUID=str.replace(Constants.NM_NOTE_CONTENT,"");
//文章
String redisContent= redisCache.getCacheObject(str);
//查询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);
}
}

View File

@ -1,10 +1,15 @@
package com.ruoyi.note.service.impl;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Filters;
import com.ruoyi.note.domain.NoteContentMgDb;
import com.ruoyi.note.service.INoteRepositoryService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
@ -23,6 +28,8 @@ public class NoteRepositoryServiceImpl implements INoteRepositoryService {
@Autowired
private MongoTemplate mongoTemplate;
/**
* 新增信息
* @param student
@ -66,4 +73,16 @@ public class NoteRepositoryServiceImpl implements INoteRepositoryService {
NoteContentMgDb byId = mongoTemplate.findById(1, NoteContentMgDb.class);
mongoTemplate.remove(byId);
}
@Override
public List<NoteContentMgDb> findById(String id) {
//修改的条件
Query query = new Query(Criteria.where("_id").is(Long.valueOf(id)));
//修改的内容
return mongoTemplate.find(query, NoteContentMgDb.class);
}
}

View File

@ -22,11 +22,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<result property="isEncryption" column="is_encryption" />
<result property="createTime" column="create_time" />
<result property="updateTime" column="update_time" />
<result property="tiymceUeditor" column="tiymce_ueditor" />
</resultMap>
<sql id="selectNmNoteVo">
select note_id, user_id, title, description, menu_id, background, note_count, note_sort, is_state, read_progress, is_star, is_delete, top_flag, is_share, is_encryption, create_time, update_time from nm_note
select note_id, user_id, title, description, menu_id, background, note_count, note_sort, is_state, read_progress, is_star, is_delete, top_flag, is_share, is_encryption, create_time, update_time,tiymce_ueditor from nm_note
</sql>
<select id="selectNmNoteList" parameterType="NmNote" resultMap="NmNoteResult">
@ -47,7 +48,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="topFlag != null "> and top_flag = #{topFlag}</if>
<if test="isShare != null "> and is_share = #{isShare}</if>
<if test="isEncryption != null "> and is_encryption = #{isEncryption}</if>
<if test="tiymceUeditor != null "> and tiymce_ueditor = #{tiymceUeditor}</if>
</where>
order by create_time desc
</select>
<select id="selectNmNoteById" parameterType="Long" resultMap="NmNoteResult">
@ -75,6 +78,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="isEncryption != null">is_encryption,</if>
<if test="createTime != null">create_time,</if>
<if test="updateTime != null">update_time,</if>
<if test="tiymceUeditor != null">tiymce_ueditor,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="noteId != null">#{noteId},</if>
@ -94,6 +98,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="isEncryption != null">#{isEncryption},</if>
<if test="createTime != null">#{createTime},</if>
<if test="updateTime != null">#{updateTime},</if>
<if test="tiymceUeditor != null">#{tiymceUeditor},</if>
</trim>
</insert>
@ -116,6 +121,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="isEncryption != null">is_encryption = #{isEncryption},</if>
<if test="createTime != null">create_time = #{createTime},</if>
<if test="updateTime != null">update_time = #{updateTime},</if>
<if test="tiymceUeditor != null">tiymce_ueditor = #{tiymceUeditor},</if>
</trim>
where note_id = #{noteId}
</update>