Secondary Kill Real Fight - High Concurrency Optimization

Catalog

1. Use caching

Page Cache+URL Cache+Object Cache

Advantages of using these caches:
1. Reduce database pressure (core)
2. Enhance the user experience
3. Enhance system concurrency

(1) Page caching and URL caching

Example: Modify in the GoodsController class

	@ApiOperation(httpMethod = "POST", value = "List of Goods", notes = "Query all items")
	    @ApiImplicitParams({
	            @ApiImplicitParam(name = "id", value = "commodity id", required = true, dataType = "Long")
	    })
	    @RequestMapping(value = "toList", produces = "text/html")
	    @ResponseBody
	    public String toList(
	            HttpServletRequest request, HttpServletResponse response,
	            Model model, MiaoshaUser user) {
	        model.addAttribute("user", user.toString());
	        List<GoodsVO> goodsList = goodsService.listGoodsVo();
	        model.addAttribute("goodsList", goodsList);
	        //Cache fetching
	        String html = redisService.get(GoodsKey.getGoodsList, "", String.class);
	        if (!StringUtils.isEmpty(html)) {
	            return html;
	        }
	        
	        WebContext ctx = new WebContext(request, response,
	                request.getServletContext(), request.getLocale(), model.asMap());
	        //Manual Rendering
	        html = thymeleafViewResolver.getTemplateEngine().process("goodsList", ctx);
	        if (!StringUtils.isEmpty(html)) {
	            redisService.set(GoodsKey.getGoodsList, "", html);
	        }
	        return html;
	    }

Create a new GoodsKey class:

public class GoodsKey extends BasePrefix{

    public GoodsKey(int expireSeconds, String prefix) {
        super(expireSeconds, prefix);
    }
    public static GoodsKey getGoodsList = new GoodsKey(60,"goodsList");
    public static GoodsKey getGoodsDetail = new GoodsKey(60,"goodsDetail");

}

(2) Object Cache

The intercepted fragments are as follows:

 public MiaoshaUser getById(long id) {
        // Cache fetching
        MiaoshaUser user = redisService.get(MiaoshaUserKey.getById,""+id,MiaoshaUser.class);
        if (user != null ){
            return user;
        }
        // Retrieve the database and store it in the cache
        user = miaoshaUserDao.getById(id);
        if (user != null ){
            redisService.set(MiaoshaUserKey.getById,""+id, user);
        }
        return user;
    }

MiaoshaUserKey:

public class MiaoshaUserKey extends BasePrefix{

    private static final int TOKEN_EXPIRE = (3600 * 24 * 2);

    public MiaoshaUserKey(String prefix) {
        super(prefix);
    }

    public MiaoshaUserKey(int expireSeconds, String prefix) {
        super(expireSeconds, prefix);
    }

    public static MiaoshaUserKey token = new MiaoshaUserKey(TOKEN_EXPIRE,"tk");

    public static MiaoshaUserKey getById = new MiaoshaUserKey(0,"id");

}

2. Page Statization

Advantages of page static:

  1. Increase Speed
    For example, dynamic web pages such as jsp need database queries. When the amount of access increases, the number of database queries will also increase, which will occupy a lot of resources and affect the response speed of the website.If the homepage and content are static, the number of queries to the database will be removed, some links will be reduced, and the reaction speed of the website will be accelerated.
    2. Search Engine Recording
    From the optimization of the website, search engines prefer static pages, which are also easy to grab, and have higher ranking of SEO.For example, Baidu, Alibaba and Sohu all use static or pseudo-static web pages to display, which facilitates the capture and ranking of search engines.
    3. Security
    Static web pages are not easily cracked by hackers.Static web pages do not query the database and do not let hackers see the contents of the database.
    4. Website stability
    If there are errors in the background program, database, etc., which affect the access to the website, affect the user's experience and reduce the user's trust, this will not happen on static web pages.

Tags: Database JSP

Posted on Thu, 11 Jun 2020 12:22:48 -0400 by lucidpc