package com.goafanti.news.service; import java.util.HashMap; import java.util.List; import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.goafanti.common.dao.NewsMapper; import com.goafanti.common.model.News; import com.goafanti.common.utils.LoggerUtils; import com.goafanti.core.mybatis.BaseMybatisDao; import com.goafanti.news.bo.NewsSummary; @Service @Transactional public class NewsService extends BaseMybatisDao { @Autowired NewsMapper newsMapper; private static final Logger logger = LoggerFactory.getLogger(NewsService.class); @Cacheable(value = "NewsCache", key = "'News:'+#id") public News selectById(Long id) { return newsMapper.selectByPrimaryKey(id); } @Cacheable(value = "NewsListCache", key = "'NewsList:Page:'+#pageNo+'Type:'+#type+'Size:'+#pageSize") public List findNewsList(Integer pageNo, Integer type, Integer pageSize) { Map params = new HashMap(); params.put("type", type); params.put("pageSize", pageSize); params.put("pageNo", pageNo); return newsMapper.findList(params); } @CacheEvict(value = "NewsListCache", allEntries = true) public void cleanList() { LoggerUtils.debug(logger, "清除新闻列表缓存"); } @CacheEvict(value = "NewsCache", key = "'News:'+#id") public void clean(String id) { LoggerUtils.debug(logger, "清除新闻缓存:[%s]", id); } @CachePut(value = "NewsCache", key = "'News:'+#id") public News save(Long id, News news) { news.setId(id); newsMapper.insert(news); return news; } }