package com.sf.manager.cache; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.sf.constant.CacheConsts; import com.sf.constant.DatabaseConsts; import com.sf.dto.resp.BookChapterRespDto; import com.sf.mapper.BookChapterMapper; import com.sf.mapper.BookContentMapper; import com.sf.po.BookChapter; import com.sf.po.BookContent; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Component; /** * 小说内容 缓存管理类 * * @author xiongxiaoyang * @date 2022/5/12 */ @Component //@RequiredArgsConstructor public class BookCacheManager { @Autowired private BookContentMapper bookContentMapper; @Autowired private BookChapterMapper bookChapterMapper; /** * 查询小说内容,并放入缓存中 */ @Cacheable(cacheManager = CacheConsts.REDIS_CACHE_MANAGER, value = CacheConsts.BOOK_CONTENT_CACHE_NAME) public String getBookContent(Long chapterId) { QueryWrapper contentQueryWrapper = new QueryWrapper<>(); contentQueryWrapper.eq(DatabaseConsts.BookContentTable.COLUMN_CHAPTER_ID, chapterId) .last(DatabaseConsts.SqlEnum.LIMIT_1.getSql()); BookContent bookContent = bookContentMapper.selectOne(contentQueryWrapper); return bookContent.getContent(); } @Cacheable(cacheManager = "redisCacheManager",value = "bookChapterCache") public BookChapterRespDto getBookChapterNew(Long chapterId){ BookChapter bookChapter = bookChapterMapper.selectById(chapterId); BookChapterRespDto bookChapterDto = BookChapterRespDto.builder() .chapterWordCount(bookChapter.getWordCount()) .chapterUpdateTime(bookChapter.getUpdateTime()).build(); BeanUtils.copyProperties(bookChapter,bookChapterDto); return bookChapterDto; } @CacheEvict(cacheManager = CacheConsts.REDIS_CACHE_MANAGER, value = CacheConsts.BOOK_CONTENT_CACHE_NAME) public void evictBookContentCache(Long chapterId) { // 调用此方法自动清除小说内容信息的缓存 } }