🍎 配置ehcache缓存

pull/18/head
ruibaby 2018-06-30 12:46:54 +08:00
parent 9ae9b6d220
commit 1b1ec582ed
12 changed files with 149 additions and 9 deletions

View File

@ -76,7 +76,7 @@ public interface PostService {
List<Post> searchPosts(String keyWord, Pageable pageable);
/**
*
*
*
* @param status 012
* @param postType post or page
@ -85,6 +85,13 @@ public interface PostService {
*/
Page<Post> findPostByStatus(Integer status, String postType, Pageable pageable);
/**
*
* @param pageable pageable
* @return Page
*/
Page<Post> findPostByStatus(Pageable pageable);
/**
*
*

View File

@ -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<Attachment> 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> attachment = this.findByAttachId(attachId);
attachmentRepository.delete(attachment.get());

View File

@ -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> 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<Category> findAllCategories() {
return categoryRepository.findAll();
}

View File

@ -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<Comment> removeByCommentId(Long commentId) {
Optional<Comment> 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<Comment> findAllComments(Integer status) {
return commentRepository.findCommentsByCommentStatus(status);
}
@ -73,6 +81,7 @@ public class CommentServiceImpl implements CommentService {
* @return List<Comment></>
*/
@Override
@Cacheable(value = COMMENTS_CACHE_NAME, key = "'comment'")
public List<Comment> 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> 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<Comment> findCommentsLatest() {
return commentRepository.findTopFive();
}

View File

@ -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> 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<Gallery> findAllGalleries() {
return galleryRepository.findAll();
}

View File

@ -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> link = this.findByLinkId(linkId);
linkRepository.delete(link.get());

View File

@ -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> menu = this.findByMenuId(menuId);
menuRepository.delete(menu.get());

View File

@ -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> 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> 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<Post> 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<Post> findAllPosts(String postType) {
return postRepository.findPostsByPostType(postType);
}
@ -122,7 +132,7 @@ public class PostServiceImpl implements PostService {
}
/**
*
*
*
* @param status 012
* @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<Post> 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<Post> 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<Archive> findPostGroupByYearAndMonth() {
List<Object[]> objects = postRepository.findPostGroupByYearAndMonth();
List<Archive> 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<Archive> findPostGroupByYear() {
List<Object[]> objects = postRepository.findPostGroupByYear();
List<Archive> 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<Post> 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<Post> 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<Post> 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<Post> 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<Post> relatedPosts(Post post) {
//获取当前文章的所有标签
List<Tag> tags = post.getTags();

View File

@ -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> 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<Tag> findAllTags() {
return tagRepository.findAll();
}

View File

@ -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 {

View File

@ -66,7 +66,7 @@ public class FrontIndexController extends BaseController {
}
//所有文章数据,分页
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) {
return this.renderNotFound();
}
@ -93,7 +93,7 @@ public class FrontIndexController extends BaseController {
//文章数据,只获取文章,没有分页
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;
}

View File

@ -30,4 +30,64 @@
timeToIdleSeconds="0"
timeToLiveSeconds="300"
memoryStoreEvictionPolicy="LRU"/>
</ehcache>
<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>