fix: 同类推荐过滤无章节小说

This commit is contained in:
xiongxiaoyang 2022-05-29 20:36:31 +08:00
parent 785646b4c4
commit ed882abbd1
3 changed files with 12 additions and 3 deletions

View File

@ -93,6 +93,8 @@ public class DatabaseConsts {
public static final String COLUMN_VISIT_COUNT = "visit_count"; public static final String COLUMN_VISIT_COUNT = "visit_count";
public static final String COLUMN_WORD_COUNT = "word_count";
public static final String COLUMN_LAST_CHAPTER_UPDATE_TIME = "last_chapter_update_time"; public static final String COLUMN_LAST_CHAPTER_UPDATE_TIME = "last_chapter_update_time";
} }

View File

@ -9,6 +9,7 @@ import io.github.xxyopen.novel.dao.mapper.BookChapterMapper;
import io.github.xxyopen.novel.dao.mapper.BookInfoMapper; import io.github.xxyopen.novel.dao.mapper.BookInfoMapper;
import io.github.xxyopen.novel.dto.resp.BookInfoRespDto; import io.github.xxyopen.novel.dto.resp.BookInfoRespDto;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -72,6 +73,11 @@ public class BookInfoCacheManager {
.build(); .build();
} }
@CacheEvict(cacheManager = CacheConsts.CAFFEINE_CACHE_MANAGER,
value = CacheConsts.BOOK_INFO_CACHE_NAME)
public void evictBookInfoCache(Long ignoredId) {
// 调用此方法自动清除小说信息的缓存
}
/** /**
* 查询每个类别下最新更新的 500 个小说ID列表并放入缓存中 1 个小时 * 查询每个类别下最新更新的 500 个小说ID列表并放入缓存中 1 个小时
@ -81,6 +87,7 @@ public class BookInfoCacheManager {
public List<Long> getLastUpdateIdList(Long categoryId) { public List<Long> getLastUpdateIdList(Long categoryId) {
QueryWrapper<BookInfo> queryWrapper = new QueryWrapper<>(); QueryWrapper<BookInfo> queryWrapper = new QueryWrapper<>();
queryWrapper.eq(DatabaseConsts.BookTable.COLUMN_CATEGORY_ID, categoryId) queryWrapper.eq(DatabaseConsts.BookTable.COLUMN_CATEGORY_ID, categoryId)
.gt(DatabaseConsts.BookTable.COLUMN_WORD_COUNT,0)
.orderByDesc(DatabaseConsts.BookTable.COLUMN_LAST_CHAPTER_UPDATE_TIME) .orderByDesc(DatabaseConsts.BookTable.COLUMN_LAST_CHAPTER_UPDATE_TIME)
.last(DatabaseConsts.SqlEnum.LIMIT_500.getSql()); .last(DatabaseConsts.SqlEnum.LIMIT_500.getSql());
return bookInfoMapper.selectList(queryWrapper).stream().map(BookInfo::getId).toList(); return bookInfoMapper.selectList(queryWrapper).stream().map(BookInfo::getId).toList();

View File

@ -301,7 +301,7 @@ public class BookServiceImpl implements BookService {
@Override @Override
public RestResp<Void> saveBookChapter(ChapterAddReqDto dto) { public RestResp<Void> saveBookChapter(ChapterAddReqDto dto) {
// 校验该作品是否属于当前作家 // 校验该作品是否属于当前作家
BookInfoRespDto bookInfo = bookInfoCacheManager.getBookInfo(dto.getBookId()); BookInfo bookInfo = bookInfoMapper.selectById(dto.getBookId());
if (!Objects.equals(bookInfo.getAuthorId(), UserHolder.getAuthorId())) { if (!Objects.equals(bookInfo.getAuthorId(), UserHolder.getAuthorId())) {
return RestResp.fail(ErrorCodeEnum.USER_UN_AUTH); return RestResp.fail(ErrorCodeEnum.USER_UN_AUTH);
} }
@ -345,8 +345,8 @@ public class BookServiceImpl implements BookService {
newBookInfo.setWordCount(bookInfo.getWordCount() + newBookChapter.getWordCount()); newBookInfo.setWordCount(bookInfo.getWordCount() + newBookChapter.getWordCount());
newBookChapter.setUpdateTime(LocalDateTime.now()); newBookChapter.setUpdateTime(LocalDateTime.now());
bookInfoMapper.updateById(newBookInfo); bookInfoMapper.updateById(newBookInfo);
// b) 刷新小说信息缓存 // b) 清除小说信息缓存
bookInfoCacheManager.cachePutBookInfo(dto.getBookId()); bookInfoCacheManager.evictBookInfoCache(dto.getBookId());
// c) 发送小说信息更新的 MQ 消息 // c) 发送小说信息更新的 MQ 消息
amqpMsgManager.sendBookChangeMsg(dto.getBookId()); amqpMsgManager.sendBookChangeMsg(dto.getBookId());
return RestResp.ok(); return RestResp.ok();