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