• 查询优化的一些经验


      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;
        }
    }
  • 相关阅读:
    Python学习第七天——随机码生成
    Python学习第六天——Fibonacci
    Python学习第五天——递归函数
    Python学习第四天——文件修改
    Python学习第三天——三级目录
    Python学习第二天——购物车程序
    Python学习第一天——用户名登录退出
    Linux什么时候在pc机上有一席之地
    关于系统设计时性能以及可扩展性的考虑
    HyberLedger Fabric学习(4)-chaincode学习(操作人员)
  • 原文地址:https://www.cnblogs.com/fubaizhaizhuren/p/5442027.html
Copyright © 2020-2023  润新知