Skip to content

Commit

Permalink
Service层添加缓存注解
Browse files Browse the repository at this point in the history
  • Loading branch information
ramostear committed Jul 2, 2020
1 parent eb63637 commit 712614d
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public interface PostRepository extends BaseRepository<Post,Integer>, JpaSpecifi
List<Object[]> findAllArchiveByStatus(Integer status);

@Query(value = "SELECT P.* FROM POSTS AS P WHERE " +
"DATE_FOMAT(P.CREATE_TIME,'%Y-%m')=?1 AND P.STATUS=?2 AND P.STYLE=0 " +
"DATE_FORMAT(P.CREATE_TIME,'%Y-%m')=?1 AND P.STATUS=?2 AND P.STYLE=0 " +
"ORDER BY P.CREATE_TIME DESC",nativeQuery = true)
List<Post> findAllPostByArchiveAndStatus(String archive,Integer status);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,6 @@ public interface PostCategoryService extends BaseService<PostCategory,Integer> {

@NonNull
Post findTopPostByCategoryId(@NonNull Integer categoryId);

List<Post> findAllPostByCategoryId(Integer id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.ramostear.unaboot.service.PostTagService;
import com.ramostear.unaboot.util.ComponentUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

Expand Down Expand Up @@ -40,6 +41,7 @@ public class ArchiveServiceImpl implements ArchiveService {
}

@Override
@Cacheable(value = "archives")
public List<ArchiveVo> archives() {
List<Object[]> data = postRepository.findAllArchiveByStatus(PostStatus.ACTIVE);
List<ArchiveVo> vos = new ArrayList<>();
Expand All @@ -48,14 +50,15 @@ public List<ArchiveVo> archives() {
Object[] objects = item;
ArchiveVo vo = new ArchiveVo();
vo.setName(objects[0].toString());
vo.setCounts(Long.valueOf(objects[0].toString()));
vo.setCounts(Long.valueOf(objects[1].toString()));
vos.add(vo);
});
}
return vos;
}

@Override
@Cacheable(value = "archives",key = "#name")
public List<PostSimpleVo> posts(String name) {
if(StringUtils.isBlank(name)){
return Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.ramostear.unaboot.util.DateTimeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -70,11 +71,13 @@ public List<Category> findAllByPid(Integer pid) {
}

@Override
@Cacheable(value = "category")
public List<Category> navigation() {
return categoryRepository.findAllByNavShowOrderBySortIdAsc(1);
}

@Override
@Cacheable(value = "category",key = "#slug")
public Category findBySlug(String slug) {
Assert.notNull(slug,"Category slug must not be null.");
return categoryRepository.getBySlug(slug).orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.ramostear.unaboot.service.LinkService;
import com.ramostear.unaboot.util.DateTimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -28,6 +29,7 @@ public LinkServiceImpl(LinkRepository linkRepository) {
}

@Override
@Cacheable(value = "link")
public List<Link> findAll() {
return super.findAll();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,13 @@ public Post findTopPostByCategoryId(Integer categoryId) {
}
return null;
}

@Override
public List<Post> findAllPostByCategoryId(Integer id) {
List<PostCategory> pcs = postCategoryRepository.findAllByCategoryId(id);
List<Integer> postIds = pcs.stream()
.map(PostCategory::getPostId)
.distinct().collect(Collectors.toList());
return postRepository.findAllById(postIds);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.ramostear.unaboot.util.DateTimeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
Expand Down Expand Up @@ -172,6 +173,7 @@ public List<PostSimpleVo> findRecommendPosts(int size) {
}

@Override
@Cacheable(value = "posts",key = "#slug")
public Post findBySlug(String slug) {
if(StringUtils.isBlank(slug)){
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.ramostear.unaboot.service.PostTagService;
import com.ramostear.unaboot.util.ComponentUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -60,6 +61,7 @@ public List<Post> findAllPostByTagId(Integer id) {
}

@Override
@Cacheable(value = "tag",key = "#id")
public List<Post> findAllPostByTagIdAndPostStatus(Integer id, int status) {
Set<Integer> postIds = postTagRepository.findAllPostByTagIdAndPostStatus(id,status);
if(CollectionUtils.isEmpty(postIds)){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.apache.shiro.util.Assert;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -60,6 +61,7 @@ public Tag delete(Integer id) {
}

@Override
@Cacheable(value = "tag",key = "#slug")
public Tag findBySlug(String slug) {
Assert.notNull(slug,"Tag slug must not be null.");
return tagRepository.findBySlug(slug);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import com.ramostear.unaboot.component.FileManager;
import com.ramostear.unaboot.domain.entity.Category;
import com.ramostear.unaboot.domain.entity.Post;
import com.ramostear.unaboot.domain.vo.CategoryVo;
import com.ramostear.unaboot.exception.UnaBootException;
import com.ramostear.unaboot.service.CategoryService;
import com.ramostear.unaboot.service.PostCategoryService;
import com.ramostear.unaboot.service.PostService;
import com.ramostear.unaboot.service.ThemeService;
import com.ramostear.unaboot.web.UnaBootController;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -14,6 +17,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
Expand All @@ -32,19 +36,26 @@ public class CategoryController extends UnaBootController {

private final CategoryService categoryService;

private final PostCategoryService postCategoryService;

private final ServletContext servletContext;

private final ThemeService themeService;

private final FileManager fileManager;

private final PostService postService;

@Autowired
CategoryController(CategoryService categoryService,ServletContext servletContext,
ThemeService themeService,FileManager fileManager){
ThemeService themeService,FileManager fileManager,
PostService postService,PostCategoryService postCategoryService){
this.postCategoryService = postCategoryService;
this.categoryService = categoryService;
this.servletContext = servletContext;
this.themeService = themeService;
this.fileManager = fileManager;
this.postService = postService;
}

@GetMapping("/")
Expand Down Expand Up @@ -108,11 +119,22 @@ public ResponseEntity<Object> category(@PathVariable("id")Integer id,Category ca
return bad();
}else{
String thumb = original.getThumb();
String postTheme = original.getPostTheme();
BeanUtils.copyProperties(category,original,"id","createTime","updateTime");
categoryService.update(original);
if(StringUtils.isNotBlank(thumb)){
fileManager.remove(thumb);
}
if(!postTheme.equals(original.getPostTheme())){
postTheme = original.getPostTheme();
List<Post> postList = postCategoryService.findAllPostByCategoryId(original.getId());
if(!CollectionUtils.isEmpty(postList)){
for(Post post : postList){
post.setTpl(postTheme);
}
postService.update(postList);
}
}
return ok();
}
}
Expand Down
39 changes: 39 additions & 0 deletions src/main/resources/ehcache.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,43 @@
eternal="true"
overflowToDisk="true">
</cache>
<cache name="category"
maxEntriesLocalHeap="100"
eternal="true"
overflowToDisk="true">
</cache>
<cache name="link"
maxEntriesLocalHeap="100"
eternal="true"
overflowToDisk="true">
</cache>
<cache name="tag"
maxEntriesLocalHeap="500"
eternal="true"
overflowToDisk="true">
</cache>
<cache name="tagPage"
maxEntriesLocalHeap="500"
eternal="true"
overflowToDisk="true">
</cache>
<cache name="archives"
maxEntriesLocalHeap="100"
eternal="true"
overflowToDisk="true">
</cache>
<cache name="posts"
maxEntriesLocalHeap="500"
eternal="true"
overflowToDisk="true">
</cache>
<cache name="search"
maxEntriesLocalHeap="500"
eternal="true"
overflowToDisk="true">
</cache>
</ehcache>

0 comments on commit 712614d

Please sign in to comment.