|
@@ -328,7 +328,7 @@ public class BookServiceImpl implements BookService {
|
|
|
|
|
|
}
|
|
|
//获取章节内容
|
|
|
- content = getBookContent(bookContentVO);
|
|
|
+ content = cacheService.getBookContentCache(bookContentVO.getBookId(), bookContentVO.getContentId());
|
|
|
// 添加到阅读记录
|
|
|
if (content != null) {
|
|
|
applicationEventPublisher.publishEvent(new UserRecentReadEvent(this, bookContentVO));
|
|
@@ -359,29 +359,21 @@ public class BookServiceImpl implements BookService {
|
|
|
@Autowired
|
|
|
private ApplicationEventPublisher applicationEventPublisher;
|
|
|
|
|
|
- private BookContent getBookContent(BookContentVO bookContentVO) {
|
|
|
- String key = CacheUtil.getKey(Const.EDIT_BOOK_PRE, bookContentVO.getBookId(), bookContentVO.getContentId());
|
|
|
- Object o = redisUtil.get(key);
|
|
|
- if (o != null) {
|
|
|
- return JsonUtils.getObject(o.toString(), BookContent.class);
|
|
|
- } else {
|
|
|
- BookContent bookContent;
|
|
|
- // 修改过的章节
|
|
|
- String content = getEditBookContentById(bookContentVO.getBookId(), bookContentVO.getContentId());
|
|
|
+ public BookContent getBookContent(Long bookId, Long contentId) {
|
|
|
+ BookContent bookContent;
|
|
|
+ // 修改过的章节
|
|
|
+ EditBook editBook = cacheService.getEditBookCache(bookId, contentId);
|
|
|
|
|
|
- if (content != null) {
|
|
|
- return getBookContentFromVO(bookContentVO.getBookId(), bookContentVO.getContentId(), bookContentVO.getContentName(), content);
|
|
|
- }
|
|
|
- if (cacheService.isUploadBook(bookContentVO.getBookId())) {
|
|
|
- // 自定义的书籍
|
|
|
- bookContent = getBookContentFromDB(bookContentVO.getBookId(), bookContentVO.getContentId(), bookContentVO.getContentName());
|
|
|
- } else {
|
|
|
- bookContent = getBookContentFromRemote(bookContentVO.getBookId(), bookContentVO.getContentId());
|
|
|
- }
|
|
|
- bookContentVO.setContentName(bookContent.getName());
|
|
|
- redisUtil.setWithTime(key, JsonUtils.toJsonStr(bookContent));
|
|
|
- return bookContent;
|
|
|
+ if (editBook != null) {
|
|
|
+ return getBookContentFromVO(bookId, contentId, editBook.getChapterName(), editBook.getContent());
|
|
|
+ }
|
|
|
+ if (cacheService.isUploadBook(bookId)) {
|
|
|
+ // 自定义的书籍
|
|
|
+ bookContent = getBookContentFromDB(bookId, contentId);
|
|
|
+ } else {
|
|
|
+ bookContent = getBookContentFromRemote(bookId, contentId);
|
|
|
}
|
|
|
+ return bookContent;
|
|
|
}
|
|
|
|
|
|
|
|
@@ -651,7 +643,7 @@ public class BookServiceImpl implements BookService {
|
|
|
|
|
|
@Override
|
|
|
public Result<BookContent> getContentByContentId2(BookContentVO bookContentVO) {
|
|
|
- BookContent content = getBookContent(bookContentVO);
|
|
|
+ BookContent content = cacheService.getBookContentCache(bookContentVO.getBookId(), bookContentVO.getContentId());
|
|
|
return Result.byObject(content);
|
|
|
}
|
|
|
|
|
@@ -682,20 +674,20 @@ public class BookServiceImpl implements BookService {
|
|
|
.build());
|
|
|
}
|
|
|
if (i > 0) {
|
|
|
- updateEditBookByIdCache(bookContentVO.getBookId(), bookContentVO.getContentId());
|
|
|
+ cacheService.updateEditBookCache(bookContentVO.getBookId(), bookContentVO.getContentId());
|
|
|
}
|
|
|
return i > 0;
|
|
|
}
|
|
|
|
|
|
- public BookContent getBookContentFromDB(Long bookId, Long contentId, String contentName) {
|
|
|
+ public BookContent getBookContentFromDB(Long bookId, Long contentId) {
|
|
|
UploadBook uploadBook = uploadBookMapper.selectOneByExampleSelective(UploadBookExample.newAndCreateCriteria()
|
|
|
.andBookIdEqualTo(bookId)
|
|
|
.andChapterIdEqualTo(contentId)
|
|
|
- .example(), UploadBook.Column.content);
|
|
|
+ .example(), UploadBook.Column.content, UploadBook.Column.chapterName);
|
|
|
if (uploadBook == null) {
|
|
|
return null;
|
|
|
} else {
|
|
|
- return getBookContentFromVO(bookId, contentId, contentName, uploadBook.getContent());
|
|
|
+ return getBookContentFromVO(bookId, contentId, uploadBook.getChapterName(), uploadBook.getContent());
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -741,16 +733,6 @@ public class BookServiceImpl implements BookService {
|
|
|
return getKey("user", id);
|
|
|
}
|
|
|
|
|
|
- public String getEditBookContentById(Long bookId, Long chapterId) {
|
|
|
- String key = CacheUtil.getKey(Const.EDIT_BOOK_PRE, bookId, chapterId);
|
|
|
- Object o = redisUtil.get(key);
|
|
|
- if (o == null) {
|
|
|
- return null;
|
|
|
- } else {
|
|
|
- return o.toString();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
public String getKey(Object... keys) {
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
for (Object key : keys) {
|
|
@@ -759,26 +741,16 @@ public class BookServiceImpl implements BookService {
|
|
|
return sb.toString();
|
|
|
}
|
|
|
|
|
|
- public void updateEditBookByIdCache(Long bookId, Long chapterId) {
|
|
|
- String key = CacheUtil.getKey(Const.EDIT_BOOK_PRE, bookId, chapterId);
|
|
|
- EditBook obj = editBookMapper.selectOneByExampleSelective(
|
|
|
- EditBookExample.newAndCreateCriteria()
|
|
|
- .andBookIdEqualTo(bookId)
|
|
|
- .andChapterIdEqualTo(chapterId)
|
|
|
- .example(),
|
|
|
- EditBook.Column.content
|
|
|
- );
|
|
|
- redisUtil.set(key, JsonUtils.toJsonStr(obj));
|
|
|
- }
|
|
|
|
|
|
- @PostConstruct
|
|
|
- public void initUpdateEditBookIdsCache() {
|
|
|
- List<EditBook> editBooks = editBookMapper.selectByExampleSelective(EditBookExample.newAndCreateCriteria().example(),
|
|
|
- EditBook.Column.bookId, EditBook.Column.chapterId, EditBook.Column.content);
|
|
|
- editBooks.forEach(x -> {
|
|
|
- String key = CacheUtil.getKey(Const.EDIT_BOOK_PRE, x.getBookId(), x.getChapterId());
|
|
|
- redisUtil.set(key, JsonUtils.toJsonStr(x));
|
|
|
- });
|
|
|
+ public EditBook getEditBookContent(Long bookId, Long contentId) {
|
|
|
+ EditBook editBook = editBookMapper.selectOneByExampleWithBLOBs(EditBookExample.newAndCreateCriteria()
|
|
|
+ .andBookIdEqualTo(bookId)
|
|
|
+ .andChapterIdEqualTo(contentId)
|
|
|
+ .example());
|
|
|
+ if (editBook == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return editBook;
|
|
|
}
|
|
|
|
|
|
|