@@ -40,6 +40,7 @@ public class PostService {
4040 public static final String CACHE_NAME_ARCHIVE = CACHE_NAME + ".archive" ;
4141 public static final String CACHE_NAME_PAGE = CACHE_NAME + ".page" ;
4242 public static final String CACHE_NAME_TAGS = CACHE_NAME + ".tag" ;
43+ public static final String CACHE_NAME_COUNTS = CACHE_NAME + ".counts_tags" ;
4344
4445 private static final Logger logger = LoggerFactory .getLogger (PostService .class );
4546
@@ -71,7 +72,8 @@ public Post getPublishedPostByPermalink(String permalink){
7172
7273 @ Caching (evict = {
7374 @ CacheEvict (value = CACHE_NAME_ARCHIVE , allEntries = true ),
74- @ CacheEvict (value = CACHE_NAME_PAGE , allEntries = true )
75+ @ CacheEvict (value = CACHE_NAME_PAGE , allEntries = true ),
76+ @ CacheEvict (value = CACHE_NAME_COUNTS , allEntries = true )
7577 })
7678 public Post createPost (Post post ) {
7779 if (post .getPostFormat () == PostFormat .MARKDOWN ) {
@@ -86,7 +88,8 @@ public Post createPost(Post post) {
8688 @ CacheEvict (value = CACHE_NAME , key = "#post.permalink" , condition = "#post.permalink != null" ),
8789 @ CacheEvict (value = CACHE_NAME_TAGS , key = "#post.id.toString().concat('-tags')" ),
8890 @ CacheEvict (value = CACHE_NAME_ARCHIVE , allEntries = true ),
89- @ CacheEvict (value = CACHE_NAME_PAGE , allEntries = true )
91+ @ CacheEvict (value = CACHE_NAME_PAGE , allEntries = true ),
92+ @ CacheEvict (value = CACHE_NAME_COUNTS , allEntries = true )
9093 })
9194 public Post updatePost (Post post ) {
9295 if (post .getPostFormat () == PostFormat .MARKDOWN ) {
@@ -101,7 +104,8 @@ public Post updatePost(Post post) {
101104 @ CacheEvict (value = CACHE_NAME , key = "#post.permalink" , condition = "#post.permalink != null" ),
102105 @ CacheEvict (value = CACHE_NAME_TAGS , key = "#post.id.toString().concat('-tags')" ),
103106 @ CacheEvict (value = CACHE_NAME_ARCHIVE , allEntries = true ),
104- @ CacheEvict (value = CACHE_NAME_PAGE , allEntries = true )
107+ @ CacheEvict (value = CACHE_NAME_PAGE , allEntries = true ),
108+ @ CacheEvict (value = CACHE_NAME_COUNTS , allEntries = true )
105109 })
106110 public void deletePost (Post post ) {
107111 postRepository .delete (post );
@@ -173,6 +177,7 @@ public Set<Tag> parseTagNames(String tagNames){
173177 Set <Tag > tags = new HashSet <>();
174178
175179 if (tagNames != null && !tagNames .isEmpty ()){
180+ tagNames = tagNames .toLowerCase ();
176181 String [] names = tagNames .split ("\\ s*,\\ s*" );
177182 for (String name : names ){
178183 tags .add (tagService .findOrCreateByName (name ));
@@ -193,7 +198,15 @@ public String getTagNames(Set<Tag> tags){
193198 return names .toString ();
194199 }
195200
201+ // cache or not?
196202 public Page <Post > findPostsByTag (String tagName , int page , int pageSize ){
197203 return postRepository .findByTag (tagName , new PageRequest (page , pageSize , Sort .Direction .DESC , "createdAt" ));
198204 }
205+
206+ @ Cacheable (value = CACHE_NAME_COUNTS , key = "#root.method.name" )
207+ public List <Map <String , Long >> countPostsByTags (){
208+ logger .debug ("Count posts group by tags." );
209+
210+ return postRepository .countPostsByTags ();
211+ }
199212}
0 commit comments