diff --git a/src/main/java/com/rymcu/vertical/dto/LabelModel.java b/src/main/java/com/rymcu/vertical/dto/LabelModel.java new file mode 100644 index 0000000..ed70e92 --- /dev/null +++ b/src/main/java/com/rymcu/vertical/dto/LabelModel.java @@ -0,0 +1,17 @@ +package com.rymcu.vertical.dto; + +import lombok.Data; + +import java.io.Serializable; + +/** + * @author ronger + */ +@Data +public class LabelModel implements Serializable { + + private String label; + + private String value; + +} diff --git a/src/main/java/com/rymcu/vertical/mapper/TagMapper.java b/src/main/java/com/rymcu/vertical/mapper/TagMapper.java index dcd8d30..ed4fbe2 100644 --- a/src/main/java/com/rymcu/vertical/mapper/TagMapper.java +++ b/src/main/java/com/rymcu/vertical/mapper/TagMapper.java @@ -1,9 +1,12 @@ package com.rymcu.vertical.mapper; import com.rymcu.vertical.core.mapper.Mapper; +import com.rymcu.vertical.dto.LabelModel; import com.rymcu.vertical.entity.Tag; import org.apache.ibatis.annotations.Param; +import java.util.List; + public interface TagMapper extends Mapper { Integer insertTagArticle(@Param("idTag") Integer idTag, @Param("idArticle") Integer idArticle); @@ -16,4 +19,9 @@ public interface TagMapper extends Mapper { Integer deleteUnusedTag(); Integer update(@Param("idTag") Integer idTag, @Param("tagUri") String tagUri, @Param("tagIconPath") String tagIconPath, @Param("tagStatus") String tagStatus, @Param("tagDescription") String tagDescription, @Param("tagReservation") String tagReservation); + + /** + * @return + */ + List selectTagLabels(); } diff --git a/src/main/java/com/rymcu/vertical/service/TagService.java b/src/main/java/com/rymcu/vertical/service/TagService.java index 7c0f4c4..6664bbb 100644 --- a/src/main/java/com/rymcu/vertical/service/TagService.java +++ b/src/main/java/com/rymcu/vertical/service/TagService.java @@ -1,11 +1,13 @@ package com.rymcu.vertical.service; import com.rymcu.vertical.core.service.Service; +import com.rymcu.vertical.dto.LabelModel; import com.rymcu.vertical.entity.Article; import com.rymcu.vertical.entity.Tag; import com.rymcu.vertical.web.api.exception.BaseApiException; import java.io.UnsupportedEncodingException; +import java.util.List; import java.util.Map; /** @@ -34,4 +36,10 @@ public interface TagService extends Service { * @return */ Map saveTag(Tag tag); + + /** + * 获取标签列表 + * @return + */ + List findTagLabels(); } diff --git a/src/main/java/com/rymcu/vertical/service/impl/TagServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/TagServiceImpl.java index d58eed3..16b25fd 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/TagServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/TagServiceImpl.java @@ -1,13 +1,16 @@ package com.rymcu.vertical.service.impl; import com.rymcu.vertical.core.service.AbstractService; +import com.rymcu.vertical.core.service.redis.RedisService; import com.rymcu.vertical.dto.ArticleTagDTO; +import com.rymcu.vertical.dto.LabelModel; import com.rymcu.vertical.entity.Article; import com.rymcu.vertical.entity.Tag; import com.rymcu.vertical.entity.User; import com.rymcu.vertical.mapper.ArticleMapper; import com.rymcu.vertical.mapper.TagMapper; import com.rymcu.vertical.service.TagService; +import com.rymcu.vertical.util.CacheUtils; import com.rymcu.vertical.util.UserUtils; import com.rymcu.vertical.web.api.exception.BaseApiException; import org.apache.commons.lang.StringUtils; @@ -33,6 +36,8 @@ public class TagServiceImpl extends AbstractService implements TagService { private TagMapper tagMapper; @Resource private ArticleMapper articleMapper; + @Resource + private RedisService redisService; @Override @Transactional(rollbackFor = { UnsupportedEncodingException.class,BaseApiException.class }) @@ -139,4 +144,14 @@ public class TagServiceImpl extends AbstractService implements TagService { } return map; } + + @Override + public List findTagLabels() { + List list = (List) CacheUtils.get("tags"); + if (list == null) { + list = tagMapper.selectTagLabels(); + CacheUtils.put("tags", list); + } + return list; + } } diff --git a/src/main/java/com/rymcu/vertical/util/CacheUtils.java b/src/main/java/com/rymcu/vertical/util/CacheUtils.java new file mode 100644 index 0000000..c532cd7 --- /dev/null +++ b/src/main/java/com/rymcu/vertical/util/CacheUtils.java @@ -0,0 +1,139 @@ +package com.rymcu.vertical.util; + +import org.apache.shiro.cache.Cache; +import org.apache.shiro.cache.CacheManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Iterator; +import java.util.Set; + +/** + * Cache工具类 + */ +public class CacheUtils { + + private static Logger logger = LoggerFactory.getLogger(CacheUtils.class); + private static CacheManager cacheManager = SpringContextHolder.getBean(CacheManager.class); + + private static final String SYS_CACHE = "system"; + + /** + * 获取SYS_CACHE缓存 + * @param key + * @return + */ + public static Object get(String key) { + return get(SYS_CACHE, key); + } + + /** + * 获取SYS_CACHE缓存 + * @param key + * @param defaultValue + * @return + */ + public static Object get(String key, Object defaultValue) { + Object value = get(key); + return value != null ? value : defaultValue; + } + + /** + * 写入SYS_CACHE缓存 + * @param key + * @return + */ + public static void put(String key, Object value) { + put(SYS_CACHE, key, value); + } + + /** + * 从SYS_CACHE缓存中移除 + * @param key + * @return + */ + public static void remove(String key) { + remove(SYS_CACHE, key); + } + + /** + * 获取缓存 + * @param cacheName + * @param key + * @return + */ + public static Object get(String cacheName, String key) { + return getCache(cacheName).get(getKey(key)); + } + + /** + * 获取缓存 + * @param cacheName + * @param key + * @param defaultValue + * @return + */ + public static Object get(String cacheName, String key, Object defaultValue) { + Object value = get(cacheName, getKey(key)); + return value != null ? value : defaultValue; + } + + /** + * 写入缓存 + * @param cacheName + * @param key + * @param value + */ + public static void put(String cacheName, String key, Object value) { + getCache(cacheName).put(getKey(key), value); + } + + /** + * 从缓存中移除 + * @param cacheName + * @param key + */ + public static void remove(String cacheName, String key) { + getCache(cacheName).remove(getKey(key)); + } + + /** + * 从缓存中移除所有 + * @param cacheName + */ + public static void removeAll(String cacheName) { + Cache cache = getCache(cacheName); + Set keys = cache.keys(); + for (Iterator it = keys.iterator(); it.hasNext();){ + cache.remove(it.next()); + } + logger.info("清理缓存: {} => {}", cacheName, keys); + } + + /** + * 获取缓存键名,多数据源下增加数据源名称前缀 + * @param key + * @return + */ + private static String getKey(String key){ +// String dsName = DataSourceHolder.getDataSourceName(); +// if (StringUtils.isNotBlank(dsName)){ +// return dsName + "_" + key; +// } + return key; + } + + /** + * 获得一个Cache,没有则显示日志。 + * @param cacheName + * @return + */ + private static Cache getCache(String cacheName){ + Cache cache = cacheManager.getCache(cacheName); + if (cache == null){ + throw new RuntimeException("当前系统中没有定义“"+cacheName+"”这个缓存。"); + } + return cache; + } + +} diff --git a/src/main/java/com/rymcu/vertical/web/api/tag/TagController.java b/src/main/java/com/rymcu/vertical/web/api/tag/TagController.java index 700bac3..406645d 100644 --- a/src/main/java/com/rymcu/vertical/web/api/tag/TagController.java +++ b/src/main/java/com/rymcu/vertical/web/api/tag/TagController.java @@ -5,7 +5,10 @@ import com.github.pagehelper.PageInfo; import com.rymcu.vertical.core.result.GlobalResult; import com.rymcu.vertical.core.result.GlobalResultGenerator; import com.rymcu.vertical.dto.ArticleDTO; +import com.rymcu.vertical.dto.LabelModel; +import com.rymcu.vertical.entity.Tag; import com.rymcu.vertical.service.ArticleService; +import com.rymcu.vertical.service.TagService; import com.rymcu.vertical.util.Utils; import org.springframework.web.bind.annotation.*; @@ -22,6 +25,8 @@ public class TagController { @Resource private ArticleService articleService; + @Resource + private TagService tagService; @GetMapping("/{name}") public GlobalResult articles(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer rows, @PathVariable String name){ @@ -31,4 +36,10 @@ public class TagController { Map map = Utils.getArticlesGlobalResult(pageInfo); return GlobalResultGenerator.genSuccessResult(map); } + + @GetMapping("/tags") + public GlobalResult tags() { + List list = tagService.findTagLabels(); + return GlobalResultGenerator.genSuccessResult(list); + } } diff --git a/src/main/java/mapper/TagMapper.xml b/src/main/java/mapper/TagMapper.xml index 4f4d437..6425fc0 100644 --- a/src/main/java/mapper/TagMapper.xml +++ b/src/main/java/mapper/TagMapper.xml @@ -18,6 +18,10 @@ + + + + insert into vertical_tag_article (id_tag,id_article,created_time,updated_time) values (#{idTag},#{idArticle},sysdate(),sysdate()) @@ -36,4 +40,7 @@ + \ No newline at end of file