zoukankan      html  css  js  c++  java
  • 代码Review

    背景:

      公司APP社区模块首页数据,第一版从数据库拉取,由于关联表过多,数据量较大,导致查询较慢。通过监控查看接口TPM,实在惨不忍睹。SQL优化后,效果不佳,第二版上线内存数据库redis,将第一页数据缓存到redis中,直接拉取cache。由于当时为了降低数据库压力,急忙上线后,才发现添加缓存带了很多业务上的处理,如:用户点赞,发表动态,评论,回复时缓存的处理,缓存的排序处理等。第三版着重进行代码的Review,填补第二版的坑。

    The Key:

      1、快速定位问题

           分析何种操作触发了问题,通过代码断点调试,进行操作重现问题。(保证测试环境同线上版本一致)

      2、灵活解决问题

           确定问题,找到解决方案后,及时编码验证。若长时间卡在某地方,进行讨论,若方案暂时无法解决,寻找其他解决方案。

    Code:

      1、插入排序

    for (int i = 1, l = TrendsList.Count; i < l; i++)
    {
        int insertIndex = i - 1;var insertVal = TrendsList[i];
        while (insertIndex >= 0 && DateTime.Compare(insertVal.Time, TrendsList[insertIndex].Time) > 0)
        {
            TrendsList[insertIndex + 1] = TrendsList[insertIndex];
            insertIndex--;
        }
        TrendsList[insertIndex + 1] = insertVal;
    }

     插入排序适用于最新,按照发表时间排序(忽略置顶,实际项目中会添加置顶)。

     2、LINQ

    int praiseCount = 0;
    string isTop = "0";
    foreach (var item in TrendsList)
    {
        praiseCount = item.Praise.Count + item.ReplyList.Count * 5;
        isTop = Convert.ToBoolean(item.IsTop) ? "1" : "0";
        if (item.SortKey == null)
        {
            item.SortKey = isTop + praiseCount.ToString("000000000");
        }
    }
    trend.SortKey = "0" + (trend.Praise.Count + trend.ReplyList.Count * 5 + 1).ToString("000000000");
    TrendsList.Insert(0, trend);
    TrendsList = TrendsList.OrderByDescending(o => o.SortKey).ToList();

     实体类添加排序字段(根据热门排序规则),使用List的linq倒序排列,在点赞数暂时未超过100000000时问题得以解决。

  • 相关阅读:
    Hackerrank--Emma and sum of products (FFT)
    Hdu 1402 (FFT)
    Hackerrank--Divisibility of Power(Math)
    Hackerrank--Stock Maximize(DP Practice)
    Codeforces 455B
    vim 简明教程(转自飘过的小牛)
    ACdream 1007 (快速幂)
    编写可维护的JS 02
    编写可维护的JS 01
    图片加载-从模糊到清晰
  • 原文地址:https://www.cnblogs.com/BurtBlog/p/6516171.html
Copyright © 2011-2022 走看看