您的位置:首页 > 其它

查询优化的一些经验

2016-04-28 11:39 281 查看
  1、加缓存

  2、list里面需要组装不同的bean,这些bean需要分别查询数据库或缓存,可以查询完之后,建一个5分钟的缓存,下次查,直接从缓存中取

  3、读写分离
http://lvwenwen.iteye.com/blog/1486939
  4、tomcat数据源jndi

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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: