BookCacheManager.java 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package com.sf.manager.cache;
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  3. import com.sf.constant.CacheConsts;
  4. import com.sf.constant.DatabaseConsts;
  5. import com.sf.dto.resp.BookChapterRespDto;
  6. import com.sf.mapper.BookChapterMapper;
  7. import com.sf.mapper.BookContentMapper;
  8. import com.sf.po.BookChapter;
  9. import com.sf.po.BookContent;
  10. import org.springframework.beans.BeanUtils;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.cache.annotation.CacheEvict;
  13. import org.springframework.cache.annotation.Cacheable;
  14. import org.springframework.stereotype.Component;
  15. /**
  16. * 小说内容 缓存管理类
  17. *
  18. * @author xiongxiaoyang
  19. * @date 2022/5/12
  20. */
  21. @Component
  22. //@RequiredArgsConstructor
  23. public class BookCacheManager {
  24. @Autowired
  25. private BookContentMapper bookContentMapper;
  26. @Autowired
  27. private BookChapterMapper bookChapterMapper;
  28. /**
  29. * 查询小说内容,并放入缓存中
  30. */
  31. @Cacheable(cacheManager = CacheConsts.REDIS_CACHE_MANAGER,
  32. value = CacheConsts.BOOK_CONTENT_CACHE_NAME)
  33. public String getBookContent(Long chapterId) {
  34. QueryWrapper<BookContent> contentQueryWrapper = new QueryWrapper<>();
  35. contentQueryWrapper.eq(DatabaseConsts.BookContentTable.COLUMN_CHAPTER_ID, chapterId)
  36. .last(DatabaseConsts.SqlEnum.LIMIT_1.getSql());
  37. BookContent bookContent = bookContentMapper.selectOne(contentQueryWrapper);
  38. return bookContent.getContent();
  39. }
  40. @Cacheable(cacheManager = "redisCacheManager",value = "bookChapterCache")
  41. public BookChapterRespDto getBookChapterNew(Long chapterId){
  42. BookChapter bookChapter = bookChapterMapper.selectById(chapterId);
  43. BookChapterRespDto bookChapterDto = BookChapterRespDto.builder()
  44. .chapterWordCount(bookChapter.getWordCount())
  45. .chapterUpdateTime(bookChapter.getUpdateTime()).build();
  46. BeanUtils.copyProperties(bookChapter,bookChapterDto);
  47. return bookChapterDto;
  48. }
  49. @CacheEvict(cacheManager = CacheConsts.REDIS_CACHE_MANAGER,
  50. value = CacheConsts.BOOK_CONTENT_CACHE_NAME)
  51. public void evictBookContentCache(Long chapterId) {
  52. // 调用此方法自动清除小说内容信息的缓存
  53. }
  54. }