1、加缓存
2、list里面需要组装不同的bean,这些bean需要分别查询数据库或缓存,可以查询完之后,建一个5分钟的缓存,下次查,直接从缓存中取
package com.moji.article.web.pattern; import java.sql.Date; import java.util.ArrayList; import java.util.List; import com.moji.article.service.ArticleService; import com.moji.article.service.ArticleStatService; import com.moji.article.web.pattern.ArticleDetailResBean.ArticleResBean; import com.moji.sns.common.constant.article.ArticleMemcachePoolValues; import com.moji.util.common.LocaleUtil; public class ArticleListByIdsStrategyImpl implements ArticleListByIdsStrategy { private ArticleDetailStrategy articleDetailStrategy; private ArticleService articleService; @Override public ArticleListByIdsResBean listByIds(ArticleListByIdsReqForm form) { ArticleListByIdsResBean resBean = new ArticleListByIdsResBean(); resBean.setCode(0); List<Long> articleIdList = form.getParams().getArticle_id_list(); Integer lanId = Integer.parseInt(LocaleUtil.getLocaleByLanguageKey(form .getCommon().getLanguage())); Long snsId = form.getCommon().getSnsid(); // 查5分钟缓存的memCache是否存在 List<ArticleDetailResBean.ArticleResBean> articleBeanList = getFromMem(); if (articleBeanList != null) { resBean.setArticle_list(articleBeanList); return resBean; } articleBeanList = new ArrayList<ArticleDetailResBean.ArticleResBean>(); for (Long articleId : articleIdList) { ArticleDetailResBean.ArticleResBean articleBean = articleDetailStrategy .format(articleId, lanId, snsId); if (articleBean != null && articleBean.getDel_status().intValue() == ArticleStatService.STAT_NOT_DEL .intValue()) { articleBeanList.add(articleBean); } } // 存5分钟缓存 setToMem(articleBeanList); resBean.setArticle_list(articleBeanList); return resBean; } private void setToMem(List<ArticleDetailResBean.ArticleResBean> articleBeanList){ articleService.setBeanToMem( ArticleListByIdsStrategy.ARTICLE_DEATIL_RES_BEAN_KEY, ArticleMemcachePoolValues.LATEST_MEMCACHE_POOL_ARTICLE, articleBeanList, new Date(1000 * 60 * 5)); } private List<ArticleDetailResBean.ArticleResBean> getFromMem(){ @SuppressWarnings("unchecked") List<ArticleDetailResBean.ArticleResBean> articleBeanList = (List<ArticleResBean>) articleService .getBeanFormMemcache( ArticleListByIdsStrategy.ARTICLE_DEATIL_RES_BEAN_KEY, ArticleMemcachePoolValues.LATEST_MEMCACHE_POOL_ARTICLE, List.class); return articleBeanList; } public void setArticleService(ArticleService articleService) { this.articleService = articleService; } public void setArticleDetailStrategy( ArticleDetailStrategy articleDetailStrategy) { this.articleDetailStrategy = articleDetailStrategy; } }
时间: 2024-10-12 14:23:24