NewsService.java 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package com.goafanti.news.service;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.cache.annotation.CacheEvict;
  9. import org.springframework.cache.annotation.CachePut;
  10. import org.springframework.cache.annotation.Cacheable;
  11. import org.springframework.stereotype.Service;
  12. import org.springframework.transaction.annotation.Transactional;
  13. import com.goafanti.common.dao.NewsMapper;
  14. import com.goafanti.common.model.News;
  15. import com.goafanti.common.utils.LoggerUtils;
  16. import com.goafanti.core.mybatis.BaseMybatisDao;
  17. import com.goafanti.news.bo.NewsSummary;
  18. @Service
  19. @Transactional
  20. public class NewsService extends BaseMybatisDao<NewsMapper> {
  21. @Autowired
  22. NewsMapper newsMapper;
  23. private static final Logger logger = LoggerFactory.getLogger(NewsService.class);
  24. @Cacheable(value = "NewsCache", key = "'News:'+#id")
  25. public News selectById(Long id) {
  26. return newsMapper.selectByPrimaryKey(id);
  27. }
  28. @Cacheable(value = "NewsListCache", key = "'NewsList:Page:'+#pageNo+'Type:'+#type+'Size:'+#pageSize")
  29. public List<NewsSummary> findNewsList(Integer pageNo, Integer type, Integer pageSize) {
  30. Map<String, Object> params = new HashMap<String, Object>();
  31. params.put("type", type);
  32. params.put("pageSize", pageSize);
  33. params.put("pageNo", pageNo);
  34. return newsMapper.findList(params);
  35. }
  36. @CacheEvict(value = "NewsListCache", allEntries = true)
  37. public void cleanList() {
  38. LoggerUtils.debug(logger, "清除新闻列表缓存");
  39. }
  40. @CacheEvict(value = "NewsCache", key = "'News:'+#id")
  41. public void clean(String id) {
  42. LoggerUtils.debug(logger, "清除新闻缓存:[%s]", id);
  43. }
  44. @CachePut(value = "NewsCache", key = "'News:'+#id")
  45. public News save(Long id, News news) {
  46. news.setId(id);
  47. newsMapper.insert(news);
  48. return news;
  49. }
  50. }