mirror of https://github.com/halo-dev/halo
parent
9b6314c040
commit
0b1e20216f
|
@ -1,5 +1,6 @@
|
||||||
package cc.ryanc.halo.model.tag;
|
package cc.ryanc.halo.model.tag;
|
||||||
|
|
||||||
|
import cc.ryanc.halo.model.enums.PostStatus;
|
||||||
import cc.ryanc.halo.model.enums.PostType;
|
import cc.ryanc.halo.model.enums.PostType;
|
||||||
import cc.ryanc.halo.service.PostService;
|
import cc.ryanc.halo.service.PostService;
|
||||||
import freemarker.core.Environment;
|
import freemarker.core.Environment;
|
||||||
|
@ -33,7 +34,7 @@ public class ArticleTagDirective implements TemplateDirectiveModel {
|
||||||
String method = map.get(METHOD_KEY).toString();
|
String method = map.get(METHOD_KEY).toString();
|
||||||
switch (method) {
|
switch (method) {
|
||||||
case "postsCount":
|
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;
|
break;
|
||||||
case "archives":
|
case "archives":
|
||||||
environment.setVariable("archives", builder.build().wrap(postService.findPostGroupByYearAndMonth()));
|
environment.setVariable("archives", builder.build().wrap(postService.findPostGroupByYearAndMonth()));
|
||||||
|
|
|
@ -155,19 +155,21 @@ public interface PostRepository extends JpaRepository<Post, Long> {
|
||||||
* 根据分类目录查询文章
|
* 根据分类目录查询文章
|
||||||
*
|
*
|
||||||
* @param category category
|
* @param category category
|
||||||
|
* @param status status
|
||||||
* @param pageable pageable
|
* @param pageable pageable
|
||||||
* @return Page
|
* @return Page
|
||||||
*/
|
*/
|
||||||
Page<Post> findPostByCategories(Category category, Pageable pageable);
|
Page<Post> findPostByCategoriesAndPostStatus(Category category, Integer status, Pageable pageable);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据标签查询文章,分页
|
* 根据标签查询文章,分页
|
||||||
*
|
*
|
||||||
* @param tag tag
|
* @param tag tag
|
||||||
|
* @param status status
|
||||||
* @param pageable pageable
|
* @param pageable pageable
|
||||||
* @return Page
|
* @return Page
|
||||||
*/
|
*/
|
||||||
Page<Post> findPostsByTags(Tag tag, Pageable pageable);
|
Page<Post> findPostsByTagsAndPostStatus(Tag tag, Integer status, Pageable pageable);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据标签查询文章
|
* 根据标签查询文章
|
||||||
|
|
|
@ -4,8 +4,6 @@ import cc.ryanc.halo.model.domain.Category;
|
||||||
import cc.ryanc.halo.repository.CategoryRepository;
|
import cc.ryanc.halo.repository.CategoryRepository;
|
||||||
import cc.ryanc.halo.service.CategoryService;
|
import cc.ryanc.halo.service.CategoryService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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 org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -22,8 +20,6 @@ public class CategoryServiceImpl implements CategoryService {
|
||||||
@Autowired
|
@Autowired
|
||||||
private CategoryRepository categoryRepository;
|
private CategoryRepository categoryRepository;
|
||||||
|
|
||||||
private static final String CATEGORIES_CACHE_NAME = "categories";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 保存/修改分类目录
|
* 保存/修改分类目录
|
||||||
*
|
*
|
||||||
|
@ -31,7 +27,6 @@ public class CategoryServiceImpl implements CategoryService {
|
||||||
* @return Category
|
* @return Category
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
@CacheEvict(value = CATEGORIES_CACHE_NAME, allEntries = true, beforeInvocation = true)
|
|
||||||
public Category saveByCategory(Category category) {
|
public Category saveByCategory(Category category) {
|
||||||
return categoryRepository.save(category);
|
return categoryRepository.save(category);
|
||||||
}
|
}
|
||||||
|
@ -43,7 +38,6 @@ public class CategoryServiceImpl implements CategoryService {
|
||||||
* @return Category
|
* @return Category
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
@CacheEvict(value = CATEGORIES_CACHE_NAME, allEntries = true, beforeInvocation = true)
|
|
||||||
public Category removeByCateId(Long cateId) {
|
public Category removeByCateId(Long cateId) {
|
||||||
Optional<Category> category = this.findByCateId(cateId);
|
Optional<Category> category = this.findByCateId(cateId);
|
||||||
categoryRepository.delete(category.get());
|
categoryRepository.delete(category.get());
|
||||||
|
@ -56,7 +50,6 @@ public class CategoryServiceImpl implements CategoryService {
|
||||||
* @return List
|
* @return List
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
@Cacheable(value = CATEGORIES_CACHE_NAME, key = "'category'")
|
|
||||||
public List<Category> findAllCategories() {
|
public List<Category> findAllCategories() {
|
||||||
return categoryRepository.findAll();
|
return categoryRepository.findAll();
|
||||||
}
|
}
|
||||||
|
|
|
@ -176,6 +176,7 @@ public class PostServiceImpl implements PostService {
|
||||||
* @return List
|
* @return List
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
|
@Cacheable(value = POSTS_CACHE_NAME, key = "'posts_status_type_'+#status+'_'+#postType")
|
||||||
public List<Post> findPostByStatus(Integer status, String postType) {
|
public List<Post> findPostByStatus(Integer status, String postType) {
|
||||||
return postRepository.findPostsByPostStatusAndPostType(status, postType);
|
return postRepository.findPostsByPostStatusAndPostType(status, postType);
|
||||||
}
|
}
|
||||||
|
@ -323,25 +324,28 @@ public class PostServiceImpl implements PostService {
|
||||||
* 根据分类目录查询文章
|
* 根据分类目录查询文章
|
||||||
*
|
*
|
||||||
* @param category category
|
* @param category category
|
||||||
|
* @param status status
|
||||||
* @param pageable pageable
|
* @param pageable pageable
|
||||||
* @return Page
|
* @return Page
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
|
@CachePut(value = POSTS_CACHE_NAME, key = "'posts_category_'+#category.cateId+'_'+#pageable.pageNumber")
|
||||||
public Page<Post> findPostByCategories(Category category, Pageable pageable) {
|
public Page<Post> 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
|
* @param pageable pageable
|
||||||
* @return Page
|
* @return Page
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
@CachePut(value = POSTS_CACHE_NAME, key = "'posts_tag_'+#tag.tagId+'_'+#pageable.pageNumber")
|
@CachePut(value = POSTS_CACHE_NAME, key = "'posts_tag_'+#tag.tagId+'_'+#pageable.pageNumber")
|
||||||
public Page<Post> findPostsByTags(Tag tag, Pageable pageable) {
|
public Page<Post> findPostsByTags(Tag tag, Pageable pageable) {
|
||||||
return postRepository.findPostsByTags(tag, pageable);
|
return postRepository.findPostsByTagsAndPostStatus(tag, PostStatus.PUBLISHED.getCode(), pageable);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -80,14 +80,4 @@
|
||||||
timeToIdleSeconds="0"
|
timeToIdleSeconds="0"
|
||||||
timeToLiveSeconds="300"
|
timeToLiveSeconds="300"
|
||||||
memoryStoreEvictionPolicy="LRU"/>
|
memoryStoreEvictionPolicy="LRU"/>
|
||||||
|
|
||||||
<cache
|
|
||||||
name="categories"
|
|
||||||
eternal="false"
|
|
||||||
maxElementsInMemory="100"
|
|
||||||
overflowToDisk="false"
|
|
||||||
diskPersistent="false"
|
|
||||||
timeToIdleSeconds="0"
|
|
||||||
timeToLiveSeconds="300"
|
|
||||||
memoryStoreEvictionPolicy="LRU"/>
|
|
||||||
</ehcache>
|
</ehcache>
|
||||||
|
|
Loading…
Reference in New Issue