diff --git a/src/main/java/cc/ryanc/halo/service/PostService.java b/src/main/java/cc/ryanc/halo/service/PostService.java index 44ad171bb..4b44a3ea0 100755 --- a/src/main/java/cc/ryanc/halo/service/PostService.java +++ b/src/main/java/cc/ryanc/halo/service/PostService.java @@ -76,7 +76,7 @@ public interface PostService { List searchPosts(String keyWord, Pageable pageable); /** - * 根据文章状态查询 分页 + * 根据文章状态查询 分页,用于后台管理 * * @param status 0,1,2 * @param postType post or page @@ -85,6 +85,13 @@ public interface PostService { */ Page findPostByStatus(Integer status, String postType, Pageable pageable); + /** + * 根据文章状态查询 分页,首页分页 + * @param pageable pageable + * @return Page + */ + Page findPostByStatus(Pageable pageable); + /** * 根据文章状态查询 * diff --git a/src/main/java/cc/ryanc/halo/service/impl/AttachmentServiceImpl.java b/src/main/java/cc/ryanc/halo/service/impl/AttachmentServiceImpl.java index a9fbdb28a..47429b1f1 100644 --- a/src/main/java/cc/ryanc/halo/service/impl/AttachmentServiceImpl.java +++ b/src/main/java/cc/ryanc/halo/service/impl/AttachmentServiceImpl.java @@ -4,6 +4,8 @@ import cc.ryanc.halo.model.domain.Attachment; import cc.ryanc.halo.repository.AttachmentRepository; import cc.ryanc.halo.service.AttachmentService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.Cacheable; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; @@ -21,6 +23,8 @@ public class AttachmentServiceImpl implements AttachmentService { @Autowired private AttachmentRepository attachmentRepository; + private static final String ATTACHMENTS_CACHE_NAME = "attachments"; + /** * 新增附件信息 * @@ -28,6 +32,7 @@ public class AttachmentServiceImpl implements AttachmentService { * @return Attachment */ @Override + @CacheEvict(value = ATTACHMENTS_CACHE_NAME, allEntries = true, beforeInvocation = true) public Attachment saveByAttachment(Attachment attachment) { return attachmentRepository.save(attachment); } @@ -38,6 +43,7 @@ public class AttachmentServiceImpl implements AttachmentService { * @return List */ @Override + @Cacheable(value = ATTACHMENTS_CACHE_NAME, key = "'attachment'") public List findAllAttachments() { return attachmentRepository.findAll(); } @@ -71,6 +77,7 @@ public class AttachmentServiceImpl implements AttachmentService { * @return Attachment */ @Override + @CacheEvict(value = ATTACHMENTS_CACHE_NAME, allEntries = true, beforeInvocation = true) public Attachment removeByAttachId(Long attachId) { Optional attachment = this.findByAttachId(attachId); attachmentRepository.delete(attachment.get()); diff --git a/src/main/java/cc/ryanc/halo/service/impl/CategoryServiceImpl.java b/src/main/java/cc/ryanc/halo/service/impl/CategoryServiceImpl.java index 8fdff4515..8d25263d5 100755 --- a/src/main/java/cc/ryanc/halo/service/impl/CategoryServiceImpl.java +++ b/src/main/java/cc/ryanc/halo/service/impl/CategoryServiceImpl.java @@ -4,6 +4,8 @@ import cc.ryanc.halo.model.domain.Category; import cc.ryanc.halo.repository.CategoryRepository; import cc.ryanc.halo.service.CategoryService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import java.util.ArrayList; @@ -20,6 +22,8 @@ public class CategoryServiceImpl implements CategoryService { @Autowired private CategoryRepository categoryRepository; + private static final String CATEGORIES_CACHE_NAME = "categories"; + /** * 保存/修改分类目录 * @@ -27,6 +31,7 @@ public class CategoryServiceImpl implements CategoryService { * @return Category */ @Override + @CacheEvict(value = CATEGORIES_CACHE_NAME, allEntries = true, beforeInvocation = true) public Category saveByCategory(Category category) { return categoryRepository.save(category); } @@ -38,6 +43,7 @@ public class CategoryServiceImpl implements CategoryService { * @return Category */ @Override + @CacheEvict(value = CATEGORIES_CACHE_NAME, allEntries = true, beforeInvocation = true) public Category removeByCateId(Long cateId) { Optional category = this.findByCateId(cateId); categoryRepository.delete(category.get()); @@ -50,6 +56,7 @@ public class CategoryServiceImpl implements CategoryService { * @return List */ @Override + @Cacheable(value = CATEGORIES_CACHE_NAME, key = "'category'") public List findAllCategories() { return categoryRepository.findAll(); } diff --git a/src/main/java/cc/ryanc/halo/service/impl/CommentServiceImpl.java b/src/main/java/cc/ryanc/halo/service/impl/CommentServiceImpl.java index 7ba3a4e69..098519f0e 100644 --- a/src/main/java/cc/ryanc/halo/service/impl/CommentServiceImpl.java +++ b/src/main/java/cc/ryanc/halo/service/impl/CommentServiceImpl.java @@ -5,6 +5,9 @@ import cc.ryanc.halo.model.domain.Post; import cc.ryanc.halo.repository.CommentRepository; import cc.ryanc.halo.service.CommentService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.CachePut; +import org.springframework.cache.annotation.Cacheable; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; @@ -22,12 +25,15 @@ public class CommentServiceImpl implements CommentService { @Autowired private CommentRepository commentRepository; + private static final String COMMENTS_CACHE_NAME = "comments"; + /** * 新增评论 * * @param comment comment */ @Override + @CacheEvict(value = COMMENTS_CACHE_NAME, allEntries = true, beforeInvocation = true) public void saveByComment(Comment comment) { commentRepository.save(comment); } @@ -39,6 +45,7 @@ public class CommentServiceImpl implements CommentService { * @return Optional */ @Override + @CacheEvict(value = COMMENTS_CACHE_NAME, allEntries = true, beforeInvocation = true) public Optional removeByCommentId(Long commentId) { Optional comment = this.findCommentById(commentId); commentRepository.delete(comment.get()); @@ -63,6 +70,7 @@ public class CommentServiceImpl implements CommentService { * @return List */ @Override + @CachePut(value = COMMENTS_CACHE_NAME, key = "'comments_status_'+#status") public List findAllComments(Integer status) { return commentRepository.findCommentsByCommentStatus(status); } @@ -73,6 +81,7 @@ public class CommentServiceImpl implements CommentService { * @return List */ @Override + @Cacheable(value = COMMENTS_CACHE_NAME, key = "'comment'") public List findAllComments() { return commentRepository.findAll(); } @@ -85,6 +94,7 @@ public class CommentServiceImpl implements CommentService { * @return Comment */ @Override + @CacheEvict(value = COMMENTS_CACHE_NAME, allEntries = true, beforeInvocation = true) public Comment updateCommentStatus(Long commentId, Integer status) { Optional comment = findCommentById(commentId); comment.get().setCommentStatus(status); @@ -133,6 +143,7 @@ public class CommentServiceImpl implements CommentService { * @return List */ @Override + @Cacheable(value = COMMENTS_CACHE_NAME, key = "'comments_latest'") public List findCommentsLatest() { return commentRepository.findTopFive(); } diff --git a/src/main/java/cc/ryanc/halo/service/impl/GalleryServiceImpl.java b/src/main/java/cc/ryanc/halo/service/impl/GalleryServiceImpl.java index cb271cd56..ec8d6c74c 100644 --- a/src/main/java/cc/ryanc/halo/service/impl/GalleryServiceImpl.java +++ b/src/main/java/cc/ryanc/halo/service/impl/GalleryServiceImpl.java @@ -4,6 +4,8 @@ import cc.ryanc.halo.model.domain.Gallery; import cc.ryanc.halo.repository.GalleryRepository; import cc.ryanc.halo.service.GalleryService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.Cacheable; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; @@ -21,6 +23,8 @@ public class GalleryServiceImpl implements GalleryService { @Autowired private GalleryRepository galleryRepository; + private static final String GALLERIES_CACHE_NAME = "galleries"; + /** * 保存图片 * @@ -28,6 +32,7 @@ public class GalleryServiceImpl implements GalleryService { * @return Gallery */ @Override + @CacheEvict(value = GALLERIES_CACHE_NAME, allEntries = true, beforeInvocation = true) public Gallery saveByGallery(Gallery gallery) { return galleryRepository.save(gallery); } @@ -39,6 +44,7 @@ public class GalleryServiceImpl implements GalleryService { * @return Gallery */ @Override + @CacheEvict(value = GALLERIES_CACHE_NAME, allEntries = true, beforeInvocation = true) public Gallery removeByGalleryId(Long galleryId) { Optional gallery = this.findByGalleryId(galleryId); galleryRepository.delete(gallery.get()); @@ -52,6 +58,7 @@ public class GalleryServiceImpl implements GalleryService { * @return Gallery */ @Override + @CacheEvict(value = GALLERIES_CACHE_NAME, allEntries = true, beforeInvocation = true) public Gallery updateByGallery(Gallery gallery) { return galleryRepository.save(gallery); } @@ -73,6 +80,7 @@ public class GalleryServiceImpl implements GalleryService { * @return List */ @Override + @Cacheable(value = GALLERIES_CACHE_NAME, key = "'gallery'") public List findAllGalleries() { return galleryRepository.findAll(); } diff --git a/src/main/java/cc/ryanc/halo/service/impl/LinkServiceImpl.java b/src/main/java/cc/ryanc/halo/service/impl/LinkServiceImpl.java index b6e1fee3b..d9a1f68b7 100755 --- a/src/main/java/cc/ryanc/halo/service/impl/LinkServiceImpl.java +++ b/src/main/java/cc/ryanc/halo/service/impl/LinkServiceImpl.java @@ -32,7 +32,7 @@ public class LinkServiceImpl implements LinkService { * @return Link */ @Override - @CacheEvict(value = LINKS_CACHE_NAME, key = LINKS_CACHE_KEY) + @CacheEvict(value = LINKS_CACHE_NAME, allEntries = true, beforeInvocation = true) public Link saveByLink(Link link) { return linkRepository.save(link); } @@ -44,7 +44,7 @@ public class LinkServiceImpl implements LinkService { * @return Link */ @Override - @CacheEvict(value = LINKS_CACHE_NAME, key = LINKS_CACHE_KEY) + @CacheEvict(value = LINKS_CACHE_NAME, allEntries = true, beforeInvocation = true) public Link removeByLinkId(Long linkId) { Optional link = this.findByLinkId(linkId); linkRepository.delete(link.get()); diff --git a/src/main/java/cc/ryanc/halo/service/impl/MenuServiceImpl.java b/src/main/java/cc/ryanc/halo/service/impl/MenuServiceImpl.java index 8a9bb592e..abaf7528b 100644 --- a/src/main/java/cc/ryanc/halo/service/impl/MenuServiceImpl.java +++ b/src/main/java/cc/ryanc/halo/service/impl/MenuServiceImpl.java @@ -43,7 +43,7 @@ public class MenuServiceImpl implements MenuService { * @return Menu */ @Override - @CacheEvict(value = MENUS_CACHE_NAME, key = MENUS_CACHE_KEY) + @CacheEvict(value = MENUS_CACHE_NAME, allEntries = true, beforeInvocation = true) public Menu saveByMenu(Menu menu) { return menuRepository.save(menu); } @@ -55,7 +55,7 @@ public class MenuServiceImpl implements MenuService { * @return Menu */ @Override - @CacheEvict(value = MENUS_CACHE_NAME,key = MENUS_CACHE_KEY) + @CacheEvict(value = MENUS_CACHE_NAME, allEntries = true, beforeInvocation = true) public Menu removeByMenuId(Long menuId) { Optional menu = this.findByMenuId(menuId); menuRepository.delete(menu.get()); diff --git a/src/main/java/cc/ryanc/halo/service/impl/PostServiceImpl.java b/src/main/java/cc/ryanc/halo/service/impl/PostServiceImpl.java index 58092fcb1..2821cf1c8 100755 --- a/src/main/java/cc/ryanc/halo/service/impl/PostServiceImpl.java +++ b/src/main/java/cc/ryanc/halo/service/impl/PostServiceImpl.java @@ -10,6 +10,9 @@ import cc.ryanc.halo.service.PostService; import cc.ryanc.halo.utils.HaloUtils; import cn.hutool.http.HtmlUtil; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.CachePut; +import org.springframework.cache.annotation.Cacheable; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; @@ -29,6 +32,8 @@ public class PostServiceImpl implements PostService { @Autowired private PostRepository postRepository; + private static final String POSTS_CACHE_NAME = "posts"; + /** * 保存文章 * @@ -36,6 +41,7 @@ public class PostServiceImpl implements PostService { * @return Post */ @Override + @CacheEvict(value = POSTS_CACHE_NAME, allEntries = true, beforeInvocation = true) public Post saveByPost(Post post) { return postRepository.save(post); } @@ -47,6 +53,7 @@ public class PostServiceImpl implements PostService { * @return Post */ @Override + @CacheEvict(value = POSTS_CACHE_NAME, allEntries = true, beforeInvocation = true) public Post removeByPostId(Long postId) { Optional post = this.findByPostId(postId); postRepository.delete(post.get()); @@ -61,6 +68,7 @@ public class PostServiceImpl implements PostService { * @return Post */ @Override + @CacheEvict(value = POSTS_CACHE_NAME, allEntries = true, beforeInvocation = true) public Post updatePostStatus(Long postId, Integer status) { Optional post = this.findByPostId(postId); post.get().setPostStatus(status); @@ -73,6 +81,7 @@ public class PostServiceImpl implements PostService { * @param postSummary postSummary */ @Override + @CacheEvict(value = POSTS_CACHE_NAME, allEntries = true, beforeInvocation = true) public void updateAllSummary(Integer postSummary) { List posts = this.findAllPosts(HaloConst.POST_TYPE_POST); for (Post post : posts) { @@ -105,6 +114,7 @@ public class PostServiceImpl implements PostService { * @return List */ @Override + @Cacheable(value = POSTS_CACHE_NAME, key = "'posts_type_'+#postType") public List findAllPosts(String postType) { return postRepository.findPostsByPostType(postType); } @@ -122,7 +132,7 @@ public class PostServiceImpl implements PostService { } /** - * 根据文章状态查询 分页 + * 根据文章状态查询 分页,用于后台管理 * * @param status 0,1,2 * @param postType post or page @@ -134,6 +144,18 @@ public class PostServiceImpl implements PostService { return postRepository.findPostsByPostStatusAndPostType(status, postType, pageable); } + /** + * 根据文章状态查询 分页,首页分页 + * + * @param pageable pageable + * @return Page + */ + @Override + @Cacheable(value = POSTS_CACHE_NAME, key = "'posts_page_'+#pageable.pageNumber") + public Page findPostByStatus(Pageable pageable) { + return postRepository.findPostsByPostStatusAndPostType(0,HaloConst.POST_TYPE_POST,pageable); + } + /** * 根据文章状态查询 * @@ -175,6 +197,7 @@ public class PostServiceImpl implements PostService { * @return List */ @Override + @Cacheable(value = POSTS_CACHE_NAME, key = "'posts_latest'") public List findPostLatest() { return postRepository.findTopFive(); } @@ -208,6 +231,7 @@ public class PostServiceImpl implements PostService { * @return List */ @Override + @Cacheable(value = POSTS_CACHE_NAME, key = "'archives_year_month'") public List findPostGroupByYearAndMonth() { List objects = postRepository.findPostGroupByYearAndMonth(); List archives = new ArrayList<>(); @@ -229,6 +253,7 @@ public class PostServiceImpl implements PostService { * @return List */ @Override + @Cacheable(value = POSTS_CACHE_NAME, key = "'archives_year'") public List findPostGroupByYear() { List objects = postRepository.findPostGroupByYear(); List archives = new ArrayList<>(); @@ -251,6 +276,7 @@ public class PostServiceImpl implements PostService { * @return List */ @Override + @Cacheable(value = POSTS_CACHE_NAME, key = "'posts_year_month_'+#year+'_'+#month") public List findPostByYearAndMonth(String year, String month) { return postRepository.findPostByYearAndMonth(year, month); } @@ -262,6 +288,7 @@ public class PostServiceImpl implements PostService { * @return List */ @Override + @Cacheable(value = POSTS_CACHE_NAME, key = "'posts_year_'+#year") public List findPostByYear(String year) { return postRepository.findPostByYear(year); } @@ -299,6 +326,7 @@ public class PostServiceImpl implements PostService { * @return Page */ @Override + @CachePut(value = POSTS_CACHE_NAME, key = "'posts_tag_'+#tag.tagId+'_'+#pageable.pageNumber") public Page findPostsByTags(Tag tag, Pageable pageable) { return postRepository.findPostsByTags(tag, pageable); } @@ -321,6 +349,7 @@ public class PostServiceImpl implements PostService { * @return List */ @Override + @Cacheable(value = POSTS_CACHE_NAME, key = "'posts_hot'") public List hotPosts() { return postRepository.findPostsByPostTypeOrderByPostViewsDesc(HaloConst.POST_TYPE_POST); } @@ -332,6 +361,7 @@ public class PostServiceImpl implements PostService { * @return List */ @Override + @CachePut(value = POSTS_CACHE_NAME, key = "'posts_related_'+#post.getPostId()") public List relatedPosts(Post post) { //获取当前文章的所有标签 List tags = post.getTags(); diff --git a/src/main/java/cc/ryanc/halo/service/impl/TagServiceImpl.java b/src/main/java/cc/ryanc/halo/service/impl/TagServiceImpl.java index dea016d15..af9917ee3 100644 --- a/src/main/java/cc/ryanc/halo/service/impl/TagServiceImpl.java +++ b/src/main/java/cc/ryanc/halo/service/impl/TagServiceImpl.java @@ -4,6 +4,8 @@ import cc.ryanc.halo.model.domain.Tag; import cc.ryanc.halo.repository.TagRepository; import cc.ryanc.halo.service.TagService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import java.util.ArrayList; @@ -20,6 +22,8 @@ public class TagServiceImpl implements TagService { @Autowired private TagRepository tagRepository; + private static final String TAGS_CACHE_NAME = "tags"; + /** * 新增/修改标签 * @@ -27,6 +31,7 @@ public class TagServiceImpl implements TagService { * @return Tag */ @Override + @CacheEvict(value = TAGS_CACHE_NAME, allEntries = true, beforeInvocation = true) public Tag saveByTag(Tag tag) { return tagRepository.save(tag); } @@ -38,6 +43,7 @@ public class TagServiceImpl implements TagService { * @return Tag */ @Override + @CacheEvict(value = TAGS_CACHE_NAME, allEntries = true, beforeInvocation = true) public Tag removeByTagId(Long tagId) { Optional tag = findByTagId(tagId); tagRepository.delete(tag.get()); @@ -50,6 +56,7 @@ public class TagServiceImpl implements TagService { * @return List */ @Override + @Cacheable(value = TAGS_CACHE_NAME, key = "'tag'") public List findAllTags() { return tagRepository.findAll(); } diff --git a/src/main/java/cc/ryanc/halo/web/controller/admin/ThemeController.java b/src/main/java/cc/ryanc/halo/web/controller/admin/ThemeController.java index 3d403cf1e..82265710f 100755 --- a/src/main/java/cc/ryanc/halo/web/controller/admin/ThemeController.java +++ b/src/main/java/cc/ryanc/halo/web/controller/admin/ThemeController.java @@ -14,7 +14,9 @@ import cn.hutool.core.util.ZipUtil; import cn.hutool.extra.servlet.ServletUtil; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; +import org.ehcache.CacheManager; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.CacheEvict; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.util.ResourceUtils; @@ -67,6 +69,7 @@ public class ThemeController extends BaseController { */ @GetMapping(value = "/set") @ResponseBody + @CacheEvict(value = "posts", allEntries = true, beforeInvocation = true) public JsonResult activeTheme(@PathParam("siteTheme") String siteTheme, HttpServletRequest request) { try { diff --git a/src/main/java/cc/ryanc/halo/web/controller/front/FrontIndexController.java b/src/main/java/cc/ryanc/halo/web/controller/front/FrontIndexController.java index aaa542202..32aefb105 100644 --- a/src/main/java/cc/ryanc/halo/web/controller/front/FrontIndexController.java +++ b/src/main/java/cc/ryanc/halo/web/controller/front/FrontIndexController.java @@ -66,7 +66,7 @@ public class FrontIndexController extends BaseController { } //所有文章数据,分页 Pageable pageable = PageRequest.of(page - 1, size, sort); - Page posts = postService.findPostByStatus(0, HaloConst.POST_TYPE_POST, pageable); + Page posts = postService.findPostByStatus(pageable); if (null == posts) { return this.renderNotFound(); } @@ -93,7 +93,7 @@ public class FrontIndexController extends BaseController { //文章数据,只获取文章,没有分页 Pageable pageable = PageRequest.of(page - 1, size, sort); - List posts = postService.findPostByStatus(0, HaloConst.POST_TYPE_POST, pageable).getContent(); + List posts = postService.findPostByStatus(pageable).getContent(); return posts; } diff --git a/src/main/resources/ehcache.xml b/src/main/resources/ehcache.xml index 36798cd35..84bde2663 100644 --- a/src/main/resources/ehcache.xml +++ b/src/main/resources/ehcache.xml @@ -30,4 +30,64 @@ timeToIdleSeconds="0" timeToLiveSeconds="300" memoryStoreEvictionPolicy="LRU"/> - \ No newline at end of file + + + + + + + + + + + + +