feat: 新增小说推荐列表查询接口

This commit is contained in:
xiongxiaoyang 2022-05-15 20:47:07 +08:00
parent 2dbcd3f701
commit 960b9124d5
5 changed files with 66 additions and 1 deletions

View File

@ -13,6 +13,7 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.security.NoSuchAlgorithmException;
import java.util.List;
/**
@ -44,6 +45,14 @@ public class BookController {
return bookService.getLastChapterAbout(bookId);
}
/**
* 小说推荐列表查询接口
* */
@GetMapping("recList")
public RestResp<List<BookInfoRespDto>> listRecBooks(Long bookId) throws NoSuchAlgorithmException {
return bookService.listRecBooks(bookId);
}
/**
* 小说内容相关信息查询接口
* */

View File

@ -69,6 +69,11 @@ public class CacheConsts {
*/
public static final String BOOK_CONTENT_CACHE_NAME = "bookContentCache";
/**
* 最近更新小说ID列表缓存
* */
public static final String LAST_UPDATE_BOOK_ID_LIST_CACHE_NAME = "lastUpdateBookIdListCache";
/**
* 缓存配置常量
@ -91,7 +96,9 @@ public class CacheConsts {
BOOK_CHAPTER_CACHE(0,BOOK_CHAPTER_CACHE_NAME,60 * 60 * 6,5000),
BOOK_CONTENT_CACHE(2, BOOK_CONTENT_CACHE_NAME, 60 * 60 * 12, 3000);
BOOK_CONTENT_CACHE(2, BOOK_CONTENT_CACHE_NAME, 60 * 60 * 12, 3000),
LAST_UPDATE_BOOK_ID_LIST_CACHE(0,LAST_UPDATE_BOOK_ID_LIST_CACHE_NAME,60 * 60, 10);
/**
* 缓存类型 0-本地 1-本地和远程 2-远程

View File

@ -11,6 +11,8 @@ import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* 小说信息 缓存管理类
*
@ -59,5 +61,17 @@ public class BookInfoCacheManager {
.build();
}
/**
* 查询每个类别下最新更新的 500 个小说ID列表并放入缓存中 1 个小时
*/
@Cacheable(cacheManager = CacheConsts.CAFFEINE_CACHE_MANAGER
, value = CacheConsts.LAST_UPDATE_BOOK_ID_LIST_CACHE_NAME)
public List<Long> getLastUpdateIdList(Long categoryId) {
QueryWrapper<BookInfo> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("category_id", categoryId)
.orderByDesc("last_chapter_update_time")
.last("limit 500");
return bookInfoMapper.selectList(queryWrapper).stream().map(BookInfo::getId).toList();
}
}

View File

@ -6,6 +6,7 @@ import io.github.xxyopen.novel.dto.resp.BookContentAboutRespDto;
import io.github.xxyopen.novel.dto.resp.BookInfoRespDto;
import io.github.xxyopen.novel.dto.resp.BookRankRespDto;
import java.security.NoSuchAlgorithmException;
import java.util.List;
/**
@ -45,4 +46,9 @@ public interface BookService {
* 小说最新章节相关信息查询
* */
RestResp<BookChapterAboutRespDto> getLastChapterAbout(Long bookId);
/**
* 小说推荐列表查询
* */
RestResp<List<BookInfoRespDto>> listRecBooks(Long bookId) throws NoSuchAlgorithmException;
}

View File

@ -11,9 +11,14 @@ import io.github.xxyopen.novel.manager.BookInfoCacheManager;
import io.github.xxyopen.novel.manager.BookRankCacheManager;
import io.github.xxyopen.novel.service.BookService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* 小说模块 服务实现类
@ -23,6 +28,7 @@ import java.util.List;
*/
@Service
@RequiredArgsConstructor
@Slf4j
public class BookServiceImpl implements BookService {
private final BookRankCacheManager bookRankCacheManager;
@ -35,6 +41,8 @@ public class BookServiceImpl implements BookService {
private final BookChapterMapper bookChapterMapper;
private static final Integer REC_BOOK_COUNT = 4;
@Override
public RestResp<List<BookRankRespDto>> listVisitRankBooks() {
return RestResp.ok(bookRankCacheManager.listVisitRankBooks());
@ -79,6 +87,27 @@ public class BookServiceImpl implements BookService {
.build());
}
@Override
public RestResp<List<BookInfoRespDto>> listRecBooks(Long bookId) throws NoSuchAlgorithmException {
Long categoryId = bookInfoCacheManager.getBookInfo(bookId).getCategoryId();
List<Long> lastUpdateIdList = bookInfoCacheManager.getLastUpdateIdList(categoryId);
List<BookInfoRespDto> respDtoList = new ArrayList<>();
List<Integer> recIdIndexList = new ArrayList<>();
int count = 0;
Random rand = SecureRandom.getInstanceStrong();
while (count < REC_BOOK_COUNT){
int recIdIndex = rand.nextInt(lastUpdateIdList.size());
if (!recIdIndexList.contains(recIdIndex)) {
recIdIndexList.add(recIdIndex);
bookId = lastUpdateIdList.get(recIdIndex);
BookInfoRespDto bookInfo = bookInfoCacheManager.getBookInfo(bookId);
respDtoList.add(bookInfo);
count ++ ;
}
}
return RestResp.ok(respDtoList);
}
@Override
public RestResp<BookContentAboutRespDto> getBookContentAbout(Long chapterId) {
// 查询章节信息