diff --git a/src/main/java/cc/ryanc/halo/model/tag/ArticleTagDirective.java b/src/main/java/cc/ryanc/halo/model/tag/ArticleTagDirective.java index 7c6608408..6d78b86b3 100644 --- a/src/main/java/cc/ryanc/halo/model/tag/ArticleTagDirective.java +++ b/src/main/java/cc/ryanc/halo/model/tag/ArticleTagDirective.java @@ -1,5 +1,6 @@ package cc.ryanc.halo.model.tag; +import cc.ryanc.halo.model.enums.PostStatus; import cc.ryanc.halo.model.enums.PostType; import cc.ryanc.halo.service.PostService; import freemarker.core.Environment; @@ -33,7 +34,7 @@ public class ArticleTagDirective implements TemplateDirectiveModel { String method = map.get(METHOD_KEY).toString(); switch (method) { case "postsCount": - environment.setVariable("postsCount", builder.build().wrap(postService.findAllPosts(PostType.POST_TYPE_POST.getDesc()).size())); + environment.setVariable("postsCount", builder.build().wrap(postService.findPostByStatus(PostStatus.PUBLISHED.getCode(), PostType.POST_TYPE_POST.getDesc()).size())); break; case "archives": environment.setVariable("archives", builder.build().wrap(postService.findPostGroupByYearAndMonth())); diff --git a/src/main/java/cc/ryanc/halo/repository/PostRepository.java b/src/main/java/cc/ryanc/halo/repository/PostRepository.java index e2be29449..55f8ebed6 100644 --- a/src/main/java/cc/ryanc/halo/repository/PostRepository.java +++ b/src/main/java/cc/ryanc/halo/repository/PostRepository.java @@ -155,19 +155,21 @@ public interface PostRepository extends JpaRepository { * 根据分类目录查询文章 * * @param category category + * @param status status * @param pageable pageable * @return Page */ - Page findPostByCategories(Category category, Pageable pageable); + Page findPostByCategoriesAndPostStatus(Category category, Integer status, Pageable pageable); /** * 根据标签查询文章,分页 * - * @param tag tag + * @param tag tag + * @param status status * @param pageable pageable * @return Page */ - Page findPostsByTags(Tag tag, Pageable pageable); + Page findPostsByTagsAndPostStatus(Tag tag, Integer status, Pageable pageable); /** * 根据标签查询文章 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 8d25263d5..8fdff4515 100755 --- a/src/main/java/cc/ryanc/halo/service/impl/CategoryServiceImpl.java +++ b/src/main/java/cc/ryanc/halo/service/impl/CategoryServiceImpl.java @@ -4,8 +4,6 @@ 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; @@ -22,8 +20,6 @@ public class CategoryServiceImpl implements CategoryService { @Autowired private CategoryRepository categoryRepository; - private static final String CATEGORIES_CACHE_NAME = "categories"; - /** * 保存/修改分类目录 * @@ -31,7 +27,6 @@ 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); } @@ -43,7 +38,6 @@ 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()); @@ -56,7 +50,6 @@ 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/PostServiceImpl.java b/src/main/java/cc/ryanc/halo/service/impl/PostServiceImpl.java index 30181273a..d55ce059c 100755 --- a/src/main/java/cc/ryanc/halo/service/impl/PostServiceImpl.java +++ b/src/main/java/cc/ryanc/halo/service/impl/PostServiceImpl.java @@ -176,6 +176,7 @@ public class PostServiceImpl implements PostService { * @return List */ @Override + @Cacheable(value = POSTS_CACHE_NAME, key = "'posts_status_type_'+#status+'_'+#postType") public List findPostByStatus(Integer status, String postType) { return postRepository.findPostsByPostStatusAndPostType(status, postType); } @@ -323,25 +324,28 @@ public class PostServiceImpl implements PostService { * 根据分类目录查询文章 * * @param category category + * @param status status * @param pageable pageable * @return Page */ @Override + @CachePut(value = POSTS_CACHE_NAME, key = "'posts_category_'+#category.cateId+'_'+#pageable.pageNumber") public Page findPostByCategories(Category category, Pageable pageable) { - return postRepository.findPostByCategories(category,pageable); + return postRepository.findPostByCategoriesAndPostStatus(category, PostStatus.PUBLISHED.getCode(), pageable); } /** - * 根据标签查询文章 + * 根据标签查询文章,分页 * - * @param tag tag + * @param tag tag + * @param status status * @param pageable pageable * @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); + return postRepository.findPostsByTagsAndPostStatus(tag, PostStatus.PUBLISHED.getCode(), pageable); } /** diff --git a/src/main/resources/ehcache.xml b/src/main/resources/ehcache.xml index 84bde2663..88acf7fb4 100644 --- a/src/main/resources/ehcache.xml +++ b/src/main/resources/ehcache.xml @@ -80,14 +80,4 @@ timeToIdleSeconds="0" timeToLiveSeconds="300" memoryStoreEvictionPolicy="LRU"/> - -