• 查询优化的一些经验


      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;
        }
    }
  • 相关阅读:
    洛谷P3128 [USACO15DEC]Max Flow P 题解 树上差分(点差分)
    数列分块解决区间更新+区间最值问题
    ThinkPad P1 Gen3 4K 显示器出现间歇闪黑屏情况解决
    Qt自定义弹出式菜单(Qt自定义弹窗)
    软件产品易用性评价评估标准
    vue用echarts实现中国地图和世界地图
    知了业务逻辑梳理
    string.gfind string.gmatch
    无法定位程序输入点在 XXXX上...
    [Lua]c解析lua 嵌套table
  • 原文地址:https://www.cnblogs.com/fubaizhaizhuren/p/5442027.html
Copyright © 2020-2023  润新知